Esempio n. 1
0
def main():
    size = 15
    time_delay = 1
    chessboard = Board(size)
    tree_ai = tree.AI(size, COLOR_BLACK, time_delay)
    greed_ai = greed.AI(size, COLOR_WHITE, time_delay)

    # chessboard.read('chess_log.txt')
    end1 = False
    end2 = False
    for i in range(size**2):
        start1 = time.time()
        tree_ai.go(chessboard.board)
        cost1 = time.time() - start1
        chessboard.load(tree_ai)
        end1 = chessboard.show()
        if end1:
            break
        # time.sleep(1)
        start = time.time()
        greed_ai.go(chessboard.board)
        cost = time.time() - start
        chessboard.load(greed_ai)
        end2 = chessboard.show()
        if end2:
            break
        # time.sleep(1)
    if end1:
        print("tree win")
    if end2:
        print("greedy win")
Esempio n. 2
0
def check_terminal():
    ai = chess_tree.AI(15, chess_tree.COLOR_BLACK, 5)
    board = np.zeros((15, 15), dtype=int)
    board[7][7] = chess_tree.COLOR_BLACK
    board[7][6] = chess_tree.COLOR_BLACK
    board[7][5] = chess_tree.COLOR_BLACK
    board[7][4] = chess_tree.COLOR_BLACK
    board[7][3] = chess_tree.COLOR_BLACK
    board[7][10] = chess_tree.COLOR_BLACK
    assert ai.terminal_test(board, (7, 7))
Esempio n. 3
0
 def test_tree_gen_terminal3(self):
     chessboard = Board(15)
     chessboard.board[3][2] = chess_tree.COLOR_WHITE
     chessboard.board[3][3] = chess_tree.COLOR_WHITE
     chessboard.board[3][5] = chess_tree.COLOR_WHITE
     chessboard.board[3][6] = chess_tree.COLOR_WHITE
     ai = chess_tree.AI(15, chess_tree.COLOR_WHITE, 5)
     ai.go(chessboard.board)
     res = ai.candidate_list[-1]
     assert res == (3, 4)
Esempio n. 4
0
    def test4(self):
        chessboard = np.zeros((15, 15), dtype=np.int)
        chessboard[2:5, 2] = 1
        chessboard[6, 3:5] = 1
        chessboard[1, 10:12] = -1
        chessboard[2, 10] = -1
        chessboard[4, 12:14] = -1

        ai = chess_tree.AI(15, chess_tree.COLOR_BLACK, 5)
        ai.go(chessboard)
        res = ai.candidate_list[-1]
        assert res in {(5, 2)}
Esempio n. 5
0
    def test2(self):
        chessboard = np.zeros((15, 15), dtype=np.int)
        chessboard[2, 2:4] = 1
        chessboard[4, 1:3] = 1
        chessboard[1, 10:12] = -1
        chessboard[2, 10] = -1
        chessboard[4, 12] = -1

        ai = chess_tree.AI(15, chess_tree.COLOR_BLACK, 5)
        ai.go(chessboard)
        res = ai.candidate_list[-1]
        assert res in {(1, 9)}
Esempio n. 6
0
    def test5(self):
        chessboard = np.zeros((15, 15), dtype=np.int)
        chessboard[1, 3] = 1
        chessboard[2, 2] = 1
        chessboard[2, 5] = 1
        chessboard[3:5, 3] = 1
        chessboard[1, 11:13] = -1
        chessboard[2, 11:13] = -1
        chessboard[5, 13] = -1

        ai = chess_tree.AI(15, chess_tree.COLOR_BLACK, 5)
        ai.go(chessboard)
        res = ai.candidate_list[-1]
        assert res in {(2, 3)}
Esempio n. 7
0
 def test_tree_gen_live4(self):
     """
     test if it can stop player 活四
     _o_oo_
     :return:
     """
     chessboard = Board(15)
     chessboard.board[3][2] = chess_tree.COLOR_WHITE
     chessboard.board[3][4] = chess_tree.COLOR_WHITE
     chessboard.board[3][5] = chess_tree.COLOR_WHITE
     ai = chess_tree.AI(15, chess_tree.COLOR_WHITE, 5)
     ai.go(chessboard.board)
     res = ai.candidate_list[-1]
     assert res == (3, 3)
Esempio n. 8
0
 def test_tree_score_live4(self):
     """
     test if it can stop player 活四
     _000_
     :return:
     """
     chessboard = Board(15)
     chessboard.board[3][2] = chess_tree.COLOR_WHITE
     chessboard.board[3][3] = chess_tree.COLOR_WHITE
     chessboard.board[3][4] = chess_tree.COLOR_WHITE
     ai = chess_tree.AI(15, chess_tree.COLOR_BLACK, 5)
     ai.go(chessboard.board)
     res = ai.candidate_list[-1]
     assert res == (3, 6) or res == (3, 1)
Esempio n. 9
0
 def test_tree_score_terminal2(self):
     """
     test if it can stop player
     oo_oo
     :return:
     """
     chessboard = Board(15)
     chessboard.board[3][1] = chess_tree.COLOR_WHITE
     chessboard.board[3][2] = chess_tree.COLOR_WHITE
     chessboard.board[3][4] = chess_tree.COLOR_WHITE
     chessboard.board[3][5] = chess_tree.COLOR_WHITE
     ai = chess_tree.AI(15, chess_tree.COLOR_BLACK, 5)
     ai.go(chessboard.board)
     res = ai.candidate_list[-1]
     assert res == (3, 3)
Esempio n. 10
0
    def test1(self):
        chessboard = np.zeros((15, 15), dtype=np.int)
        chessboard[2, 2] = 1
        chessboard[3, 3] = 1
        chessboard[4, 4] = 1
        chessboard[5, 6] = 1
        chessboard[5, 8] = 1
        chessboard[1:3, 11] = -1
        chessboard[3, 9:11] = -1
        chessboard[6, 13] = -1

        ai = chess_tree.AI(15, chess_tree.COLOR_BLACK, 5)
        ai.go(chessboard)
        res = ai.candidate_list[-1]
        assert res in {(5, 5)}