为什么我的 3d 数组是颠倒的?

Why is my 3d array reversed?

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

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

orientations 填充了块的每个可能位置,dimension 是一个提供碰撞检查信息的结构:

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->orientations[3] 时,它 returns 我之前设置为 shape->orientations[0] 的值。任何帮助将不胜感激,提前致谢。

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