Skip to content
Open
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
16 changes: 13 additions & 3 deletions GomokuCode/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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:
Expand Down