def test_two_by_two(player): state = GameState.root(2) assert str(state) == (''' A B 1 . . O 2 . . O @ @''') assert player.value(state) == 1 state.play(0) assert str(state) == (''' A B 1 O . O 2 . . O @ @''') assert player.value(state) == 1 state.undo() state.play(1) assert str(state) == (''' A B 1 . . O 2 O . O @ @''') assert player.value(state) == -1
def test_two_by_two_action(player): state = GameState.root(2) assert str(state) == ( ''' A B 1 . . O 2 . . O @ @''') assert player.select_action(state) == 1 state.play(0) assert str(state) == ( ''' A B 1 O . O 2 . . O @ @''') assert player.select_action(state) == 2 state.undo() state.play(1) assert str(state) == ( ''' A B 1 . . O 2 O . O @ @''') assert player.select_action(state) == 0
def test_with_action_applied(): patient = GameState.root(5) assert patient.player_to_act() == color_to_player(COLOR_WHITE) for _ in patient.with_action_applied(0): assert patient.player_to_act() == color_to_player(COLOR_WHITE) assert str(patient) == ( ''' A B C D E 1 O . . . . O 2 . . . . . O 3 . . . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''' ) assert patient.player_to_act() == color_to_player(COLOR_WHITE) assert str(patient) == ( ''' A B C D E 1 . . . . . O 2 . . . . . O 3 . . . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''' )
def test_white_win2(): patient = GameState.root(5) patient.place(patient.board.cell_index(1, 0), COLOR_WHITE) patient.place(patient.board.cell_index(1, 1), COLOR_WHITE) patient.place(patient.board.cell_index(1, 2), COLOR_WHITE) patient.place(patient.board.cell_index(1, 3), COLOR_WHITE) assert str(patient) == ( ''' A B C D E 1 . . . . . O 2 O O O O . O 3 . . . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''' ) patient.place(patient.board.cell_index(0, 4), COLOR_WHITE) assert str(patient) == ( ''' A B C D E 1 . . . . O O 2 O O O O . O 3 . . . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''' ) assert patient.winner() == COLOR_WHITE
def test_two_by_two_action(player): state = GameState.root(2) assert str(state) == (''' A B 1 . . O 2 . . O @ @''') assert player.select_action(state) == 1 state.play(0) assert str(state) == (''' A B 1 O . O 2 . . O @ @''') assert player.select_action(state) == 2 state.undo() state.play(1) assert str(state) == (''' A B 1 . . O 2 O . O @ @''') assert player.select_action(state) == 0
def test_legal_neighbors(): patient = GameState.root(5) patient.place(patient.board.cell_index(1, 1), COLORS['white']) patient.place(patient.board.cell_index(0, 1), COLORS['black']) assert str(patient) == (''' A B C D E 1 . @ . . . O 2 . O . . . O 3 . . . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''') num_neighbors = 5 count = 0 for cell in patient.board.every_legal_neighbor(1, 1): patient.place(patient.board.cell_index(*cell), COLORS['white']) count += 1 assert count == num_neighbors assert str(patient) == (''' A B C D E 1 . @ O . . O 2 O O O . . O 3 O O . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''')
def test_legal_neighbors(): patient = GameState.root(5) patient.place(patient.board.cell_index(1, 1), COLOR_WHITE) patient.place(patient.board.cell_index(0, 1), COLOR_BLACK) assert str(patient) == ( ''' A B C D E 1 . @ . . . O 2 . O . . . O 3 . . . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''' ) num_neighbors = 5 count = 0 for cell in patient.board.every_legal_neighbor(1, 1): patient.place(patient.board.cell_index(*cell), COLOR_WHITE) count += 1 assert count == num_neighbors assert str(patient) == ( ''' A B C D E 1 . @ O . . O 2 O O O . . O 3 O O . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''' )
def test_two_by_two(player): state = GameState.root(2) assert str(state) == ( ''' A B 1 . . O 2 . . O @ @''') assert player.value(state) == 1 state.play(0) assert str(state) == ( ''' A B 1 O . O 2 . . O @ @''') assert player.value(state) == 1 state.undo() state.play(1) assert str(state) == ( ''' A B 1 . . O 2 O . O @ @''') assert player.value(state) == -1
def test_select_action(): random.seed(0) state = GameState.root() patient = MctsAgent(random, UctNode(1)) action = patient.select_action(state, num_iterations=10) state.play(action) assert patient.select_action(state, num_iterations=10) != action
def test_neighbors(): patient = GameState.root(5) patient.place(patient.board.cell_index(1, 1), COLOR_WHITE) assert str(patient) == ( ''' A B C D E 1 . . . . . O 2 . O . . . O 3 . . . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''' ) num_neighbors = 6 count = 0 for cell in patient.board.every_legal_neighbor(1, 1): patient.place(patient.board.cell_index(*cell), COLOR_WHITE) count += 1 assert count == num_neighbors assert str(patient) == ( ''' A B C D E 1 . O O . . O 2 O O O . . O 3 O O . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''' )
def test_move_made_twice(): patient = GameState.root(5) patient.play(patient.board.cell_index(0, 0)) with pytest.raises(IllegalAction) as excinfo: patient.play(patient.board.cell_index(0, 0)) assert 'stone already on cell' in str(excinfo.value)
def test_white_win2(): patient = GameState.root(5) patient.place(patient.board.cell_index(1, 0), COLORS['white']) patient.place(patient.board.cell_index(1, 1), COLORS['white']) patient.place(patient.board.cell_index(1, 2), COLORS['white']) patient.place(patient.board.cell_index(1, 3), COLORS['white']) assert str(patient) == (''' A B C D E 1 . . . . . O 2 O O O O . O 3 . . . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''') patient.place(patient.board.cell_index(0, 4), COLORS['white']) assert str(patient) == (''' A B C D E 1 . . . . O O 2 O O O O . O 3 . . . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''') assert patient.winner() == COLORS['white']
def test_one_by_one(): state = GameState.root(1) assert str(state) == (''' A 1 . O @''') state.play(0) assert state.is_terminal() == True
def test_one_by_one_action(player): state = GameState.root(1) assert str(state) == (''' A 1 . O @''') assert player.select_action(state) == 0
def test_one_by_one(player): state = GameState.root(1) assert str(state) == (''' A 1 . O @''') assert player.value(state) == 1
def test_two_by_two_bug(): patient = GameState.root(2) patient.place(patient.board.cell_index(0, 0), COLORS['white']) patient.place(patient.board.cell_index(1, 1), COLORS['white']) patient.place(patient.board.cell_index(0, 1), COLORS['black']) patient.place(patient.board.cell_index(1, 0), COLORS['black']) assert patient.winner() == COLORS['black']
def test_two_by_two_bug(): patient = GameState.root(2) patient.place(patient.board.cell_index(0, 0), COLOR_WHITE) patient.place(patient.board.cell_index(1, 1), COLOR_WHITE) patient.place(patient.board.cell_index(0, 1), COLOR_BLACK) patient.place(patient.board.cell_index(1, 0), COLOR_BLACK) assert patient.winner() == COLOR_BLACK
def test_one_by_one(player): state = GameState.root(1) assert str(state) == ( ''' A 1 . O @''') assert player.value(state) == 1
def test_connected_neighbors_with_edge(): patient = GameState.root(5) neighbors = [] for neighbor in patient.board.connected_neighbors(-1, 1): neighbors.append(neighbor) neighbors.sort() expected = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0)] assert expected == neighbors
def test_one_by_one_action(player): state = GameState.root(1) assert str(state) == ( ''' A 1 . O @''') assert player.select_action(state) == 0
def test_one_by_one(): state = GameState.root(1) assert str(state) == ( ''' A 1 . O @''') state.play(0) assert state.is_terminal() == True
def test_connected_neighbors_with_border_cell(): patient = GameState.root(5) neighbors = [] for neighbor in patient.board.connected_neighbors((0, 0), 0): neighbors.append(neighbor) assert -1 in neighbors neighbors.remove(-1) neighbors.sort() expected = [(0, 1), (1, 0)] assert expected == neighbors
def test_board_prints(): patient = GameState.root(5) assert str(patient) == (''' A B C D E 1 . . . . . O 2 . . . . . O 3 . . . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''')
def test_play_move(): patient = GameState.root(6, 5) patient.play(patient.board.cell_index(1, 2)) assert str(patient) == (''' A B C D E 1 . . . . . O 2 . . O . . O 3 . . . . . O 4 . . . . . O 5 . . . . . O 6 . . . . . O @ @ @ @ @''')
def test_undo(): patient = GameState.root(5) patient.play(patient.board.cell_index(0, 0)) patient.undo() assert str(patient) == (''' A B C D E 1 . . . . . O 2 . . . . . O 3 . . . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''')
def test_board_prints(): patient = GameState.root(5) assert str(patient) == ( ''' A B C D E 1 . . . . . O 2 . . . . . O 3 . . . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''' )
def test_set_colorless2(): patient = GameState.root(5) with pytest.raises(IndexError): patient.place(patient.board.cell_index(0, 0), COLORS['none']) assert str(patient) == (''' A B C D E 1 . . . . . O 2 . . . . . O 3 . . . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''')
def test_terminal_black(): patient = GameState.root(5) assert patient.winner() == COLOR_NONE patient.place(patient.board.cell_index(0, 0), COLOR_BLACK) assert patient.winner() == COLOR_NONE patient.place(patient.board.cell_index(1, 0), COLOR_BLACK) assert patient.winner() == COLOR_NONE patient.place(patient.board.cell_index(2, 0), COLOR_BLACK) assert patient.winner() == COLOR_NONE patient.place(patient.board.cell_index(3, 0), COLOR_BLACK) assert patient.winner() == COLOR_NONE patient.place(patient.board.cell_index(4, 0), COLOR_BLACK) assert patient.winner() == COLOR_BLACK
def test_terminal_white(): patient = GameState.root(5) assert patient.winner() == COLORS['none'] patient.place(patient.board.cell_index(0, 0), COLORS['white']) assert patient.winner() == COLORS['none'] patient.place(patient.board.cell_index(0, 1), COLORS['white']) assert patient.winner() == COLORS['none'] patient.place(patient.board.cell_index(0, 2), COLORS['white']) assert patient.winner() == COLORS['none'] patient.place(patient.board.cell_index(0, 3), COLORS['white']) assert patient.winner() == COLORS['none'] patient.place(patient.board.cell_index(0, 4), COLORS['white']) assert patient.winner() == COLORS['white']
def test_terminal_black(): patient = GameState.root(5) assert patient.winner() == COLORS['none'] patient.place(patient.board.cell_index(0, 0), COLORS['black']) assert patient.winner() == COLORS['none'] patient.place(patient.board.cell_index(1, 0), COLORS['black']) assert patient.winner() == COLORS['none'] patient.place(patient.board.cell_index(2, 0), COLORS['black']) assert patient.winner() == COLORS['none'] patient.place(patient.board.cell_index(3, 0), COLORS['black']) assert patient.winner() == COLORS['none'] patient.place(patient.board.cell_index(4, 0), COLORS['black']) assert patient.winner() == COLORS['black']
def test_terminal_white(): patient = GameState.root(5) assert patient.winner() == COLOR_NONE patient.place(patient.board.cell_index(0, 0), COLOR_WHITE) assert patient.winner() == COLOR_NONE patient.place(patient.board.cell_index(0, 1), COLOR_WHITE) assert patient.winner() == COLOR_NONE patient.place(patient.board.cell_index(0, 2), COLOR_WHITE) assert patient.winner() == COLOR_NONE patient.place(patient.board.cell_index(0, 3), COLOR_WHITE) assert patient.winner() == COLOR_NONE patient.place(patient.board.cell_index(0, 4), COLOR_WHITE) assert patient.winner() == COLOR_WHITE
def test_border_cells(): patient = GameState.root(5) border_cells = patient.board.border_cells(0, -1) expected_cells = [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4)] assert border_cells == expected_cells border_cells = patient.board.border_cells(0, -2) expected_cells = [(4, 0), (4, 1), (4, 2), (4, 3), (4, 4)] assert border_cells == expected_cells border_cells = patient.board.border_cells(1, -1) expected_cells = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0)] assert border_cells == expected_cells border_cells = patient.board.border_cells(1, -2) expected_cells = [(0, 4), (1, 4), (2, 4), (3, 4), (4, 4)] assert border_cells == expected_cells
def test_undo(): patient = GameState.root(5) patient.play(patient.board.cell_index(0, 0)) patient.undo() assert str(patient) == ( ''' A B C D E 1 . . . . . O 2 . . . . . O 3 . . . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''' )
def test_play_move(): patient = GameState.root(6, 5) patient.play(patient.board.cell_index(1, 2)) assert str(patient) == ( ''' A B C D E 1 . . . . . O 2 . . O . . O 3 . . . . . O 4 . . . . . O 5 . . . . . O 6 . . . . . O @ @ @ @ @''' )
def test_connected_neighbors_with_played_cell(): patient = GameState.root(5) patient.play(patient.board.cell_index(4, 4)) patient.play(patient.board.cell_index(1, 1)) neighbors = [] for neighbor in patient.board.connected_neighbors((0, 1), 0): neighbors.append(neighbor) assert -1 in neighbors neighbors.remove(-1) neighbors.sort() expected = [(1, 0), (0, 0), (0, 2), (2, 0), (1, 2), (2, 1)] expected.sort() assert expected == neighbors
def test_set_colorless1(): patient = GameState.root(5) patient.place(patient.board.cell_index(0, 0), COLORS['white']) with pytest.raises(IllegalAction): patient.place(patient.board.cell_index(0, 0), COLORS['none']) assert str(patient) == (''' A B C D E 1 O . . . . O 2 . . . . . O 3 . . . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''')
def test_set_colorless2(): patient = GameState.root(5) with pytest.raises(IndexError): patient.place(patient.board.cell_index(0, 0), COLOR_NONE) assert str(patient) == ( ''' A B C D E 1 . . . . . O 2 . . . . . O 3 . . . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''' )
def test_set_colorless1(): patient = GameState.root(5) patient.place(patient.board.cell_index(0, 0), COLOR_WHITE) with pytest.raises(IllegalAction): patient.place(patient.board.cell_index(0, 0), COLOR_NONE) assert str(patient) == ( ''' A B C D E 1 O . . . . O 2 . . . . . O 3 . . . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''' )
def test_terminal_black(): patient = GameState.root(5) assert patient.winner() == COLORS['none'] patient.place(patient.board.cell_index(0, 0), COLORS['black']) assert patient.winner() == COLORS['none'] patient.place(patient.board.cell_index(1, 0), COLORS['black']) assert patient.winner() == COLORS['none'] patient.place(patient.board.cell_index(2, 0), COLORS['black']) assert patient.winner() == COLORS['none'] patient.place(patient.board.cell_index(3, 0), COLORS['black']) assert patient.winner() == COLORS['none'] patient.place(patient.board.cell_index(4, 0), COLORS['black']) assert patient.winner() == COLORS['black'] patient.set_player_to_act(color_to_player(COLORS['white'])) patient.undo() assert patient.winner() == COLORS['none']
def test_terminal_black(): patient = GameState.root(5) assert patient.winner() == COLOR_NONE patient.place(patient.board.cell_index(0, 0), COLOR_BLACK) assert patient.winner() == COLOR_NONE patient.place(patient.board.cell_index(1, 0), COLOR_BLACK) assert patient.winner() == COLOR_NONE patient.place(patient.board.cell_index(2, 0), COLOR_BLACK) assert patient.winner() == COLOR_NONE patient.place(patient.board.cell_index(3, 0), COLOR_BLACK) assert patient.winner() == COLOR_NONE patient.place(patient.board.cell_index(4, 0), COLOR_BLACK) assert patient.winner() == COLOR_BLACK patient.set_player_to_act(color_to_player(COLOR_WHITE)) patient.undo() assert patient.winner() == COLOR_NONE
def test_on_empty(): random_generator = random.Random(290134) state = GameState.root(5) patient = RandomAgent(lambda: random_generator.uniform(0, 1)) action = patient.select_action(state) cell = (state.board.row(action), state.board.column(action)) assert cell == (1, 4) assert cell_str(*cell) == 'e2' assert cell == cell_str_to_cell('e2') state.play(action) assert str(state) == (''' A B C D E 1 . . . . . O 2 . . . . O O 3 . . . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''')
def test_black_moves_are_not_hidden_from_white(): patient = GameState.root(5) cell = (0, 0) patient.place(patient.board.cell_index(*cell), COLORS['black']) patient.set_player_to_act(color_to_player(COLORS['black'])) assert patient.player_to_act() == color_to_player(COLORS['black']) assert patient[cell] == COLORS['black'] patient.set_player_to_act(color_to_player(COLORS['white'])) assert patient.player_to_act() == color_to_player(COLORS['white']) assert patient[cell] == COLORS['black'] assert str(patient) == (''' A B C D E 1 @ . . . . O 2 . . . . . O 3 . . . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''')
def test_black_moves_are_not_hidden_from_white(): patient = GameState.root(5) cell = (0, 0) patient.place(patient.board.cell_index(*cell), COLOR_BLACK) patient.set_player_to_act(color_to_player(COLOR_BLACK)) assert patient.player_to_act() == color_to_player(COLOR_BLACK) assert patient[cell] == COLOR_BLACK patient.set_player_to_act(color_to_player(COLOR_WHITE)) assert patient.player_to_act() == color_to_player(COLOR_WHITE) assert patient[cell] == COLOR_BLACK assert str(patient) == ( ''' A B C D E 1 @ . . . . O 2 . . . . . O 3 . . . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''' )
def test_on_empty(): random_generator = random.Random(290134) state = GameState.root(5) patient = RandomAgent(lambda: random_generator.uniform(0, 1)) action = patient.select_action(state) cell = (state.board.row(action), state.board.column(action)) assert cell == (1, 4) assert cell_str(*cell) == 'e2' assert cell == cell_str_to_cell('e2') state.play(action) assert str(state) == ( ''' A B C D E 1 . . . . . O 2 . . . . O O 3 . . . . . O 4 . . . . . O 5 . . . . . O @ @ @ @ @''' )
def test_dijkstra_distance(): patient = GameState.root(5) distance = patient.board.dijkstra_distance(0, -1, -2) assert distance == 6
def test_dijkstra_distance_with_played_cell(): patient = GameState.root(5) patient.play(patient.board.cell_index(2, 0)) distance = patient.board.dijkstra_distance(1, -1, -2) assert distance == 5
def test_heuristic(): patient = GameState.root(5) assert patient.heuristic(0) == 0
def test_heuristic_with_played_cell(): patient = GameState.root(5) patient.play(patient.board.cell_index(2, 0)) assert patient.heuristic(1) == 1 / 5
def game_states(): return [ FakeGameState(), HexGameState.root(), TttGameState() ]