实现 CanMoveRight 右移碰撞判断 完成函数8

This commit is contained in:
2026-04-24 08:24:47 +08:00
parent 0221841ad5
commit ddffcf2466
4 changed files with 36 additions and 2 deletions
+1 -1
View File
@@ -18,7 +18,7 @@
- [x] `6. CanMoveDown` - `src/source/TetrisLogic.cpp`
- [x] `7. CanMoveLeft` - `src/source/TetrisLogic.cpp`
- [ ] `8. CanMoveRight` - `src/source/TetrisLogic.cpp`
- [x] `8. CanMoveRight` - `src/source/TetrisLogic.cpp`
## 第三阶段:方块移动与旋转
Binary file not shown.

After

Width:  |  Height:  |  Size: 218 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

+35 -1
View File
@@ -149,9 +149,43 @@ bool CanMoveLeft()
return true;
}
/**
* @brief 判断当前方块是否可以继续向右移动。
*
* 遍历当前处于活动状态下方块的 4x4 矩阵,计算其向右移动一步(X 坐标加 1)后的位置,
* 并检查每个非空方块单元:
* 1. 是否超出了游戏工作区的右侧边界(对应数组索引 >= 10)。
* 2. 是否与工作区右侧已经固定的其他方块发生碰撞(即对应位置的值不为 0)。
* 如果遇到以上任意一种情况,则认为方块受到阻挡,无法继续右移。
*
* @return bool 如果可以继续安全右移返回 true,否则返回 false。
*/
bool CanMoveRight()
{
// TODO(作业8): 判断当前方块是否可以向右移动。
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (bricks[type][state][i][j] != 0)
{
int nextY = point.y + i;
int nextX = point.x + j + 1;
// 检查是否到达右侧边界
if (nextX >= nGameWidth)
{
return false;
}
// 检查右侧是否有其他固定方块
if (workRegion[nextY][nextX] != 0)
{
return false;
}
}
}
}
return true;
}