From cbf60ae37c806d9f5b30c537cbff0b9ea299cd8d Mon Sep 17 00:00:00 2001 From: Tsubasa Date: Fri, 19 Jul 2024 22:19:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E8=90=BD=E5=AD=90=E5=8F=8A=E7=82=B9=E5=87=BB=E8=BE=B9=E7=95=8C?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=B4=A9=E6=BA=83=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GomokuCode/window.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/GomokuCode/window.py b/GomokuCode/window.py index 3e9e08f..c77d460 100755 --- a/GomokuCode/window.py +++ b/GomokuCode/window.py @@ -120,6 +120,7 @@ def mouseMoveEvent(self, e): game_x = -1 game_y = -1 + # 2. 然后判断鼠标位置较前一时刻是否发生了变化 pos_change = False # 标记鼠标位置是否发生了变化 if game_x != self.last_pos[0] or game_y != self.last_pos[1]: @@ -150,15 +151,24 @@ def mousePressEvent(self, e): game_y = int((mouse_y + 15) // 40) - 1 else: # 鼠标点击的位置不正确 return - self.g.move_1step(True, game_x, game_y) - # 2. 根据操作结果进行一轮游戏循环 + # 2. 检查点击的位置是否已经有棋子 + if self.g.g_map[game_x][game_y] != 0: + return # 若点击的位置有棋子,则忽略操作 + + # 添加边界检查 + if not (0 <= game_x < 15 and 0 <= game_y < 15): + return # 若点击的位置超出边界,则忽略操作 + + # 3. 执行落子操作 + self.g.move_1step(True, game_x, game_y) res, self.flash_pieces = self.g.game_result(show=True) # 判断游戏结果 if res != 0: # 如果游戏结果为“已经结束”,则显示游戏内容,并退出主循环 self.repaint(0, 0, 650, 650) self.game_restart(res) return - # self.g.ai_move_1step() # 电脑下一步 + + # 电脑下一步 self.g.ai_play_1step() # 电脑下一步 res, self.flash_pieces = self.g.game_result(show=True) if res != 0: