def select_move(self, game_state): """Choose a random valid move that preserves our own eyes.""" candidates = [] for r in range(1, game_state.board.num_rows + 1): for c in range(1, game_state.board.num_cols + 1): candidate = Point(row=r, col=c) if game_state.is_valid_move(Move.play(candidate)) and \ not is_point_an_eye(game_state.board, candidate, game_state.next_player): candidates.append(candidate) if not candidates: return Move.pass_turn() return Move.play(random.choice(candidates))
def select_move(self, game_state): """Choose a random valid move that preserves our own eyes.""" dim = (game_state.board.num_rows, game_state.board.num_cols) if dim != self.dim: self._update_cache(dim) idx = np.arange(len(self.point_cache)) np.random.shuffle(idx) for i in idx: p = self.point_cache[i] if game_state.is_valid_move(Move.play(p)) and \ not is_point_an_eye(game_state.board, p, game_state.next_player): return Move.play(p) return Move.pass_turn()
def legal_moves(self): # 所有合法的操作 moves = [] for row in range(1, self.board.num_rows + 1): for col in range(1, self.board.num_cols + 1): move = Move.play(Point(row, col)) if self.is_valid_move(move): moves.append(move) moves.append(Move.pass_turn()) moves.append(Move.resign()) moves.append(Move.restart()) moves.append(Move.turn_back()) return moves
def playGame(screen, event): global game, game_mode if game_mode == 1: if is_in_area(event.pos, pos_restart_button): # 点击“重新开始” move = Move(is_restart=True) game = game.apply_move(move) draw_chessBoard(screen) elif is_in_area(event.pos, pos_turn_back_button): # 点击返回按钮 move = Move(is_turn_back=True) game = game.apply_move(move) select_gameMode(screen) if not game.is_over(): # 当前对局未结束时以下功能可用 if is_in_area(event.pos, pos_board): # 合法落子 point = Human.go_strategy(event.pos) move = Move.play(point) if game.is_valid_move(move): game = game.apply_move(move) draw_chessBoard(screen) draw_stones(game.board, screen) elif is_in_area(event.pos, pos_regret_button): # 点击“悔棋” move = Move(is_regret=True) if game.is_valid_move(move): game = game.apply_move(move) draw_chessBoard(screen) draw_stones(game.board, screen) elif is_in_area(event.pos, pos_pass_button): # 点击“过棋” move = Move(is_pass=True) game = game.apply_move(move) elif is_in_area(event.pos, pos_resign_button): # 点击“认输” move = Move(is_resign=True) game = game.apply_move(move) # print(game.is_over()) show_winner(game.next_player, screen) else: show_winner(game.next_player, screen) elif game_mode == 2: if is_in_area(event.pos, pos_restart_button): # 点击“重新开始” move = Move(is_restart=True) game = game.apply_move(move) draw_chessBoard(screen) elif is_in_area(event.pos, pos_turn_back_button): # 点击返回按钮 move = Move(is_turn_back=True) game = game.apply_move(move) select_gameMode(screen) if not game.is_over(): # 当前对局未结束时以下功能可用 if game.next_player == Player.black: if is_in_area(event.pos, pos_board): # 合法落子 point = Human.go_strategy(event.pos) # print(point) move = Move.play(point) if game.is_valid_move(move): game = game.apply_move(move) draw_chessBoard(screen) draw_stones(game.board, screen) elif is_in_area(event.pos, pos_regret_button): # 点击“悔棋” if game.next_player == Player.black: move = Move(is_regret=True) if game.is_valid_move(move): game = game.apply_move(move) draw_chessBoard(screen) draw_stones(game.board, screen) elif is_in_area(event.pos, pos_pass_button): # 点击“过棋” move = Move(is_pass=True) game = game.apply_move(move) elif is_in_area(event.pos, pos_resign_button): # 点击“认输” move = Move(is_resign=True) game = game.apply_move(move) # print(game.is_over()) show_winner(game.next_player, screen) else: pass else: show_winner(game.next_player, screen) elif game_mode == 3: pass