Exemplo n.º 1
0
    def test_diagonal_win(self):

        tictactoe = TicTacToe()

        tictactoe.move((0, 0))
        tictactoe.move((0, 1))
        tictactoe.move((1, 1))
        tictactoe.move((1, 0))
        tictactoe.move((2, 2))

        self.assertEqual(tictactoe.game_has_ended(), (True, "x"))
Exemplo n.º 2
0
    def emit_move(self):

        valid_moves = self.playing_board.get_valid_moves()

        if uniform(0, 1) <= self.exploration_rate:
            chosen_move = choice(valid_moves)
            visited = TicTacToe(self.playing_board.get_state())
            visited.move(chosen_move)
            next_state = visited.get_state()
            self.visited_states.append(next_state)
            if next_state not in self.moves_dict.keys():
                self.moves_dict.update({next_state: 0.5})
            return chosen_move
        else:
            considering_moves = []
            for move in valid_moves:
                next_board = TicTacToe(self.playing_board.get_state())
                next_board.move(move)
                next_state = next_board.get_state()

                if next_board.game_has_ended():
                    self.moves_dict.update({next_state: 1})
                    considering_moves.append({move: 1.0})
                elif next_state in self.moves_dict.keys():
                    considering_moves.append({move: self.moves_dict[next_state]})
                else:
                    self.moves_dict.update({next_state: 0.5})
                    considering_moves.append({move: 0.5})

            sorted_list = list(
                reversed(
                    sorted(considering_moves, key=lambda dict: list(dict.values())[0])
                )
            )
            chosen_move = list(sorted_list[0].keys())[0]
            next_board = TicTacToe(self.playing_board.get_state())
            next_board.move(chosen_move)
            next_state = next_board.get_state()
            self.visited_states.append(next_state)

            return chosen_move
Exemplo n.º 3
0
        with open(args.file2, 'rb') as fp:
            state_dict2 = pickle.load(fp)
    else:
        state_dict2 = {}

for i in range(args.n):

    tictactoe = TicTacToe()

    Player1 = RLTicTacToeAgent(tictactoe, state_dict1)
    Player2 = RandomTicTacToeAgent(tictactoe)

    tictactoe.add_agent(Player1, 'x')
    tictactoe.add_agent(Player2, 'o')

    while not tictactoe.game_has_ended()[0]:
        next_move1 = Player1.emit_move()
        tictactoe.move(next_move1)
        if not tictactoe.game_has_ended()[0]:
            next_move2 = Player2.emit_move()
            tictactoe.move(next_move2)

    if args.verbose:
        who_won = tictactoe.game_has_ended()[1]
        if who_won == 'draw':
            print("Draw!")
        elif who_won == 'x':
            print("Won!")
        elif who_won == 'o':
            print('Lost!')
        else:
Exemplo n.º 4
0
else:
    state_dict = {}

tictactoe = TicTacToe()

Player1 = HumanTicTacToeAgent(tictactoe)
Player2 = RLTicTacToeAgent(tictactoe, state_dict)

while True:
    print(tictactoe)
    print(tictactoe.get_state())
    print("Player1: ")
    player1_move = Player1.emit_move()
    tictactoe.move(player1_move)

    has_ended, player = tictactoe.game_has_ended()
    if has_ended:
        if player == 'draw':
            print('Draw!')
        else:
            print('Player ' + player + ' won!')
        break

    print(tictactoe)
    print("Player2: ")
    player2_move = Player2.emit_move()
    tictactoe.move(player2_move)

    has_ended, player = tictactoe.game_has_ended()
    if has_ended:
        if player == 'draw':