コード例 #1
0
def test_make_move_o():
    text = """\
......
......
......
......
......
...XO.
>O
"""
    move = 32
    expected_display = """\
......
......
......
......
......
..OOO.
>X
"""
    board1 = OthelloState(text)
    board2 = board1.make_move(move)
    display = board2.display()

    assert display == expected_display
コード例 #2
0
def test_get_active_player_x():
    text = """\
......
......
......
......
....X.
...XO.
>X
"""
    expected_player = OthelloState.X_PLAYER
    board = OthelloState(text)
    player = board.get_active_player()

    assert player == expected_player
コード例 #3
0
def test_winner_o():
    text = """\
XXXXXX
XXXXXX
XXXXXO
OOOOOO
OOOOOO
OOOOOO
>O
"""
    board = OthelloState(text)
    expected_winner = board.O_PLAYER
    winner = board.get_winner()

    assert winner == expected_winner
コード例 #4
0
def test_piece_click_then_pass(pixmap_differ: PixmapDiffer):
    start_state = """\
OX....
.X....
......
......
......
......
>O
"""
    expected_state = """\
OOO...
.X....
......
......
......
......
>O
"""
    display = OthelloDisplay(6, 6)
    display.update_board(OthelloState(start_state))
    display.on_click(display.spaces[0][2])

    state = display.current_state.display()

    assert state == expected_state
    display.close()
コード例 #5
0
def test_analyze_bad_move():
    """ Player chooses a move not in the top ten. """
    log = LogDisplay()
    step = 1
    move = 60  # 8E
    move_probabilities = [('2G', 1.0, 9999, 1.0), ('2F', 0.0, 2, 0.0),
                          ('3F', 0.0, 2, 0.0), ('2D', 0.0, 2, 0.0),
                          ('2C', 0.0, 2, 0.0), ('3C', 0.0, 2, 0.0),
                          ('4C', 0.0, 2, 0.0), ('6C', 0.0, 2, 0.0),
                          ('7D', 0.0, 2, 0.0), ('7E', 0.0, 2, 0.0)]
    board = OthelloState("""\
........
........
...XO.X.
...OOXXO
..OXXXXO
...XXXXO
.....XOO
.....OOO
>O
""",
                         board_height=8,
                         board_width=8)

    log.record_move(board, move)
    log.analyse_move(board, TicTacToeState.X_PLAYER, move_probabilities)

    assert log.items == [
        LogItem(step, 'Player O', '8E', board, '?', move_probabilities)
    ]
コード例 #6
0
def test_no_winner_tie():
    text = """\
XXXXXX
XXXXXX
XXXXXX
OOOOOO
OOOOOO
OOOOOO
>O
"""
    board = OthelloState(text)
    expected_winner = board.NO_PLAYER
    winner = board.get_winner()

    assert winner == expected_winner
    assert board.is_ended()
コード例 #7
0
def test_winner_x():
    text = """\
XXXXXX
XXXXXX
XXXXXX
OOOOOX
OOOOOO
OOOOOO
>O
"""
    board = OthelloState(text)
    expected_winner = board.X_PLAYER
    winner = board.get_winner()

    assert winner == expected_winner
    assert board.is_win(expected_winner)
コード例 #8
0
def test_no_moves_for_either():
    board = OthelloState("""\
......
......
......
X.....
.OO...
.OO...
>X
""")
    expected_valid_moves = [False] * 37

    valid_moves = board.get_valid_moves()

    assert valid_moves.tolist() == expected_valid_moves
    assert board.get_winner() == board.O_PLAYER
コード例 #9
0
def test_pass_is_not_win():
    board = OthelloState("""\
......
......
X.....
.OO...
.OO...
......
>O
""")
    expected_valid_moves = [False] * 36 + [True]

    valid_moves = board.get_valid_moves()

    assert valid_moves.tolist() == expected_valid_moves
    assert not board.is_ended()
コード例 #10
0
def test_training_data():
    state = OthelloState()
    neural_net = NeuralNet(state)
    neural_net.epochs_to_train = 10
    search_manager = SearchManager(state, neural_net)
    boards, outputs = search_manager.create_training_data(
        iterations=10,
        data_size=10)
    neural_net.train(boards, outputs)
コード例 #11
0
def test_get_valid_moves_with_gap():
    text = """\
  ABCDEF
1 ......
2 ......
3 ......
4 ....X.
5 ...O..
6 ...XO.
>O
"""
    expected_moves = [[0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 1],
                      [0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0],
                      [0, 0, 1, 0, 0, 0]]
    board = OthelloState(text)
    moves = board.get_valid_moves()

    assert moves.astype(int)[:36].reshape((6, 6)).tolist() == expected_moves
コード例 #12
0
def test_get_no_valid_moves():
    text = """\
  ABCDEF
1 ......
2 ......
3 ......
4 ......
5 ....X.
6 ...OO.
>X
"""
    expected_moves = [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,
                      1]  # Only move is to pass.
    board = OthelloState(text)
    moves = board.get_valid_moves()

    assert moves.tolist() == expected_moves
コード例 #13
0
def test_get_valid_moves():
    text = """\
  ABCDEF
1 ......
2 ......
3 ......
4 ......
5 ...OX.
6 ...XO.
>X
"""
    expected_moves = [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, 1, 0, 0, 0,
                      0, 0, 0, 0, 0, 1,
                      0]  # Can't pass when other moves are valid.
    board = OthelloState(text)
    moves = board.get_valid_moves()

    assert moves.tolist() == expected_moves
コード例 #14
0
def test_create_board():
    x, o = OthelloState.X_PLAYER, OthelloState.O_PLAYER
    # 6x6 grid of spaces, plus next player.
    expected_board = [0, 0, 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0,
                      0, 0, o, x, 0, 0,
                      0, 0, x, o, 0, 0,
                      0, 0, 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0,
                      x]
    board = OthelloState()

    assert board.board.tolist() == expected_board
コード例 #15
0
def test_piece_count_after_update(pixmap_differ: PixmapDiffer):
    display = OthelloDisplay()
    state2 = OthelloState(board_width=8, board_height=8, text="""\
........
........
........
...OX...
...XXX..
........
........
........
>O
""")
    display.update_board(state2)
    assert display.ui.black_count.text() == '4'
    assert display.ui.white_count.text() == '1'
    display.close()
コード例 #16
0
def test_display():
    x, o = OthelloState.X_PLAYER, OthelloState.O_PLAYER
    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, x, 0,
                      0, 0, 0, x, o, 0,
                      x])
    expected_text = """\
......
......
......
......
....X.
...XO.
>X
"""
    text = OthelloState(spaces=board).display()

    assert text == expected_text
コード例 #17
0
def test_create_board_from_text():
    x, o = OthelloState.X_PLAYER, OthelloState.O_PLAYER
    text = """\
......
......
......
......
....X.
...XO.
>O
"""
    expected_board = [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, x, 0,
                      0, 0, 0, x, o, 0,
                      o]
    board = OthelloState(text)

    assert board.board.tolist() == expected_board
コード例 #18
0
def test_display_coordinates():
    x, o = OthelloState.X_PLAYER, OthelloState.O_PLAYER
    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, x, 0,
                      0, 0, 0, x, o, 0,
                      x])
    expected_text = """\
  ABCDEF
1 ......
2 ......
3 ......
4 ......
5 ....X.
6 ...XO.
>X
"""
    text = OthelloState(spaces=board).display(show_coordinates=True)

    assert text == expected_text
コード例 #19
0
def test_create_board_with_coordinates():
    x, o = OthelloState.X_PLAYER, OthelloState.O_PLAYER
    text = """\
  ABCDEF
1 ......
2 ......
3 ......
4 ......
5 ....X.
6 ...XO.
>X
"""
    expected_board = [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, x, 0,
                      0, 0, 0, x, o, 0,
                      x]
    board = OthelloState(text)

    assert board.board.tolist() == expected_board
コード例 #20
0
def test_display_move(move, expected_text):
    state = OthelloState(board_height=8, board_width=8)
    move_text = state.display_move(move)

    assert move_text == expected_text
コード例 #21
0
def test_create_from_array():
    board = OthelloState(spaces=np.zeros(65, dtype=np.int8))

    assert board.board_width == 8
コード例 #22
0
def test_parse_move(text, expected_move):
    board = OthelloState()
    move = board.parse_move(text)

    assert move == expected_move
コード例 #23
0
def test_parse_move_fails(text, expected_message):
    state = OthelloState()
    with pytest.raises(ValueError, match=expected_message):
        state.parse_move(text)
コード例 #24
0
 def __init__(self, board_height: int = 8, board_width: int = 8):
     super().__init__(OthelloState(board_height=board_height,
                                   board_width=board_width))
     self.ui.black_count_pixmap.setPixmap(self.player1_icon)
     self.ui.white_count_pixmap.setPixmap(self.player2_icon)
     self.update_count_text()
コード例 #25
0
def test_playout():
    """ Just checking that playouts don't raise an exception. """
    board = OthelloState()
    playout = Playout()

    playout.simulate(board)