def test_array_bin_array(): array = [ [0, 1, 2, 2, 2, 2], [2, 2, 2, 2, 2, 2], [2, 2, 2, 2, 2, 2], [2, 2, 2, 2, 2, 2], [2, 2, 2, 2, 2, 2], [2, 2, 2, 2, 1, 0], ] bin_rep = 0b010000000000000000000000000000000010100000000000000000000000000000000001 b = array_to_bin(array, empty=2) assert b == bin_rep # binary to array test assert bin_to_array(b, empty=2) == array
def time_moves(): array = [[random.choice((None, 0, 1)) for _ in range(6)] for _ in range(6)] quarter_masks = generate_quarter_masks() b = array_to_bin(array) rotation_map = generate_rotation_map() def rotate_binboard(b, q, clockwise): quarter_mask = quarter_masks[q] quarter_binboard = b & quarter_mask hash_ = quarter_binboard | (clockwise << 72) rotated_quarter_binboard = rotation_map[hash_] rotated_binboard = b & (~quarter_mask) | rotated_quarter_binboard return rotated_binboard t0 = time.clock() number = 10000 for i in range(number): rotate_binboard(b, 1, 1) b1 = b | (1 << (10)) rotate_binboard(b, 1, 0) b1 = b | (1 << (22 + 36)) rotate_binboard(b, 2, 1) b1 = b | (1 << (22 + 36)) rotate_binboard(b, 2, 0) b1 = b | (1 << (22)) rotate_binboard(b, 3, 0) b1 = b | (1 << (11)) rotate_binboard(b, 3, 1) b1 = b | (1 << (11 + 36)) rotate_binboard(b, 0, 0) b1 = b | (1 << (1)) rotate_binboard(b, 0, 1) b1 = b | (1 << (1 + 36)) t1 = time.clock() dt = t1 - t0 print("%i board operations in %.3fs." % (number * 8, dt))
def make_state_test(): # 1 game = PentagoGame() state = game.make_state() assert state.num_moves == 0 # 2 array = [ [0, 1, 1, 1, None, 1], [None, 0, None, None, None, None], [None, 1, 0, None, None, None], [None, 1, None, 0, None, None], [None, 1, None, None, None, None], [None, 1, None, 0, None, None], ] b = array_to_bin(array) state = game.make_state(b=b) assert state.num_moves == 13
def time_utility(): array = [ [0, 1, 1, 1, None, 1], [None, 0, None, None, None, None], [None, 1, 0, None, None, None], [None, 1, None, 0, None, None], [None, 1, None, None, None, None], [None, 1, None, 0, None, None], ] b = array_to_bin(array) number = 100000 utility = game.utility t0 = time.clock() for i in range(number): game.utility(b, 0) t1 = time.clock() print('%s repetition in %.3fs.' % (number, t1 - t0))
def utility_test(win_threshold=60000, not_win_threshold=40000): tests = [ [ [ [0, None, 0, 0, 0, 0], [None, 1, None, None, None, None], [None, None, 1, None, None, None], [None, None, None, 1, None, None], [None, None, None, None, 1, None], [None, None, None, None, None, None], ], (-1000, not_win_threshold) ], [ [ [0, None, None, None, None, None], [None, 0, None, None, None, None], [None, None, 0, None, None, None], [None, None, None, 0, None, None], [None, None, None, None, 0, None], [None, None, None, None, None, 0], ], (win_threshold, 1000000) ], [ [ [None, None, None, None, None, None], [0, None, None, None, None, None], [None, 0, None, None, None, None], [None, None, 0, None, None, None], [None, None, None, 0, None, None], [None, None, None, None, 0, None], ], (win_threshold, 1000000) ], [ [ [None, None, None, None, None, 0], [None, None, None, None, 0, None], [None, None, None, 0, None, None], [None, None, 0, None, None, None], [None, 0, None, None, None, None], [None, None, None, None, None, None], ], (win_threshold, 1000000) ], [ [ [None, None, None, None, None, 0], [None, None, None, None, 0, None], [None, None, None, 0, None, None], [None, None, 0, None, None, None], [None, 0, None, None, None, None], [0, None, None, None, None, None], ], (win_threshold, 1000000) ], [ [ [1, 1, 1, 1, 1, 0], [None, None, None, None, 0, None], [None, None, None, 0, None, None], [None, None, 0, None, None, None], [None, 0, None, None, None, None], [0, None, None, None, None, None], ], (0, win_threshold * 2) ], [ [ [None, None, None, None, None, None], [None, None, None, None, 0, None], [None, None, None, 0, None, None], [None, None, 0, None, None, None], [None, 0, None, None, None, None], [1, None, None, None, None, None], ], (0, not_win_threshold) ], [ [ [None, 1, 0, None, 0, 0], [0, None, 1, None, None, None], [None, 0, None, 1, 0, 0], [None, None, None, None, 1, None], [None, None, None, 1, None, 1], [None, None, None, None, None, None], ], (-10000000, -win_threshold) ], [ [ [0, 0, 0, 0, 0, None], [None, None, None, None, None, None], [1, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None] ], (win_threshold, 100000000000) ], [ [ [0, 0, 0, None, 0, 0], [None, None, None, None, None, None], [1, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None] ], (0, not_win_threshold) ], ] for i, (array, (minv, maxv)) in enumerate(tests): b = array_to_bin(array) ut = game.utility(b, 0) if not minv < ut < maxv: print("Utility Test %s failed." % i) pprint(array) print("%.2f" % ut)