Example #1
0
def test_start_self_play():
    chess = board.Gomoku(board_size=2, n_long=1)
    player = MockPlayer(chess)

    s = []
    p = []
    v = []

    state = np.zeros((4, 2, 2))
    probs = np.zeros((2 * 2))

    value = 1.0
    state[3] = 1.0

    probs[0] = 0.9

    s.append(state)
    p.append(probs)
    v.append(value)

    result = list(zip(s, p, v))

    a = self_play.start_self_play(chess, player)

    b = list(a)

    for i in range(len(b)):
        assert (b[i][0] == result[i][0]).all()
        assert (b[i][1] == result[i][1]).all()
        assert b[i][2] == result[i][2]
Example #2
0
def test_mcts_get_move2():
    # 多方有4连线
    chess = board.Gomoku(board_size=6)
    chess.last_action = 7
    chess.moved_player = 1
    chess.status = [
      2, 2, 0, 0, 0, 2,
      0, 1, 1, 1, 0, 0,
      0, 0, 1, 0, 0, 2,
      2, 0, 0, 1, 0, 0,
      0, 0, 0, 0, 1, 0,
      2, 2, 0, 0, 0, 0]
    for action in range(chess.size * chess.size):
        if chess.status[action] != 0:
            chess.available.remove(action)

    chess.current_player = 2
    mcts_app = mcts.MCTS(mcts.policy_value_fn, c_puct=5, simulate_time=15)

    move = mcts_app.get_move(chess)

    count = 0
    for i in range(3):
        move = mcts_app.get_move(chess)
        if move == 35:
            count = count + 1

    assert(move >= 2)
Example #3
0
def test_expert_vs_random():
    chess = board.Gomoku(board_size=6)
    player1 = player.RandomPlayer(chess)
    player2 = player.ExpertPlayer(chess)
    chess.play(player1, player2, isShow=False)

    assert chess.end
    assert chess.winner == chess.player2
Example #4
0
def test_mcts_vs_mcts():
    chess = board.Gomoku(board_size=6)
    player1 = player.MCTSPlayer(chess, simulate_time=1)
    player2 = player.RandomPlayer(chess)
    chess.play(player1, player2, isShow=False)

    assert chess.end
    assert chess.winner != chess.player2  # tie or mcts win
Example #5
0
def test_mcts_vs_expert():
    chess = board.Gomoku(board_size=6)
    player1 = player.MCTSPlayer(chess, simulate_time=1)
    player2 = player.ExpertPlayer(chess)
    chess.play(player1, player2, isShow=False)

    assert chess.end
    assert chess.winner != chess.player1  # tie or expert win
Example #6
0
def test_is_won_6():
    chess = board.Gomoku(board_size=6)

    chess.last_action = 11
    chess.moved_player = 1
    chess.status = [
        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, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0
    ]
    assert chess.is_won()
Example #7
0
def test_is_won_1():
    chess = board.Gomoku(board_size=6)

    chess.status = [
        0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
        0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0
    ]
    chess.last_action = 3
    chess.moved_player = 1
    assert chess.is_won() == True
Example #8
0
def test_is_won_2():
    chess = board.Gomoku(board_size=6)

    chess.moved_player = 2
    chess.last_action = 10
    chess.status = [
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0,
        0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0
    ]
    assert chess.is_won() == True
Example #9
0
def test_current_state_1():
    chess = board.Gomoku(board_size=3)

    chess.last_action = 2
    chess.moved_player = 1
    chess.current_player = 2
    chess.status = [1, 2, 1, 0, 0, 0, 0, 0, 0]

    state = chess.current_state()

    p = np.zeros((4, 3, 3))
    p[0][0, 0] = 1
    p[0][0, 2] = 1

    p[1][0, 1] = 1

    p[2][0, 2] = 1

    p[3] = 0

    assert (state == p).all()  # 所有元素相等,返回true
Example #10
0
def test_rotate():
    chess = board.Gomoku(board_size=2, n_long=1)
    s = []
    state = np.zeros((4, 2, 2))
    state[0][0, 0] = 1
    state[0][0, 1] = 2
    state[0][1, 0] = 3
    state[0][1, 1] = 4
    s.append(state)
    p = [np.array([1, 2, 3, 4])]
    v = [1]
    one_game_data = zip(s, p, v)
    extra = self_play.rotate(chess, one_game_data)
    a1 = np.array([[2, 4], [1, 3]])
    b1 = np.array([2, 4, 1, 3])
    a2 = np.array([[4, 3], [2, 1]])
    b2 = np.array([4, 3, 2, 1])
    # print(extra[2][1])
    assert (extra[0][0][0] == a1).all()
    assert (extra[0][1] == b1).all()
    assert (extra[1][0][0] == a2).all()
    assert (extra[1][1] == b2).all()