Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions GomokuCode/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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为平局"""
Expand Down
6 changes: 5 additions & 1 deletion GomokuCode/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) # 判断游戏结果
Expand Down