c - 为什么我的 3d 数组是相反的?

标签 c arrays tetris

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

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

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

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

每个方向和尺寸应通过 block 的 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] 时,它返回我之前为 shape->orientations[0] 设置的值。任何帮助将不胜感激,提前致谢。

最佳答案

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

关于c - 为什么我的 3d 数组是相反的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30205190/

相关文章:

sql - 根据标签交集获取相似的行

javascript - 为什么俄罗斯方 block 会同时掉落而不是一次掉落一个?

c - 如何在C中向下移动二维数组元素

C 如何让 scanf 在空白处停止?

c - 我的 mex 文件有一些问题

C - strtok() 的段错误

c - 函数结束后如何 "free"变量?

javascript - 在函数外部填充数组不起作用

javascript - js 与数组匹配搜索

c++ - 俄罗斯方 block :类的布局