示例#1
0
 def test_legal_actions_raises_exception_on_terminal_input_state(
         self, mocker):
     mock_game = mocker.MagicMock()
     mock_game.is_terminal = mocker.MagicMock(return_value=True)
     mock_state = mocker.MagicMock()
     with pytest.raises(ValueError) as exception_info:
         NoughtsAndCrosses.legal_actions(mock_game, mock_state)
     assert str(
         exception_info.value) == ("Legal actions can not be computed"
                                   " for a terminal state.")
示例#2
0
 def test_generating_a_dict_of_all_possible_next_states(
         self, size, actions_to_binary, state, player, expected_states,
         mocker):
     # TODO: need to split this into two tests: one testing the _next_state function and one testing legal actions
     rows, columns = size
     mock_game = mocker.MagicMock(rows=rows,
                                  columns=columns,
                                  _actions_to_binary=actions_to_binary)
     mock_game.is_terminal = mocker.MagicMock(return_value=False)
     mock_game.current_player = mocker.MagicMock(return_value=player)
     assert NoughtsAndCrosses.legal_actions(mock_game,
                                            state) == expected_states
示例#3
0
def test_random_noughts_and_crosses_player_gives_equal_action_probabilities():
    nac = NoughtsAndCrosses()
    player = RandomPlayer(game=nac)
    action, action_probs = player.choose_action(nac.initial_state,
                                                return_probabilities=True)

    next_states = nac.legal_actions(nac.initial_state)
    expected_action_probs = {
        action: 1 / len(next_states)
        for action in next_states.keys()
    }

    for action in expected_action_probs.keys():
        np.testing.assert_almost_equal(action_probs[action],
                                       expected_action_probs[action])
示例#4
0
    nac = NoughtsAndCrosses()
    evaluator = create_trivial_estimator(nac.legal_actions)

    state = nac.INITIAL_STATE
    computer_player_no = np.random.choice([1, 2])
    computer_player = MCTSPlayer(nac,
                                 evaluator,
                                 mcts_iters=2000,
                                 c_puct=0.5,
                                 tau=0.01)
    human_player_no = 1 if computer_player_no == 2 else 2
    print("You are player: {}".format(human_player_no))
    while not nac.is_terminal(state):
        player_no = nac.current_player(state)
        next_states = nac.legal_actions(state)
        if player_no == computer_player_no:
            action = computer_player.choose_action(state)
            computer_player.update(action)
            print("Taking action: {}".format(action))
        else:
            action = None
            while action not in next_states:
                action_ix = int(
                    input("Your move (0-8 reading "
                          "across the board): "))
                if 0 <= action_ix <= 8:
                    action = nac.ACTION_SPACE[action_ix]
                    computer_player.update(action)
        state = next_states[action]