예제 #1
0
    def get_moves_for_player(self, board, piece_set):
        self.checkers_board = CheckersBoard(board)
        self.piece = self.checkers_board.piece

        # self.checkers_board.initialize_board(board)
        move_list = []
        positions = self.get_piece_positions_of_current_player(piece_set)
        for position in positions:
            move_list.append(self.get_capturing_move_list(readable_position(position)))
            move_list.append(self.get_move_list(readable_position(position)))

        move_list = [item for sublist in move_list for item in sublist]
        capturing_move_list = [move for move in move_list if 'x' in move]
        return capturing_move_list or move_list
예제 #2
0
def find_next_move(board, turn):
    board = CheckersBoard(board)

    root_node = Node()
    root_node.set_state(board.wooden_board, turn)
    tree = Tree(root_node)

    # Tree search loop. Fixed for now
    for i in range(100):
        promising_node = select_promising_node(root_node)
        if not promising_node.state.game_over():
            expand_node(promising_node)

        node_to_explore = copy.deepcopy(promising_node)
        # node_to_explore = promising_node

        if promising_node.children > 0:
            node_to_explore = promising_node.get_random_child()

        result = simulate_random_playout(node_to_explore)
        back_propagate(node_to_explore, result)

    winner_node = root_node.get_child_with_maximum_score()
    tree.set_root(winner_node)

    return winner_node.state.board
예제 #3
0
def generate_data(game, winner):
    board = CheckersBoard()
    movement = Movement()
    board.set_pieces_on_board_for_a_new_game()
    game_states = ''

    for move in game[:-1]:
        board = movement.make_move(board, move)
        board_state = board.get_wooden_board()
        reduced_board_state = reduce_board(board_state)
        reduced_board_state = [piece[item] for item in reduced_board_state]
        game_states += ''.join(str(x) for x in reduced_board_state) + '\n'

    timestamp = str(int(time.time() * 1000000))
    file = open('games/' + winner + '/' + winner + timestamp, 'w+')
    file.write(game_states)
    file.close()
예제 #4
0
class Game():
    def __init__(self):
        self.board = CheckersBoard()
        self.board.set_pieces_on_board_for_a_new_game()
        self.movement = Movement()
        self.move_list_generator = MoveListGenerator()
        self.players = cycle([piece_set_two, piece_set_one])

    def start(self):
        return self.begin_game_loop()

    def start_random(self):
        return self.begin_game_loop(True)

    def begin_game_loop(self, is_random=False):
        game_moves = []
        while True:
            current_piece_set = next(self.players)
            print_board(self.board.wooden_board)
            moves = self.move_list_generator.get_moves_for_player(
                self.board.wooden_board, current_piece_set)
            if moves:
                selected_move = self.select_move(moves, is_random)
                self.board = self.movement.make_move(self.board,
                                                     moves[selected_move])
                game_moves.append(moves[selected_move])
            else:
                winner = next(self.players)
                print winner[0] + " wins"
                break
        return game_moves

    def select_move(self, moves, is_random):
        if is_random:
            choice = random.choice(range(len(moves)))
        else:
            self.print_available_moves(moves)
            choice = input('Select a move: ')
        return choice

    def print_available_moves(moves):
        for (index, move) in enumerate(moves):
            print index, move
예제 #5
0
 def __init__(self):
     self.board = CheckersBoard()
     self.board.set_pieces_on_board_for_a_new_game()
     self.movement = Movement()
     self.move_list_generator = MoveListGenerator()
     self.players = cycle([piece_set_two, piece_set_one])
예제 #6
0
        node_to_explore = copy.deepcopy(promising_node)
        # node_to_explore = promising_node

        if promising_node.children > 0:
            node_to_explore = promising_node.get_random_child()

        result = simulate_random_playout(node_to_explore)
        back_propagate(node_to_explore, result)

    winner_node = root_node.get_child_with_maximum_score()
    tree.set_root(winner_node)

    return winner_node.state.board


board = CheckersBoard()
board.set_pieces_on_board_for_a_new_game()
movement = Movement()
move_list_generator = MoveListGenerator()

available_moves = move_list_generator.get_moves_for_player(
    board.wooden_board, piece_set['b'])
choice = random.choice(range(len(available_moves)))
print_board(board.wooden_board)
print available_moves
print choice
board = movement.make_move(board, available_moves[choice])

print_board(board.wooden_board)

find_next_move(board.wooden_board, 'w')
예제 #7
0
class MoveListGenerator:

    def get_moves_for_player(self, board, piece_set):
        self.checkers_board = CheckersBoard(board)
        self.piece = self.checkers_board.piece

        # self.checkers_board.initialize_board(board)
        move_list = []
        positions = self.get_piece_positions_of_current_player(piece_set)
        for position in positions:
            move_list.append(self.get_capturing_move_list(readable_position(position)))
            move_list.append(self.get_move_list(readable_position(position)))

        move_list = [item for sublist in move_list for item in sublist]
        capturing_move_list = [move for move in move_list if 'x' in move]
        return capturing_move_list or move_list

    # Private Methods

    def get_move_list(self, position):
        movable_neighbours = self.get_movable_neighbour_cells(position_parser(position))
        move_list = []
        for neighbour in movable_neighbours:
            if self.checkers_board.get_piece(neighbour) == piece['empty']:
                move = position + '-' + readable_position(neighbour)
                move_list.append(move)
        return move_list

    def get_capturing_move_list(self, position):
        self.capture_move_list = []
        [row, column] = position_parser(position)
        piece = self.checkers_board.get_piece([row, column])
        self.get_captures(position, [row, column], piece)
        return self.capture_move_list

    def get_movable_neighbour_cells(self, position):
        [row, column] = position
        piece = self.checkers_board.get_piece(position)
        self.neighbours = []

        # Get neighbours by adding offset. King pieces have two offsets [1, -1]
        for offset in move_offset[piece]:
            self.add_neighbour([row + offset, column + 1])
            self.add_neighbour([row + offset, column - 1])

        return self.neighbours

    def add_neighbour(self, neighbour):
        if self.is_valid(neighbour):
            self.neighbours.append(neighbour)

    def is_valid(self, position):
        [row, column] = position
        return 0 <= row <= 7 and 0 <= column <= 7

    def get_captures(self, move, position, piece):
        [row, column] = position
        capture_neighbours = self.get_capture_neighbour_cells(position, piece)

        for neighbour in capture_neighbours:
            current_position = readable_position(neighbour)
            if current_position in move:
                self.capture_move_list.append(move)
            else:
                move += "x" + current_position
                self.get_captures(move, neighbour, piece)

        if capture_neighbours == []:
            if len(move) != 2:
                self.capture_move_list.append(move)

    def get_capture_neighbour_cells(self, position, piece):
        [row, column] = position
        self.capture_neighbours = []

        for offset in move_offset[piece]:
            self.add_capture_neighbour([row, column], [row + offset, column + 1], [row + 2 * offset, column + 2], piece)
            self.add_capture_neighbour([row, column], [row + offset, column - 1], [row + 2 * offset, column - 2], piece)

        return self.capture_neighbours

    # TODO: Refactoring
    def add_capture_neighbour(self, initial_position, opponent_position, final_position, piece):
        if self.is_valid(initial_position) and self.is_valid(opponent_position) and self.is_valid(final_position):
            opponent_piece = self.checkers_board.get_piece(opponent_position)
            if self.is_opponent(piece, opponent_piece):
                if self.checkers_board.get_piece(final_position) == self.piece['empty']:
                    self.capture_neighbours.append(final_position)

    def is_opponent(self, piece, opponent_piece):
        return (piece in piece_set_one and opponent_piece in piece_set_two) or (piece in piece_set_two and opponent_piece in piece_set_one)

    def get_piece_positions_of_current_player(self, piece_set):
        positions = []
        for i in range(8):
            for j in range(8):
                if self.checkers_board.get_piece([i, j]) in piece_set:
                    positions.append([i, j])
        return positions