I am trying to make a Tetris game. I am trying to work on a function that rotates a 2D variable array 90 degrees (or -90).
For example, given an array like:
"-T-",
"TTT"
It would output:
"T-",
"TT",
"T-"
I have tried this function:
function rotateN90(a){
var temp = [];
for(var x = 0; x<a[0].length; x++){
temp.push("");
for(var y = 0; y<a.length; y++){
temp[x] += a[y][x];
}
}
return temp;
}
But it does not give the desired result. While it does rotate the first T-Block example given -90 degrees once, afterwards it reverts to it's original state.
Please help!
(PS: I am using KA's processing environment, so I can't use libraries or ES6)
The following code is to rotate a mxn size array to -90 degree.
function rotateN90(a){
var temp = new Array(a[0].length); // number of columns
var i=0;
for (i = 0; i < temp.length; i++) {
temp[i] = [];
}
for(i=0;i<a.length;i++){
for(let j = 0; j<a[0].length;j++){
temp[j][i]= a[i][a[i].length-1-j];
}
}
return temp;
}
If your array is : [[1, 2,3],[4, 5, 6]]
It will rotate -90 degree and returned array will be [[3, 6],[2, 5],[1, 4]]