Ejemplo n.º 1
0
def test_choose_move():
    np.random.seed(0)
    start_state = Connect4State()
    state1 = Connect4State("""\
.......
.......
.......
...XX..
OXOXO..
XOXOXOO
""")
    expected_display = """\
.......
.......
.......
..XXX..
OXOXO..
XOXOXOO
"""
    player = MctsPlayer(start_state, iteration_count=200)

    move = player.choose_move(state1)
    state2 = state1.make_move(move)
    display = state2.display()

    assert display == expected_display
Ejemplo n.º 2
0
def test_create_board():
    expected_spaces = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0],
                                [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0],
                                [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]])
    board = Connect4State()

    assert np.array_equal(board.get_spaces(), expected_spaces)
Ejemplo n.º 3
0
def test_pieces(pixmap_differ: PixmapDiffer):
    actual: QPainter
    expected: QPainter
    with pixmap_differ.create_painters(280, 240,
                                       'connect4_pieces') as (actual,
                                                              expected):
        draw_grid(expected)
        expected.setBrush(Connect4Display.player1_colour)
        expected.drawEllipse(125, 205, 30, 30)
        expected.drawEllipse(165, 205, 30, 30)
        expected.setBrush(Connect4Display.player2_colour)
        expected.drawEllipse(125, 165, 30, 30)

        display = Connect4Display()
        board = Connect4State('''\
.......
.......
.......
.......
...O...
...XX..
''')
        display.resize(372, 264)
        display.update_board(board)

        render_display(display, actual)
Ejemplo n.º 4
0
def test_choose_move_in_pool():
    start_state = Connect4State()
    state1 = Connect4State("""\
.......
.......
.......
...XX..
OXOXO..
XOXOXOO
""")
    player = MctsPlayer(start_state, iteration_count=200, process_count=2)
    valid_moves = start_state.get_valid_moves()

    move = player.choose_move(state1)

    # Can't rely on which move, because other process has separate random seed.
    assert valid_moves[move]
Ejemplo n.º 5
0
def test_train():
    training_path = Path(__file__).parent / 'training_data.json'
    boards_list, outputs_list = json.loads(training_path.read_text())
    boards = np.array(boards_list)
    outputs = np.array(outputs_list)

    board = Connect4State()
    neural_net = NeuralNet(board)

    history = neural_net.train(boards, outputs)

    assert history is not None
Ejemplo n.º 6
0
    def __init__(self):
        display = Connect4Display()
        display.resize(288, 204)
        display.update_board(Connect4State('''\
.......
......X
.....XO
..XOXOX
..OXOXO
..OXXXO
'''))
        super().__init__(display, 210, 180)
Ejemplo n.º 7
0
def test_diagonal2_winner():
    text = """\
......X
.....XO
..XOXOX
..OXOXO
..OXXXO
"""
    board = Connect4State(text)
    expected_winner = board.X_PLAYER
    winner = board.get_winner()

    assert winner == expected_winner
Ejemplo n.º 8
0
def test_choose_move_sets_current_node():
    np.random.seed(0)
    start_state = Connect4State()
    state1 = Connect4State("""\
.......
.......
.......
.......
OXOXOXO
XOXOXOX
""")
    player = MctsPlayer(start_state, iteration_count=20)

    move1 = player.choose_move(state1)
    current_node1 = player.search_manager.current_node
    state2 = state1.make_move(move1)
    move2 = player.choose_move(state2)
    current_node2 = player.search_manager.current_node
    state3 = state2.make_move(move2)

    assert current_node1.game_state == state2
    assert current_node2.game_state == state3
Ejemplo n.º 9
0
def test_valid_moves_after_win():
    text = """\
.......
.......
...O...
..OX...
.OXX...
OXXO...
"""
    board = Connect4State(text)
    valid_moves = board.get_valid_moves()

    assert not valid_moves.any()
Ejemplo n.º 10
0
def test_vertical_winner():
    text = """\
.......
.....O.
.....O.
....XO.
..XXXO.
"""
    board = Connect4State(text)
    expected_winner = board.O_PLAYER
    winner = board.get_winner()

    assert winner == expected_winner
Ejemplo n.º 11
0
def test_choose_move_no_iterations():
    np.random.seed(0)
    start_state = Connect4State()
    state1 = Connect4State("""\
.......
.......
.......
...XX..
OXOXO..
XOXOXOO
""")
    test_count = 400
    expected_count = test_count / 7
    expected_low = expected_count * 0.9
    expected_high = expected_count * 1.1
    move_counts = Counter()
    for _ in range(test_count):
        player = MctsPlayer(start_state, iteration_count=0)

        move = player.choose_move(state1)
        move_counts[move] += 1

    assert expected_low < move_counts[2] < expected_high
Ejemplo n.º 12
0
def test_get_active_player_x():
    text = """\
.......
.......
.......
....O..
....X..
...XO..
"""
    expected_player = Connect4State.X_PLAYER
    board = Connect4State(text)
    player = board.get_active_player()

    assert player == expected_player
Ejemplo n.º 13
0
def test_longer_winner():
    text = """\
.......
.......
.......
.......
..OOO..
XXXXXO.
"""
    board = Connect4State(text)
    expected_winner = board.X_PLAYER
    winner = board.get_winner()

    assert winner == expected_winner
Ejemplo n.º 14
0
def test_horizontal_end_winner():
    text = """\
.......
.......
.......
.......
...OO..
..OXXXX
"""
    board = Connect4State(text)
    expected_winner = board.X_PLAYER
    winner = board.get_winner()

    assert winner == expected_winner
Ejemplo n.º 15
0
def test_get_valid_moves():
    text = """\
1234567
....X..
....O..
....X..
....O..
....X..
...XO..
"""
    expected_moves = np.array([1, 1, 1, 1, 0, 1, 1])
    board = Connect4State(text)
    moves = board.get_valid_moves()

    assert np.array_equal(expected_moves, moves)
Ejemplo n.º 16
0
def test_display():
    spaces = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 1, -1, 0, 0]])
    expected_text = """\
.......
.......
.......
.......
....X..
...XO..
"""
    text = Connect4State(spaces=spaces).display()

    assert text == expected_text
Ejemplo n.º 17
0
def test_create_board_from_text():
    text = """\
.......
.......
.......
.......
....X..
...XO..
"""
    expected_spaces = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0],
                                [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0],
                                [0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 1, -1, 0, 0]])
    board = Connect4State(text)

    assert np.array_equal(board.get_spaces(), expected_spaces)
Ejemplo n.º 18
0
def test_display_coordinates():
    spaces = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 1, -1, 0, 0]])
    expected_text = """\
1234567
.......
.......
.......
.......
....X..
...XO..
"""
    text = Connect4State(spaces=spaces).display(show_coordinates=True)

    assert expected_text == text
Ejemplo n.º 19
0
def test_create_board_with_coordinates():
    text = """\
1234567
.......
.......
.......
.......
....X..
...XO..
"""
    expected_board = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0],
                               [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0],
                               [0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 1, -1, 0, 0]])
    board = Connect4State(text)

    assert np.array_equal(board.get_spaces(), expected_board)
Ejemplo n.º 20
0
def test_end_game_value():
    board = Connect4State("""\
.......
.......
.......
.......
OOO....
XXXX...
""")
    heuristic = NeuralNet(board)
    expected_value = -1.0
    expected_policy = [1/7] * 7

    value, policy = heuristic.analyse(board)

    assert expected_value == value
    assert expected_policy == policy.tolist()
Ejemplo n.º 21
0
def test_make_move_o():
    text = """\
.......
.......
.......
.......
....X..
...XO..
"""
    move = 4
    expected_display = """\
.......
.......
.......
....O..
....X..
...XO..
"""
    board1 = Connect4State(text)
    board2 = board1.make_move(move)
    display = board2.display()

    assert display == expected_display
Ejemplo n.º 22
0
def test_parse_move(text, expected_move):
    game = Connect4State()
    move = game.parse_move(text)

    assert move == expected_move
Ejemplo n.º 23
0
def test_parse_move_fails(text, expected_message):
    game = Connect4State()
    with pytest.raises(ValueError, match=expected_message):
        game.parse_move(text)
Ejemplo n.º 24
0
 def __init__(self):
     super().__init__(Connect4State())
Ejemplo n.º 25
0
def test_display_player_o():
    expected_display = 'Player O'

    display = Connect4State().display_player(Connect4State.O_PLAYER)

    assert display == expected_display