コード例 #1
0
    def generate_successors(
        self,
        state: TwoPlayerGameState,
    ) -> List[TwoPlayerGameState]:
        """Generate the list of successors of a game state."""
        successors = []
        board = state.board
        moves = self._get_valid_moves(board, state.next_player.label)

        for move in moves:
            board_successor = copy.deepcopy(state.board)
            assert isinstance(state.next_player, Player)
            # show the move on the board
            board_successor[move] = state.next_player.label
            # flip enemy
            for enemy in self._enemy_captured_by_move(board, move, state.next_player.label):
                board_successor[enemy] = state.next_player.label
            move_code = self._matrix_to_display_coordinates(move)
            successor = state.generate_successor(
                board_successor,
                move_code,
            )

            successors.append(successor)

        if not successors:
            board_successor = copy.deepcopy(state.board)
            move_code = None
            no_movement = state.generate_successor(
                board_successor,
                move_code,
            )
            successors = [ no_movement ]

        return successors
コード例 #2
0
ファイル: tictactoe.py プロジェクト: Fgp910/IA_2021
    def generate_successors(
        self,
        state: TwoPlayerGameState,
    ) -> List[TwoPlayerGameState]:
        """Generate the list of successors of a game state."""
        successors = []

        n_rows, n_columns = np.shape(state.board)
        for i in range(n_rows):
            for j in range(n_columns):
                if (state.board[i, j] == 0):
                    # Prevent modification of the board
                    board_successor = copy.deepcopy(state.board)
                    assert isinstance(state.next_player, Player)
                    board_successor[i, j] = state.next_player.label
                    move_code = self._matrix_to_display_coordinates(i, j)
                    successor = state.generate_successor(
                        board_successor,
                        move_code,
                    )

                    successors.append(successor)

        return successors