# How to rotate a two dimensional array by 90 degrees clockwise or counter clockwise like Knightfall game

I was playing an old game called Knightfall when something came to my mind… ok, you can rotate the Display Object and this is easy, but how can you rotate the two dimensional array which is obviously storing game field information?

I mean some function which will turn an array like this:

```[a][b][d]
[e][f][g][h]
[i][j][k][l]
[m][n][o][p]```

into this one:

```[m][i][e][a]
[n][j][f][b]
[o][k][g]
[p][l][h][d]```

It seems an easy task but it’s not… and around the web you can only find examples building a new array, but I wanted to transform the array itself.

So I am sharing you these two functions to rotate by 90 degrees clockwise and counter clockwise any two dimensional array with the same number of rows and columns.

```package {
import flash.display.Sprite;
public class Main extends Sprite {
public function Main() {
var myArray:Array=[["a","b","c","d"],["e","f","g","h"],["i","j","k","l"],["m","n","o","p"]];
traceArray(myArray);
rotateClockwise(myArray);
traceArray(myArray);
rotateCounterClockwise(myArray);
traceArray(myArray);
}
private function traceArray(a:Array) {
for (var i:int=0; i
And this is the result: first, the original array, then the same array rotated clockwise, and last the rotated array rotated again, counter clockwise, to give us the original array.
a b c d
e f g h
i j k l
m n o p
-------
d h l p
c g k o
b f j n
a e i m
-------
a b c d
e f g h
i j k l
m n o p
-------
Hope you will find it useful, what about a rotating Bejeweled game?
```
214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 10000000
// 2 Cars
// 2048
// Avoider
// Ballz
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Breakout
// Bricks
// Columns
// CubesOut
// Dots
// DROP'd
// Dudeski
// Eskiv
// Filler
// Fling
// Globe
// HookPod
// Hundreds
// InkTd
// Iromeku
// Lumines
// Magick
// MagOrMin
// Maze
// Memdot
// Nano War
// Nodes
// o:anquan
// Ononmin
// Pacco
// Phyballs
// Platform
// Poker
// Pool
// Poux
// Pudi
// qomp
// Racing
// Renju
// SameGame
// Security
// Sling
// Slingy
// Sokoban
// Splitter
// Sproing
// Stack
// Stairs
// Stringy
// Sudoku
// Tetris
// Threes
// Toony
// Turn
// TwinSpin
// vvvvvv
// Wordle
// Worms
// Yanga
// Zhed
// zNumbers