def q4(): import reversi_zero.lib.reversi_solver as p import spike.reversi_solver_cython as c board = ''' ########## # X XXXO# #O XXXXXX# #OOXOOOXX# #OOOOOOXO# #OOOOXOOO# #OOOOXOOX# # XXXOO # # OOO # ##########''' b, w = parse_to_bitboards(board) start_time = time() ret = p.ReversiSolver().solve(b, w, next_player=Player.black, exactly=True) print(f"{time()-start_time} sec: ret={ret}") start_time = time() ret = c.ReversiSolver().solve(b, w, next_player=1, exactly=True) print(f"{time()-start_time} sec: ret={ret}")
def q1(): import reversi_zero.lib.reversi_solver as p import spike.reversi_solver_cython as c board = ''' ########## #XXXX # #XOXX # #XOXXOOOO# #XOXOXOOO# #XOXXOXOO# #OOOOXOXO# # OOOOOOO# # XXXXXO# ##########''' b, w = parse_to_bitboards(board) print("correct is (57, +2)") start_time = time() ret = p.ReversiSolver().solve(b, w, next_player=Player.white, exactly=False) print(f"{time()-start_time} sec: ret={ret}") start_time = time() ret = c.ReversiSolver().solve(b, w, next_player=2, exactly=False) print(f"{time()-start_time} sec: ret={ret}")
def test_find_correct_move(): import spike.bitboard_cython as f import reversi_zero.lib.bitboard as b for ex in examples(): black, white = parse_to_bitboards(ex) assert f.find_correct_moves(black, white) == b.find_correct_moves( black, white) cy = timeit.timeit("f.find_correct_moves(black, white)", globals=locals(), number=10000) py = timeit.timeit("b.find_correct_moves(black, white)", globals=locals(), number=10000) print(f"Cython={cy} : cPython={py}")
def test_parse_to_bitboards(): ex = ''' ########## #OO # #XOO # #OXOOO # # XOX # # XXX # # X # # X # # X# ##########''' black, white = util.parse_to_bitboards(ex) eq_(ex.strip(), board_to_string(black, white).strip(), f"{ex}\n-------\n{board_to_string(black, white)}")
def q3(): # O: black, X: white board = ''' ########## # X OOO # #X XOXO O# #XXXXOXOO# #XOXOOXXO# #XOOOOXXO# #XOOOXXXO# # OOOOXX # # OOOOX # ##########''' b, w = parse_to_bitboards(board) rr = ReversiSolver() print("correct is (3, +2)") print(rr.solve(b, w, Player.white, exactly=True)) print(len(rr.cache))
def q2(): board = ''' ########## #XXXX # #XXXX X # #XXXXXXOO# #XXXXXXOO# #XXXXOXOO# #OXOOXOXO# # OOOOOOO# #OOOOOOOO# ##########''' b, w = parse_to_bitboards(board) rr = ReversiSolver() print("correct is (4 or 14, -2)") print(rr.solve(b, w, Player.black, exactly=False)) print(len(rr.cache))
def q1(): board = ''' ########## #XXXX # #XOXX # #XOXXOOOO# #XOXOXOOO# #XOXXOXOO# #OOOOXOXO# # OOOOOOO# # XXXXXO# ##########''' b, w = parse_to_bitboards(board) rr = ReversiSolver() print("correct is (57, +2)") print(rr.solve(b, w, Player.white, exactly=False)) print(len(rr.cache))
def test_parse_to_bitboards_init(): ex = ''' ########## # # # # # # # OX # # XO # # # # # # # ########## ''' black, white = util.parse_to_bitboards(ex) eq_(black, 0b00001000 << 24 | 0b00010000 << 32, f"{ex}\n-------\n{board_to_string(black, white)}") eq_(white, 0b00010000 << 24 | 0b00001000 << 32, f"{ex}\n-------\n{board_to_string(black, white)}")
def test_calc_flip(): import spike.bitboard_cython as f import reversi_zero.lib.bitboard as b for ex in examples(): black, white = parse_to_bitboards(ex) assert f.find_correct_moves(black, white) == b.find_correct_moves( black, white) legal_moves = f.find_correct_moves(black, white) action_list = [idx for idx in range(64) if legal_moves & (1 << idx)] for action in action_list: assert f.calc_flip(action, black, white) == b.calc_flip(action, black, white) cy = timeit.timeit("f.calc_flip(action, black, white)", globals=locals(), number=10000) py = timeit.timeit("b.calc_flip(action, black, white)", globals=locals(), number=10000) print(f"Cython={cy} : cPython={py}")
def _flip_test(ex, expect, player_black=True): b, w = parse_to_bitboards(ex) moves = find_correct_moves(b, w) if player_black else find_correct_moves( w, b) res = board_to_string(b, w, extra=moves) eq_(res.strip(), expect.strip(), f"\n{res}----{expect}")