Пример #1
0
    def run_game():
        gm = game.Game()
        boardState = Gameplay.board_state_from_board(gm.board)
        while (not gm.is_over()):
            Gameplay.show_board(boardState)
            print("Current Player: " +
                  ("White" if gm.whose_turn() == 1 else "Black"))
            possible_moves_coord = list(
                map(lambda x: Gameplay.move_from_pos(x, gm),
                    gm.get_possible_moves()))
            print("Valid Moves: ")
            for i in range(len(possible_moves_coord)):
                print(str(i + 1) + ": " + (str(possible_moves_coord[i])))

            for bd_st in Gameplay.board_states_from_possible_moves(gm.board):
                Gameplay.show_board(bd_st)

            move_idx = -1
            while move_idx not in range(len(possible_moves_coord)):
                usr_input = input("Pick a move: ")
                move_idx = -1 if (usr_input == '') else (int(usr_input) - 1)
                if move_idx not in range(len(possible_moves_coord)):
                    print("Illegal move")

            move = gm.get_possible_moves()[move_idx]
            if (move in gm.board.get_possible_capture_moves()):
                print('capture move!')

            piece_was_king = gm.board.searcher.get_piece_by_position(
                move[0]).king
            boardState = Gameplay.make_move(gm, move)

            if (not piece_was_king
                ) and gm.board.searcher.get_piece_by_position(move[1]).king:
                print('king move!')

        print("Game Over! ")
        if gm.move_limit_reached():
            print("It's a tie!!")
        else:
            print("Winner is: " +
                  ("White" if gm.get_winner() == 1 else "Black"))
Пример #2
0
    def run_agent_duel(agt1, agt2, verbose=False):
        players = [agt1, agt2]
        gm = game.Game()
        boardState = Gameplay.board_state_from_board(gm.board)
        random.shuffle(players)
        while (not gm.is_over()):
            if verbose:
                Gameplay.show_board(boardState)
                print("Current Player: " +
                      ("White" if gm.whose_turn() == 1 else "Black"))
                possible_moves_coord = list(
                    map(lambda x: Gameplay.move_from_pos(x, gm),
                        gm.get_possible_moves()))
                print("Valid Moves: ")
                for i in range(len(possible_moves_coord)):
                    print(str(i + 1) + ": " + (str(possible_moves_coord[i])))

            curr_agt = players[gm.whose_turn() - 1]
            move, q_val = Gameplay.get_QAgent_move_pp(curr_agt, gm)

            if verbose:
                print("Agent {} picks {}: ".format(curr_agt.name, move + 1))
                print("Agent {} Q-value {}: ".format(curr_agt.name, q_val))

            boardState = Gameplay.make_move(gm, gm.get_possible_moves()[move])

        if verbose:
            print("Game Over! ")

        if gm.move_limit_reached():
            result = 'tie'
            if verbose:
                print("It's a tie!!")
        else:
            result = players[gm.get_winner() - 1].name
            if verbose:
                print("Winner is: {}".format(players[gm.get_winner() -
                                                     1].name))

        return result
Пример #3
0
    def run_game_with_agent(agent):
        players = [HUMAN, AGENT]
        gm = game.Game()
        boardState = Gameplay.board_state_from_board(gm.board)
        random.shuffle(players)
        while (not gm.is_over()):
            Gameplay.show_board(boardState)
            print("Current Player: " +
                  ("White" if gm.whose_turn() == 1 else "Black"))
            possible_moves_coord = list(
                map(lambda x: Gameplay.move_from_pos(x, gm),
                    gm.get_possible_moves()))
            print("Valid Moves: ")
            for i in range(len(possible_moves_coord)):
                print(str(i + 1) + ": " + (str(possible_moves_coord[i])))

            if (players[gm.whose_turn() - 1] == HUMAN):
                move = -1
                while move not in range(len(possible_moves_coord)):
                    usr_input = input("Pick a move: ")
                    move = -1 if (usr_input == '') else (int(usr_input) - 1)
                    if move not in range(len(possible_moves_coord)):
                        print("Illegal move")
                print("Human picks {}: ".format(move))
            else:
                move, q_val = Gameplay.get_QAgent_move_pp(agent, gm)
                print("Agent {} picks {}: ".format(agent.name, move + 1))
                print("Agent {} Q-value {}: ".format(agent.name, q_val))

            boardState = Gameplay.make_move(gm, gm.get_possible_moves()[move])

        print("Game Over! ")
        if gm.move_limit_reached():
            print("It's a tie!!")
        else:
            print("Winner is: " +
                  ("White" if gm.get_winner() == 1 else "Black"))
Пример #4
0
    def auto_play(self, n_episodes):
        plt.ion()
        plt.xlabel('Episodes')
        plt.ylabel('{} mean error'.format(self.name))
        x, y = [], []
        line, = plt.plot(x, y)
        plt.xlim(0, n_episodes)
        plt.ylim(0, config.PLOT_Y_LIM)

        for i in range(n_episodes):
            print("Episode {}".format(i))
            turns_hist = {
                1: [],
                2: []
            }
            gm = game.Game()
            boardState = Gameplay.board_state_from_board(gm.board)

            while (not gm.is_over()):
                player = gm.whose_turn()

                possible_board_states = Gameplay.board_states_from_possible_moves(gm.board)
                move_idx, q_val = Gameplay.get_QAgent_move_pp(self, gm)

                draw_counter = gm.moves_since_last_capture

                if (player == 2):
                    boardState = Gameplay.invert_board(boardState)
                    possible_board_states = np.array(
                        list(map(lambda x: Gameplay.invert_board(x), possible_board_states)))

                # Updating previous history
                if len(turns_hist[player]) > 0:
                    turns_hist[player][-1]['next_board_state'] = boardState
                    turns_hist[player][-1]['next_draw_counter'] = draw_counter
                    turns_hist[player][-1]['next_possible_board_states'] = possible_board_states

                move = gm.get_possible_moves()[move_idx]

                reward = 0
                if (move in gm.board.get_possible_capture_moves()):
                    reward += config.CAPTURE_REWARD

                piece_was_king = gm.board.searcher.get_piece_by_position(move[0]).king
                new_boardState = Gameplay.make_move(gm, move)

                if (not piece_was_king) and gm.board.searcher.get_piece_by_position(move[1]).king:
                    reward += config.KING_REWARD

                if len(turns_hist[Gameplay.get_other_player(player)]) > 0:
                    turns_hist[Gameplay.get_other_player(player)][-1]['reward'] -= reward

                # New history
                turns_hist[player].append({
                    'board_state': boardState,
                    'draw_counter': draw_counter,
                    'board_state_action': new_boardState,
                    'reward': reward,
                    'next_board_state': None,
                    'next_draw_counter': None,
                    'next_possible_board_states': None,
                    'done': False
                })
                if (player == 2):
                    turns_hist[player][-1]['board_state_action'] = Gameplay.invert_board(new_boardState)

                boardState = new_boardState

            print("Game Over! ")
            if gm.move_limit_reached():
                print("It's a tie!!")
                for j in range(2):
                    turns_hist[j + 1][-1]['reward'] += config.DRAW_REWARD
                    turns_hist[j + 1][-1]['done'] = True
            else:
                print("Winner is: {}".format(gm.get_winner()))
                turns_hist[gm.get_winner()][-1]['reward'] += config.WIN_REWARD
                turns_hist[gm.get_winner()][-1]['done'] = True
                turns_hist[Gameplay.get_other_player(gm.get_winner())][-1]['reward'] -= config.WIN_REWARD
                turns_hist[Gameplay.get_other_player(gm.get_winner())][-1]['done'] = True

            for k, v in turns_hist.items():
                print("Reward sum for {}: {}".format(k, sum(list(map(lambda x: x['reward'], v)))))

            for k, v in turns_hist.items():
                for turn_hist in v:
                    self.remember(turn_hist['board_state'], turn_hist['draw_counter'],
                                  turn_hist['board_state_action'], turn_hist['reward'],
                                  turn_hist['next_board_state'], turn_hist['next_draw_counter'],
                                  turn_hist['next_possible_board_states'], turn_hist['done'])

            if (len(self.memory) > self.replay_batch_size):
                self.replay_memory()
                y.append(self.loss_mean)
                x.append(i)
                line.set_data(x, y)
                plt.draw()
                plt.pause(0.000000001)

        return self