diff --git a/list.md b/list.md index 13578cb..8652613 100644 --- a/list.md +++ b/list.md @@ -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` ## 第三阶段:方块移动与旋转 diff --git a/report/code-snippets/Part2/CanMoveRightAfter.png b/report/code-snippets/Part2/CanMoveRightAfter.png new file mode 100644 index 0000000..1324b96 Binary files /dev/null and b/report/code-snippets/Part2/CanMoveRightAfter.png differ diff --git a/report/code-snippets/Part2/CanMoveRightBefore.png b/report/code-snippets/Part2/CanMoveRightBefore.png new file mode 100644 index 0000000..89d2e6d Binary files /dev/null and b/report/code-snippets/Part2/CanMoveRightBefore.png differ diff --git a/src/source/TetrisLogic.cpp b/src/source/TetrisLogic.cpp index 73ea7aa..2b31148 100644 --- a/src/source/TetrisLogic.cpp +++ b/src/source/TetrisLogic.cpp @@ -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; }