为什么我的3D阵列会反转?

时间:2015-05-13 03:33:49

标签: c arrays tetris

我有一个存储在结构中的3d数组,代表一个俄罗斯方块:

typedef struct
{
  int orientations[4][4][4];
  dimension dimensions[4];
  int i;
  int x;
  int y;
}block;

方向填充了块的每个可能位置,而维度是一个为碰撞检查提供信息的结构:

typedef struct 
{
int left, right, bottom;
}dimension;

每个方向和维度应该通过块的i值链接。由于某种原因,取向(但不是维度)似乎是相反的。有谁知道这是为什么?

以下是我如何为维度分配值:

block* shape = malloc(sizeof(block));
shape->dimensions[0].left = 0;
shape->dimensions[0].right = 3;
shape->dimensions[0].bottom = 1;
shape->dimensions[1].left = 2;
shape->dimensions[1].right = 2;
shape->dimensions[1].bottom = 3;
shape->dimensions[2].left = 0;
shape->dimensions[2].right = 3;
shape->dimensions[2].bottom = 2;
shape->dimensions[3].left = 1;
shape->dimensions[3].right = 1;
shape->dimensions[3].bottom = 3;

和方向:

int first[4][4] = {{0,0,0,0}, {2,2,2,2}, {0,0,0,0}, {0,0,0,0}};
int second[4][4] = {{0,0,2,0},{0,0,2,0},{0,0,2,0},{0,0,2,0}};
int third[4][4] = {{0,0,0,0},{0,0,0,0},{2,2,2,2},{0,0,0,0}};
int fourth[4][4] = {{0,2,0,0},{0,2,0,0},{0,2,0,0},{0,2,0,0}};
for (i = 0; i < 4; i++)
{
  for (j = 0; j < 4; j++)
  {
    shape->orientations[0][i][j] = first[i][j];
  }
}
for (i = 0; i < 4; i++)
{
  for (j = 0; j < 4; j++)
  {
    shape->orientations[1][i][j] = second[i][j];
  }
}
for (i = 0; i < 4; i++)
{
  for (j = 0; j < 4; j++)
  {
    shape->orientations[2][i][j] = third[i][j];
  }
}
for (i = 0; i < 4; i++)
{
  for (j = 0; j < 4; j++)
  {
    shape->orientations[3][i][j] = fourth[i][j];
  }
}

这是我如何访问数组:

shape->orientations[current->i][i][j]

当我尝试访问shape-&gt;方向[3]时,它返回我之前设置为shape-&gt; orientationations [0]的值。非常感谢任何帮助,提前谢谢。

1 个答案:

答案 0 :(得分:0)

原因是您的数组已编入索引[orientation][row][column],但是当您将其解释为[orientation][x][y]时,您将其解释为[orientation][y][x]。所以你得到一个交换了x和y的模式,看起来好像它已被旋转到不同的方向(实际上它等同于旋转和翻转)。

相关问题