我正在尝试制作俄罗斯方块游戏。到目前为止我完成了一切,除了旋转功能。所以,总结一下。计划是将以下数组元素旋转90°:(在本例中为bool数组)
. . . . . . . . . .
. . x . . . . . . .
. . x x . ====== > . . x x .
. . . x . . x x . .
. . . . . . . . . .
为此,我编写了以下代码:
private bool[,] rotateGrid(bool[,] _grid)
{
bool[,] g = new bool[5, 5];
for(int i=0; i<5; i++)
{
bool[] row = new bool[5];
for(int j=4; j>=0; j--)
{
int jInvert = 4 - j;
row[jInvert] = grid[j, i];
}
for(int j=0; j<5; j++)
{
grid[i, j] = row[j];
}
}
return g;
}
不知怎的,如果我用一个完整的数组调用这个函数,它会重新调整一个空函数。
为什么?
答案 0 :(得分:0)
java中的我的代码,只需将其转换为c#即可。 如果您想更好地执行此操作,请使用lambda表达式。 使用lambda表达式,您可以在一行代码中执行此操作:)
/*rotates a block 90* right*/
private static boolean[][] rotateBlock(boolean[][] block){
boolean [][] temp;
int longestRow = longestRow(block);
temp = new boolean[longestRow][block.length];
int counter = 0;
for(int i = 0; i < longestRow;i++){
for(int j = 0; j < block.length;j++){
if(i < block[j].length){
if(block[j][i]){
temp[i][counter] = true;
counter++;
}
}
else{
//null = false
temp[i][counter] = false;
counter++;
}
}
counter = 0;
}
return temp;
}