Esempio n. 1
0
 def test_utility_raises_exception_on_non_terminal_input_state(
         self, size, state, mocker):
     rows, columns = size
     mock_is_terminal = mocker.MagicMock(return_value=False)
     mock_game = mocker.MagicMock(rows=rows,
                                  columns=columns,
                                  is_terminal=mock_is_terminal)
     with pytest.raises(ValueError) as exception_info:
         NoughtsAndCrosses.utility(mock_game, state)
     assert str(exception_info.value) == ("Utility can not be calculated "
                                          "for a non-terminal state.")
     mock_is_terminal.assert_called_once_with(state)
Esempio n. 2
0
    def test_utility_function_returns_correct_outcomes(self, size,
                                                       win_bitmasks, state,
                                                       outcome, mocker):
        rows, columns = size
        mock_game = mocker.MagicMock(rows=rows,
                                     columns=columns,
                                     _win_bitmasks=win_bitmasks)

        assert NoughtsAndCrosses.utility(mock_game, state) == outcome
Esempio n. 3
0
    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]

        nac.display(state)
        print("\n")

    # The state is terminal, so let's see who won.
    utility = nac.utility(state)
    if utility[human_player_no] == 1:
        print("You win!")
    elif utility[human_player_no] == 0:
        print("It's a draw!")
    else:
        print("You lose!")