diff --git a/list.md b/list.md index b2fed32..57269b1 100644 --- a/list.md +++ b/list.md @@ -35,7 +35,7 @@ 简要说明:完成方块落地后的固定、游戏结束判断、预测落点与重新开始等状态控制逻辑。 - [x] `14. Fixing` - `src/source/TetrisLogic.cpp` -- [ ] `17. GameOver` - `src/source/TetrisLogic.cpp` +- [x] `17. GameOver` - `src/source/TetrisLogic.cpp` - [ ] `18. ComputeTarget` - `src/source/TetrisLogic.cpp` - [ ] `19. Restart` - `src/source/TetrisLogic.cpp` diff --git a/report/code-snippets/Part4/GameOverAfter.png b/report/code-snippets/Part4/GameOverAfter.png new file mode 100644 index 0000000..991b0ee Binary files /dev/null and b/report/code-snippets/Part4/GameOverAfter.png differ diff --git a/report/code-snippets/Part4/GameOverBefore.png b/report/code-snippets/Part4/GameOverBefore.png new file mode 100644 index 0000000..827b511 Binary files /dev/null and b/report/code-snippets/Part4/GameOverBefore.png differ diff --git a/src/source/TetrisLogic.cpp b/src/source/TetrisLogic.cpp index 1d38469..99c32c9 100644 --- a/src/source/TetrisLogic.cpp +++ b/src/source/TetrisLogic.cpp @@ -299,9 +299,38 @@ void DeleteLines() // TODO(作业16): 完成消行与计分逻辑。 } +/** + * @brief 判断游戏是否结束。 + * + * 当新的活动方块生成到初始位置后,如果它的任意一个非空单元 + * 与工作区中已经固定的方块发生重叠,则说明顶部已被占满, + * 当前局面无法继续生成新方块,游戏结束。 + * + * @return bool 若游戏结束返回 true,否则返回 false。 + */ bool GameOver() { - // TODO(作业17): 判断游戏是否结束。 + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 4; j++) + { + if (bricks[type][state][i][j] != 0) + { + int checkY = point.y + i; + int checkX = point.x + j; + + // 检查新方块生成位置是否与固定方块重叠 + if (checkY >= 0 && checkY < nGameHeight && checkX >= 0 && checkX < nGameWidth) + { + if (workRegion[checkY][checkX] != 0) + { + return true; + } + } + } + } + } + return false; }