From 421f8afa51f8e665ba0843cb027fa59fdbff0ead Mon Sep 17 00:00:00 2001 From: sy_ck Date: Wed, 17 Jul 2024 23:03:19 +0800 Subject: [PATCH] fix the repeated click problem --- GomokuCode/game.py | 29 +++++++++++++++++------------ GomokuCode/window.py | 6 +++++- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/GomokuCode/game.py b/GomokuCode/game.py index bd4cf14..fb02345 100755 --- a/GomokuCode/game.py +++ b/GomokuCode/game.py @@ -23,18 +23,23 @@ def move_1step(self, input_by_window=False, pos_x=None, pos_y=None): :param pos_x: 从图形界面输入时,输入的x坐标为多少 :param pos_y: 从图形界面输入时,输入的y坐标为多少 """ - while True: - try: - if not input_by_window: - pos_x = int(input('x: ')) # 接受玩家的输入人 - pos_y = int(input('y: ')) - if 0 <= pos_x <= 14 and 0 <= pos_y <= 14: # 判断这个格子能否落子 - if self.g_map[pos_x][pos_y] == 0: - self.g_map[pos_x][pos_y] = 1 - self.cur_step += 1 - return - except ValueError: # 玩家输入不正确的情况(例如输入了‘A’) - continue + + if not input_by_window: + pos_x = int(input('x: ')) # 接受玩家的输入人 + pos_y = int(input('y: ')) + if 0 <= pos_x <= 14 and 0 <= pos_y <= 14: # 判断这个格子能否落子 + if self.g_map[pos_x][pos_y] == 0: + self.g_map[pos_x][pos_y] = 1 + self.cur_step += 1 + ''' + 正确落子之后返回true + ''' + return True + ''' + 没有正确落子后返回false + ''' + return False + def game_result(self, show=False): """判断游戏的结局。0为游戏进行中,1为玩家获胜,2为电脑获胜,3为平局""" diff --git a/GomokuCode/window.py b/GomokuCode/window.py index 3e9e08f..fd1dea4 100755 --- a/GomokuCode/window.py +++ b/GomokuCode/window.py @@ -150,7 +150,11 @@ def mousePressEvent(self, e): game_y = int((mouse_y + 15) // 40) - 1 else: # 鼠标点击的位置不正确 return - self.g.move_1step(True, game_x, game_y) + ''' + 添加一个判断,如果落子失败不执行任何操作 + ''' + if self.g.move_1step(True, game_x, game_y) == False: + return # 2. 根据操作结果进行一轮游戏循环 res, self.flash_pieces = self.g.game_result(show=True) # 判断游戏结果