def test_create_board():
    expected_spaces = np.array([[0, 0, 0],
                                [0, 0, 0],
                                [0, 0, 0]])
    board = TicTacToeState()

    assert np.array_equal(board.get_spaces(), expected_spaces)
def test_get_valid_moves():
    text = """\
X..
.O.
...
"""
    expected_moves = np.array([0, 1, 1, 1, 0, 1, 1, 1, 1])
    board = TicTacToeState(text)
    moves = board.get_valid_moves()

    assert np.array_equal(moves, expected_moves)
def test_draw_ended():
    text = """\
OXX
XOO
XOX
"""
    expected_is_ended = True
    board = TicTacToeState(text)
    is_ended = board.is_ended()

    assert is_ended == expected_is_ended
def test_winner_ended():
    text = """\
OX.
XOO
XXO
"""
    expected_is_ended = True
    board = TicTacToeState(text)
    is_ended = board.is_ended()

    assert is_ended == expected_is_ended
def test_not_ended():
    text = """\
OX.
XOO
XOX
"""
    expected_is_ended = False
    board = TicTacToeState(text)
    is_ended = board.is_ended()

    assert is_ended == expected_is_ended
def test_diagonal_winner():
    text = """\
OX.
XOO
XXO
"""
    board = TicTacToeState(text)
    expected_winner = board.O_PLAYER
    winner = board.get_winner()

    assert winner == expected_winner
def test_vertical_winner():
    text = """\
XXO
.OO
XXO
"""
    board = TicTacToeState(text)
    expected_winner = board.O_PLAYER
    winner = board.get_winner()

    assert winner == expected_winner
def test_horizontal_winner():
    text = """\
XXX
.OO
...
"""
    board = TicTacToeState(text)
    expected_winner = board.X_PLAYER
    winner = board.get_winner()

    assert winner == expected_winner
def test_no_winner():
    text = """\
XX.
.OO
...
"""
    board = TicTacToeState(text)
    expected_winner = board.NO_PLAYER
    winner = board.get_winner()

    assert winner == expected_winner
def test_get_active_player_x():
    text = """\
XX.
.OO
...
"""
    expected_player = TicTacToeState.X_PLAYER
    board = TicTacToeState(text)
    player = board.get_active_player()

    assert player == expected_player
def test_display():
    board = TicTacToeState(spaces=np.array([[1, 0, 0],
                                            [0, -1, 0],
                                            [0, 0, 0]]))
    expected_text = """\
X..
.O.
...
"""
    text = board.display()

    assert text == expected_text
def test_get_valid_moves_after_win():
    """ Win gets checked in search, so don't also check in valid moves. """
    text = """\
XXX
OO.
...
"""
    expected_moves = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1])
    board = TicTacToeState(text)
    moves = board.get_valid_moves()

    assert np.array_equal(expected_moves, moves)
def test_create_board_from_text():
    text = """\
X..
.O.
...
"""
    expected_spaces = np.array([[1, 0, 0],
                                [0, -1, 0],
                                [0, 0, 0]])
    board = TicTacToeState(text)

    assert np.array_equal(board.get_spaces(), expected_spaces)
Exemplo n.º 14
0
def test_select_first_child():
    start_state = TicTacToeState()
    expected_leaf_board = start_state.make_move(0)
    expected_leaf = SearchNode(expected_leaf_board)

    node = SearchNode(start_state)
    node.record_value(1)

    leaf = node.select_leaf()

    assert leaf == expected_leaf
    assert node.average_value == -1.0
def test_create_board_with_coordinates():
    text = """\
  ABC
1 X..
2 .O.
3 ...
"""
    expected_spaces = np.array([[1, 0, 0],
                                [0, -1, 0],
                                [0, 0, 0]])
    board = TicTacToeState(text)

    assert np.array_equal(board.get_spaces(), expected_spaces)
def test_display_coordinates():
    board = TicTacToeState(spaces=np.array([[1, 0, 0],
                                            [0, -1, 0],
                                            [0, 0, 0]]))
    expected_text = """\
  ABC
1 X..
2 .O.
3 ...
"""
    text = board.display(show_coordinates=True)

    assert text == expected_text
Exemplo n.º 17
0
def test_search_manager_reuses_node():
    start_state = TicTacToeState()
    manager = SearchManager(start_state, Playout())
    manager.search(start_state, iterations=10)
    move = manager.get_best_move()
    state2 = start_state.make_move(move)
    node = manager.current_node

    first_value_count = node.value_count
    manager.search(state2, iterations=10)
    second_value_count = node.value_count

    assert first_value_count > 0
    assert first_value_count + 10 == second_value_count
Exemplo n.º 18
0
def test_search_manager_with_opponent():
    """ Like when opponent is not sharing the SearchManager. """
    start_state = TicTacToeState()
    manager = SearchManager(start_state, Playout())
    manager.search(start_state, iterations=10)
    node = manager.current_node.children[0]  # Didn't call get_best_move().
    move = 0
    state2 = start_state.make_move(move)

    first_value_count = node.value_count
    manager.search(state2, iterations=10)
    second_value_count = node.value_count

    assert first_value_count > 0
    assert first_value_count + 10 == second_value_count
Exemplo n.º 19
0
def test_select_grandchild():
    start_state = TicTacToeState()
    expected_leaf_board = TicTacToeState("""\
XO.
...
...
""")
    expected_leaf = SearchNode(expected_leaf_board)

    node = SearchNode(start_state)
    for _ in range(10):
        node.select_leaf().record_value(0)

    leaf = node.select_leaf()

    assert leaf == expected_leaf
Exemplo n.º 20
0
def test_eq():
    board1 = TicTacToeState()
    board2 = TicTacToeState()
    board3 = TicTacToeState("""\
...
.X.
...
""")

    node1 = SearchNode(board1)
    node2 = SearchNode(board2)
    node3 = SearchNode(board3)

    assert node1 == node2
    assert node1 != node3
    assert node1 != 42
def test_coordinates(pixmap_differ: PixmapDiffer):
    assert 1 == 1
    actual: QPainter
    expected: QPainter
    with pixmap_differ.create_painters(200, 200,
                                       'tictactoe_coordinates') as (actual,
                                                                    expected):
        expected.fillRect(0, 0, 250, 200, TicTacToeDisplay.background_colour)
        expected.drawLine(50, 100, 200, 100)
        expected.drawLine(50, 150, 200, 150)
        expected.drawLine(100, 50, 100, 200)
        expected.drawLine(150, 50, 150, 200)
        expected.setBrush(TicTacToeDisplay.player1_colour)
        expected.drawEllipse(106, 106, 37, 37)
        set_font_size(expected, 25)
        draw_text(expected, 75, 25, 'A')
        draw_text(expected, 125, 25, 'B')
        draw_text(expected, 175, 25, 'C')
        draw_text(expected, 25, 75, '1')
        draw_text(expected, 25, 125, '2')
        draw_text(expected, 25, 175, '3')

        display = TicTacToeDisplay()
        display.resize(276, 224)

        display.show_coordinates = True
        board = TicTacToeState('''\
  ABC
1 ...
2 .X.
3 ...
''')
        display.update_board(board)

        render_display(display, actual)
def test_pieces(pixmap_differ: PixmapDiffer):
    size = 240
    with pixmap_differ.create_painters(size, size,
                                       'tictactoe_pieces') as (actual,
                                                               expected):
        draw_square_grid(expected)
        expected.setBrush(TicTacToeDisplay.player1_colour)
        expected.drawEllipse(10, 10, 60, 60)
        expected.drawEllipse(90, 90, 60, 60)
        expected.setBrush(TicTacToeDisplay.player2_colour)
        expected.drawEllipse(90, 10, 60, 60)

        display = TicTacToeDisplay()
        board = TicTacToeState('''\
XO.
.X.
...
''')
        display.update_board(board)

        display.resize(324, 264)
        render_display(display, actual)

    player_pixmap = display.ui.player_pixmap.pixmap()
    assert player_pixmap.toImage() == display.player2_icon.toImage()
def test_winner(pixmap_differ: PixmapDiffer):
    assert 1 == 1
    size = 240
    with pixmap_differ.create_painters(size, size,
                                       'tictactoe_winner') as (actual,
                                                               expected):
        draw_square_grid(expected)
        expected.setBrush(TicTacToeDisplay.player1_colour)
        expected.drawEllipse(10, 10, 60, 60)
        expected.drawEllipse(90, 90, 60, 60)
        expected.drawEllipse(170, 170, 60, 60)
        expected.setBrush(TicTacToeDisplay.player2_colour)
        expected.drawEllipse(90, 10, 60, 60)
        expected.drawEllipse(170, 90, 60, 60)

        display = TicTacToeDisplay()
        display.resize(324, 264)

        board = TicTacToeState('''\
XO.
.XO
..X
''')
        display.update_board(board)

        assert not display.spaces[0][2].isVisible()
        expected_icon = display.player1_icon.toImage()
        assert display.ui.player_pixmap.pixmap().toImage() == expected_icon
        assert display.move_text.text() == 'wins'
        render_display(display, actual)
def test_draw(pixmap_differ: PixmapDiffer):
    size = 240
    with pixmap_differ.create_painters(size, size,
                                       'tictactoe_draw') as (actual, expected):
        draw_square_grid(expected)
        expected.setBrush(TicTacToeDisplay.player1_colour)
        expected.drawEllipse(10, 10, 60, 60)
        expected.drawEllipse(10, 90, 60, 60)
        expected.drawEllipse(90, 170, 60, 60)
        expected.drawEllipse(170, 170, 60, 60)
        expected.drawEllipse(170, 10, 60, 60)
        expected.setBrush(TicTacToeDisplay.player2_colour)
        expected.drawEllipse(90, 10, 60, 60)
        expected.drawEllipse(90, 90, 60, 60)
        expected.drawEllipse(170, 90, 60, 60)
        expected.drawEllipse(10, 170, 60, 60)

        display = TicTacToeDisplay()
        display.resize(324, 264)

        display.update_board(display.start_state)
        board = TicTacToeState('''\
XOX
XOO
OXX
''')
        display.update_board(board)

        render_display(display, actual)
    assert display.ui.player_pixmap.pixmap() is None
    assert display.move_text.text() == 'draw'
def test_piece_hover_enter(pixmap_differ: PixmapDiffer):
    size = 240
    with pixmap_differ.create_painters(
            size, size, 'tictactoe_piece_hover_enter') as (actual, expected):
        draw_square_grid(expected)
        expected.setBrush(TicTacToeDisplay.player1_colour)
        expected.drawEllipse(10, 10, 60, 60)
        colour = QColor(TicTacToeDisplay.player2_colour)
        expected.setBrush(colour)
        colour.setAlpha(127)
        expected.setBrush(colour)
        colour.setRgb(0, 0, 0, 127)
        pen = QPen(colour)
        expected.setPen(pen)
        expected.drawEllipse(90, 10, 60, 60)

        display = TicTacToeDisplay()

        board = TicTacToeState('''\
X..
...
...
''')
        display.resize(324, 264)
        display.ui.game_display.grab()
        display.update_board(board)
        display.on_hover_enter(display.spaces[0][1])

        render_display(display, actual)
Exemplo n.º 26
0
def test_default_board():
    expected_board = TicTacToeState()
    expected_node = SearchNode(expected_board)

    node = SearchNode(expected_board)

    assert expected_node == node
Exemplo n.º 27
0
def test_select_leaf_self():
    game = TicTacToeState()
    node = SearchNode(game)
    expected_leaf = node

    leaf = node.select_leaf()

    assert expected_leaf == leaf
def test_make_move_o():
    text = """\
XX.
.O.
...
"""
    move = 3
    expected_display = """\
XX.
OO.
...
"""
    board1 = TicTacToeState(text)
    board2 = board1.make_move(move)
    display = board2.display()

    assert display == expected_display
Exemplo n.º 29
0
def test_win_scores_one():
    """ Expose bug where search continues after a game-ending position. """
    state1 = TicTacToeState("""\
..X
XX.
OO.
""")

    player = MctsPlayer(TicTacToeState(), state1.X_PLAYER, iteration_count=100)

    move = player.choose_move(state1)

    search_node1 = player.search_manager.current_node.parent
    for child_node in search_node1.children:
        if child_node.move == 8:
            assert child_node.average_value == 1.0
    assert move == 8
Exemplo n.º 30
0
    def __init__(self):
        display = TicTacToeDisplay()
        display.update_board(TicTacToeState('''\
OX.
XO.
X.O
'''))
        display.resize(276, 224)
        super().__init__(display, 200, 200)