Exemple #1
0
    def test_initialise(self):
        game_one = TicTacToeGame()

        # Assertions
        self.assertEquals(game_one.BOARD.sum(), 0)
        self.assertEquals(game_one.WINNER, None)
        self.assertEquals(game_one.BOARD_COLS, 3)
        self.assertEquals(game_one.BOARD_ROWS, 3)
    def play_game(self):
        in_progress = True
        active_gamer = self.AGENT1 if random.random() < 0.5 else self.AGENT2
        game = TicTacToeGame(columns=self.DEFAULT_BOARD[1], rows=self.DEFAULT_BOARD[0])

        # While there are available states continue playing!
        while in_progress:
            # Get the current State
            game_state = game.get_state()

            # Get the eligible Actions
            eligible_actions = self.get_eligible_indexes(array=game.BOARD)

            # Let the Agent take an action based on the state
            action = active_gamer.take_action(game_state, eligible_actions[0])
            active_gamer.append(state=game_state, action=action, reward=game.REWARD_IN_PROGRESS)

            # Update the Board with the Desired Action
            game.update_board(action=action, value=active_gamer.ACTION)

            # Check if the Game is complete...
            if game.is_end_state():
                self.update_reward(game=game, action=action)
                in_progress = False
                self.SCORES[game.WINNER] += 1

            # Switch to the Other Player
            active_gamer = self.switch_role(id=active_gamer.ID)
Exemple #3
0
    def test_check_row(self):
        game_three = TicTacToeGame()
        game_three.BOARD[0, 0] = 1

        # Should return False
        assert game_three.get_state() == '[1, 0, 0, 0, 0, 0, 0, 0, 0]'
        assert not game_three.is_end_state()

        game_three.BOARD[0, 1] = 1
        game_three.BOARD[0, 2] = 1

        # Assertions
        assert game_three.is_end_state()
        assert game_three.WINNER == 1
Exemple #4
0
    def test_check_opposite_diagonal(self):
        game_five = TicTacToeGame()
        game_five.BOARD[0, 2] = 1

        # Should return False
        assert not game_five.is_end_state()

        game_five.BOARD[1, 1] = 1
        game_five.BOARD[2, 0] = 1

        # Assertions
        assert game_five.is_end_state()
        assert game_five.WINNER == 1
Exemple #5
0
    def test_check_column(self):
        game_four = TicTacToeGame()
        game_four.BOARD[0, 0] = 1

        # Should return False
        assert not game_four.is_end_state()

        game_four.BOARD[1, 0] = 1
        game_four.BOARD[2, 0] = 1

        # Assertions
        assert game_four.is_end_state()
        assert game_four.WINNER == 1
                                agent_two=env2.AGENT2,
                                trials=n3)
    env3.train()

    # Writing a Pickle
    with open('RLBook/Chapter1/AGENT.pickle', 'wb') as handle:
        pickle.dump(env3.AGENT2, handle, protocol=pickle.HIGHEST_PROTOCOL)

    # Read in said Pickle File
    with open('RLBook/Chapter1/AGENT.pickle', 'rb') as handle:
        env3.AGENT2 = pickle.load(handle)

    # Human Interaction
    in_progress, active_gamer = True, env3.AGENT2
    active_gamer.EPSILON = 0  # Agent should make the decisions entirely on his own
    game = TicTacToeGame(columns=3, rows=3)

    while in_progress:  # Step through as opposed to using the Loop
        # Invoke the Agent
        game_state = game.get_state()
        eligible_actions = env2.get_eligible_indexes(array=game.BOARD)
        action = active_gamer.take_action(game_state, eligible_actions[0])
        game.update_board(action=action, value=active_gamer.ACTION)

        # Check if the End of the Game has been found?
        in_progress = not game.is_end_state()
        print(game.WINNER)

        # Show the Current State
        game.display()
Exemple #7
0
    def test_check_draw(self):
        game_six = TicTacToeGame()
        game_six.BOARD[0, 0] = 1
        game_six.BOARD[1, 0] = 1
        game_six.BOARD[2, 0] = -1
        game_six.BOARD[0, 1] = -1
        game_six.BOARD[1, 1] = -1
        game_six.BOARD[2, 1] = 1
        game_six.BOARD[0, 2] = 1
        game_six.BOARD[1, 2] = -1
        game_six.BOARD[2, 2] = -1

        # Assertions
        assert game_six.is_end_state()
        assert game_six.WINNER == 0
Exemple #8
0
    def test_getting_state(self):
        game_two = TicTacToeGame()
        game_two.BOARD[0, 0] = 1

        assert game_two.get_state() == '[1, 0, 0, 0, 0, 0, 0, 0, 0]'