Example #1
0
 def test_generate_next_states_adds_current_player_to_each_open_spot(self):
     states = StateGenerator.generate_next_states(
         State(numpy.array([[0, 1, 0], [1, 2, 0], [2, 0, 1]]), 2))
     self.assertCountEqual(states, [
         State(numpy.array([[2, 1, 0], [1, 2, 0], [2, 0, 1]]), 1),
         State(numpy.array([[0, 1, 2], [1, 2, 0], [2, 0, 1]]), 1),
         State(numpy.array([[0, 1, 0], [1, 2, 2], [2, 0, 1]]), 1),
         State(numpy.array([[0, 1, 0], [1, 2, 0], [2, 2, 1]]), 1)
     ])
Example #2
0
 def test_returns_ten_states_when_depth_is_one(self):
     states = StateGenerator.generate(1)
     expected_states = [
         State(numpy.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]]), 1),
         State(numpy.array([[1, 0, 0], [0, 0, 0], [0, 0, 0]]), 2),
         State(numpy.array([[0, 1, 0], [0, 0, 0], [0, 0, 0]]), 2),
         State(numpy.array([[0, 0, 1], [0, 0, 0], [0, 0, 0]]), 2),
         State(numpy.array([[0, 0, 0], [1, 0, 0], [0, 0, 0]]), 2),
         State(numpy.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]]), 2),
         State(numpy.array([[0, 0, 0], [0, 0, 1], [0, 0, 0]]), 2),
         State(numpy.array([[0, 0, 0], [0, 0, 0], [1, 0, 0]]), 2),
         State(numpy.array([[0, 0, 0], [0, 0, 0], [0, 1, 0]]), 2),
         State(numpy.array([[0, 0, 0], [0, 0, 0], [0, 0, 1]]), 2)
     ]
     self.assertCountEqual(expected_states, states)
Example #3
0
def play(players):
    state = State()
    state.revise()
    print(state)
    while not state.terminal:
        player = players[state.next_to_play]
        if not player.auto:
            a = int(input('%s move? ' % player.symbol))
        else:
            a = state.action()
            print('%s action: %d\n' % (player.symbol, a))
        state = state.apply_action(a)
        state.revise()
        print(state)
    if state.winner is not None:
        print('%s wins.' % players[state.winner].symbol)
    else:
        print('Draw.')
    return state.winner
Example #4
0
 def test_generate_from_states_filters_out_terminal_states_from_generating_more_states(
         self):
     input_states = [
         State(numpy.array([[1, 1, 1], [0, 0, 0], [0, 0, 0]]), 2),
         State(numpy.array([[1, 0, 0], [0, 0, 0], [0, 0, 0]]), 2)
     ]
     output_states = StateGenerator.generate_from_states(input_states, 1)
     expected_states = [
         State(numpy.array([[1, 1, 1], [0, 0, 0], [0, 0, 0]]), 2),
         State(numpy.array([[1, 0, 0], [0, 0, 0], [0, 0, 0]]), 2),
         State(numpy.array([[1, 2, 0], [0, 0, 0], [0, 0, 0]]), 1),
         State(numpy.array([[1, 0, 2], [0, 0, 0], [0, 0, 0]]), 1),
         State(numpy.array([[1, 0, 0], [2, 0, 0], [0, 0, 0]]), 1),
         State(numpy.array([[1, 0, 0], [0, 2, 0], [0, 0, 0]]), 1),
         State(numpy.array([[1, 0, 0], [0, 0, 2], [0, 0, 0]]), 1),
         State(numpy.array([[1, 0, 0], [0, 0, 0], [2, 0, 0]]), 1),
         State(numpy.array([[1, 0, 0], [0, 0, 0], [0, 2, 0]]), 1),
         State(numpy.array([[1, 0, 0], [0, 0, 0], [0, 0, 2]]), 1),
     ]
     self.assertCountEqual(output_states, expected_states)
Example #5
0
from tictactoe.state import State


def print_state(state):
    print(state.board[0])
    print(state.board[1])
    print(state.board[2])


print('Welcome to tic-tac-toe!')

state = State()
print_state(state)
 def test_returns_zero_if_no_winner(self):
     self.assertEqual(
         0,
         State(numpy.array([[1, 1, 2], [2, 2, 1], [1, 1, 2]]), 1).winner())
 def test_take_turn_switches_from_player_two_to_player_one(self):
     state = State(numpy.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]]), 2)
     state.take_turn(1, 2)
     self.assertEqual(state.current_player, 1)
 def generate(depth):
     initial_states = [State()]
     return StateGenerator.generate_from_states(initial_states, depth)
 def test_returns_one_if_third_column_all_belong_to_player_one(self):
     self.assertEqual(
         1,
         State(numpy.array([[0, 0, 1], [0, 0, 1], [0, 0, 1]]), 1).winner())
 def test_defaults_state_and_turn(self):
     self.assertTrue(
         numpy.array_equal(State().board,
                           numpy.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]])))
     self.assertEqual(State().current_player, 1)
 def test_returns_zero_if_only_first_position_belongs_to_player(self):
     self.assertEqual(
         0,
         State(numpy.array([[1, 0, 0], [0, 0, 0], [0, 0, 0]]), 1).winner())
 def test_winner_returns_one_if_first_row_all_belong_to_player_one(self):
     self.assertEqual(
         1,
         State(numpy.array([[1, 1, 1], [0, 0, 0], [0, 0, 0]]), 1).winner())
 def test_winner_returns_zero_for_empty_game(self):
     self.assertEqual(
         0,
         State(numpy.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]]), 1).winner())
 def test_equals_returns_false_if_non_state_is_passed(self):
     state = State(numpy.array([0, 0, 0]), 1)
     self.assertFalse(state == 3)
 def test_two_states_are_equal_if_they_have_the_same_board_and_current_player(
         self):
     first_state = State(numpy.array([[2, 0, 1], [0, 1, 0], [0, 0, 0]]), 2)
     second_state = State(numpy.array([[2, 0, 1], [0, 1, 0], [0, 0, 0]]), 2)
     self.assertEqual(first_state, second_state)
Example #16
0
 def test_returns_single_state_when_zero_turns_have_been_taken(self):
     states = StateGenerator.generate(0)
     self.assertCountEqual(
         [State(numpy.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]]), 1)], states)
 def assert_converts_correctly(self, state, player, expected):
     result = Teacher.convert_state_to_inputs(State(state, player))
     self.assertTrue(numpy.array_equal(result, expected),
                     "expected: {}, got: {}".format(expected, result))
 def test_returns_one_if_second_diagonal_all_belong_to_player_one(self):
     self.assertEqual(
         1,
         State(numpy.array([[0, 0, 1], [0, 1, 0], [1, 0, 0]]), 1).winner())
 def test_returns_two_if_second_diagonal_all_belong_to_player_two(self):
     self.assertEqual(
         2,
         State(numpy.array([[0, 0, 2], [0, 2, 0], [2, 0, 0]]), 1).winner())
 def test_take_turn_adds_current_player_to_specified_position(self):
     state = State(numpy.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]]), 1)
     state.take_turn(1, 2)
     self.assertTrue(
         numpy.array_equal(state.board,
                           numpy.array([[0, 0, 0], [0, 0, 1], [0, 0, 0]])))