I am programming a Tetris clone and in my game I store my tetromino blocks as 4x4 arrays of blocks. I now need to be able to rotate the integer positions in the arrays so th
js code for both clockwise and counter-clockwise:
function arrRotation90(arr, clockwise) {
var arr_rotated = [];
for (var i = 0; i < arr[0].length; i++) {
arr_rotated[i] = [];
}
if (clockwise) {
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr[i].length; j++) {
arr_rotated[arr[i].length-1-j][i] = arr[i][j];
}
}
} else {
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr[i].length; j++) {
arr_rotated[j][arr.length - 1 - i] = arr[i][j];
}
}
}
return arr_rotated;
}