def test_win_bonus(self): # control n_to_win = 2 agent = aba.AlphaBetaAgent("TEST_AI", 1, n_to_win) agent.player = 1 control_board = board.Board( [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], 4, 4, n_to_win) control_result = agent.win_bonus(control_board) control_expect = 0 self.assertEqual(control_result, control_expect) # test n to win n_to_win = 4 agent = aba.AlphaBetaAgent("TEST_AI", 1, n_to_win) agent.player = 1 b = board.Board( [[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0]], 4, 4, n_to_win) control_result = agent.win_bonus(b) control_expect = 500 self.assertEqual(control_result, control_expect) n_to_win = 4 agent = aba.AlphaBetaAgent("TEST_AI", 1, n_to_win) agent.player = 1 b = board.Board( [[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0]], 4, 4, n_to_win) control_result = agent.win_bonus(b) control_expect = 0 self.assertEqual(control_result, control_expect) # opponent is negative n_to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 1, n_to_win) agent.player = 1 b = board.Board( [[2, 0, 0, 0], [0, 2, 0, 0], [0, 0, 2, 0], [0, 0, 0, 0]], 4, 4, n_to_win) control_result = agent.win_bonus(b) control_expect = -800 self.assertEqual(control_result, control_expect) # bound to who wins not HOW they win n_to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 1, n_to_win) agent.player = 1 b = board.Board( [[2, 2, 2, 2], [1, 2, 1, 1], [2, 1, 2, 2], [0, 0, 0, 2]], 4, 4, n_to_win) control_result = agent.win_bonus(b) control_expect = -800 self.assertEqual(control_result, control_expect)
def test_add_to_points_list(self): agent = aba.AlphaBetaAgent("TEST_AI", 1, 4) # empty list empty_list = [] try: agent.add_to_points_list(empty_list, 4) except IndexError: self.fail("Index out of bounds on empty_list") self.assertEqual([], empty_list) # negative point value small_list = [3, 2, 1, 7] agent.add_to_points_list(small_list, -4) self.assertEqual([3, 2, 1, 7], small_list) # zero point small_list = [3, 2, 1, 7] agent.add_to_points_list(small_list, 0) self.assertEqual([3, 2, 1, 7], small_list) # add point larger than list small_list = [3, 2, 1, 7] agent.add_to_points_list(small_list, 5) self.assertEqual([3, 2, 1, 8], small_list) # add last point small_list = [3, 2, 1, 7] agent.add_to_points_list(small_list, 4) self.assertEqual([3, 2, 1, 8], small_list) # neglect points of 1 small_list = [3, 2, 1, 7] agent.add_to_points_list(small_list, 1) self.assertEqual([3, 2, 1, 7], small_list)
def test_quad_scalar(self): def q(x, to_win): return x * x * x / to_win to_win = 11 for x in range(20): agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) found = agent.quad_scalar(x) expected = q(x, to_win) to_win = to_win / 2 * -1 self.assertEqual(found, expected)
def run_test_suite(ABPlayer, depth, time, x, y, n): seed = 1234 random.seed(seed) max = 25 AlphaBetaVictories = 0 for i in range(0, 25): # # Random vs. AlphaBeta # goSecond = game.Game( x, # width y, # height n, # tokens in a row to win agent.RandomAgent("random"), # player 1 aba.AlphaBetaAgent("alphabeta", depth)) # player 2 # AlphaBeta vs Random # goFirst = game.Game( x, # width y, # height n, # tokens in a row to win aba.AlphaBetaAgent("alphabeta", depth), # player 1 agent.RandomAgent("random")) # player 2 if ABPlayer == 1: outcome = goFirst.timed_go(time) if outcome == 1: AlphaBetaVictories += 1 else: outcome = goSecond.timed_go(time) if outcome == 2: AlphaBetaVictories += 1 seed += random.randint(1, 100) #print("RANDOM SEED: " + str(seed)) random.seed(seed) print("Game " + str(i) + " complete") print("AlphaBeta won " + str(AlphaBetaVictories) + " out of " + str(max))
def test_alphabeta(self): n_to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 1, n_to_win) agent.player = 1 b = board.Board( [[2, 2, 2, 2], [1, 2, 1, 1], [2, 1, 2, 2], [0, 0, 0, 2]], 4, 4, n_to_win) mmRes = agent.minimax(b, 1, n_to_win) abRes = agent.alphabeta(b, 1, n_to_win, float('-inf'), float('inf')) self.assertEqual(mmRes, abRes) n_to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 2, n_to_win) agent.player = 1 b = board.Board( [[2, 2, 2, 2], [1, 2, 1, 1], [2, 1, 2, 2], [0, 0, 0, 2]], 4, 4, n_to_win) mmRes = agent.minimax(b, 2, n_to_win) abRes = agent.alphabeta(b, 2, n_to_win, float('-inf'), float('inf')) self.assertEqual(mmRes, abRes) n_to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 3, n_to_win) agent.player = 1 b = board.Board( [[2, 2, 2, 2], [1, 2, 1, 1], [2, 1, 2, 2], [0, 0, 0, 2]], 4, 4, n_to_win) mmRes = agent.minimax(b, 3, n_to_win) abRes = agent.alphabeta(b, 3, n_to_win, float('-inf'), float('inf')) self.assertEqual(mmRes, abRes) n_to_win = 4 agent = aba.AlphaBetaAgent("TEST_AI", 1, n_to_win) agent.player = 1 b = board.Board( [[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0]], 4, 4, n_to_win) mmRes = agent.minimax(b, 1, n_to_win) abRes = agent.alphabeta(b, 1, n_to_win, float('-inf'), float('inf')) self.assertEqual(mmRes, abRes) n_to_win = 4 agent = aba.AlphaBetaAgent("TEST_AI", 2, n_to_win) agent.player = 1 b = board.Board( [[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0]], 4, 4, n_to_win) mmRes = agent.minimax(b, 2, n_to_win) abRes = agent.alphabeta(b, 2, n_to_win, float('-inf'), float('inf')) self.assertEqual(mmRes, abRes) n_to_win = 4 agent = aba.AlphaBetaAgent("TEST_AI", 3, n_to_win) agent.player = 1 b = board.Board( [[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0]], 4, 4, n_to_win) mmRes = agent.minimax(b, 3, n_to_win) abRes = agent.alphabeta(b, 3, n_to_win, float('-inf'), float('inf')) self.assertEqual(mmRes, abRes)
def test_col_midpoint_scalar(self): def f(col, last_col): return 25 * ((-1 * col * col) + (last_col * col)) agent = aba.AlphaBetaAgent("TEST_AI", 1, 3) col = 0 last_col = 20 for _ in range(5): found = agent.col_midpoint_scalar(col, last_col) expect = f(col, last_col) self.assertEqual(found, expect) col = col + 1 last_col = last_col / 2
# # g = game.Game(7, # width # 6, # height # 4, # tokens in a row to win # agent.InteractiveAgent("human"), # player 1 # agent.RandomAgent("random")) # player 2 # # Random vs. AlphaBeta g = game.Game( 10, # width 8, # height 4, # tokens in a row to win agent.RandomAgent("random"), # player 1 aba.AlphaBetaAgent("alphabeta", 6)) # player 2 # # Human vs. AlphaBeta # #g = game.Game(10, # width # 8, # height # 5, # tokens in a row to win # agent.InteractiveAgent("human"), # player 1 # aba.AlphaBetaAgent("alphabeta", 4)) # player 2 # # Human vs. Human # # g = game.Game(7, # width # 6, # height
# # Random vs. AlphaBeta # #g = game.Game(7, # width # 6, # height # 4, # tokens in a row to win # agent.RandomAgent("random"), # player 1 # aba.AlphaBetaAgent("alphabeta", 4)) # player 2 # # Human vs. AlphaBeta # g = game.Game(10, # width 8, # height 4, # tokens in a row to win aba.AlphaBetaAgent("alphabeta1", 3), # player 1 aba.AlphaBetaAgent("alphabeta2", 3)) # player 2 # # Human vs. Human # # g = game.Game(7, # width # 6, # height # 4, # tokens in a row to win # agent.InteractiveAgent("human1"), # player 1 # agent.InteractiveAgent("human2")) # player 2 # Execute the game outcome = g.go()
scores[ps[j]] = scores[ps[j]] + s2 print("TOURNAMENT END") # Calculate and print scores sscores = sorted(((v, k.name) for k, v in scores.items()), reverse=True) print("\nSCORES:") for v, k in sscores: print(v, k) ####################### # Run the tournament! # ####################### # Set random seed for reproducibility random.seed(1) # Construct list of agents in the tournament agents = [ # aba.AlphaBetaAgent("aba", 4), aba.AlphaBetaAgent("abaThree", 6, 1, 0), aba.AlphaBetaAgent("abaFour", 8, 1) ] # Run! play_tournament( 7, # board width 6, # board height 4, # tokens in a row to win 15, # time limit in seconds agents) # player list
# Human vs. Random # # g = game.Game(7, # width # 6, # height # 4, # tokens in a row to win # agent.InteractiveAgent("human"), # player 1 # agent.RandomAgent("random")) # player 2 # # Random vs. AlphaBeta # g = game.Game( 6, # width 7, # height 4, # tokens in a row to win aba.AlphaBetaAgent("bad", 1), # player 1 aba.AlphaBetaAgent("alphabeta", 3)) # player 2 # # Human vs. AlphaBeta # # g = game.Game(7, # width # 7, # height # 4, # tokens in a row to win # agent.InteractiveAgent("human"), # player 1 # aba.AlphaBetaAgent("alphabeta", 4)) # player 2 # # Human vs. Human # # g = game.Game(7, # width
# aba.AlphaBetaAgent("alpha-beta-1-0-2-1-1", depth, 1,0,2,1,1), # agent.RandomAgent("poor-random-guy :(") # ] # agents = [ aba.AlphaBetaAgent("alpha-beta-" + str(a) + "-" + str(b) + "-" + str(c) + "-" + str(d) + "-" + str(e), depth, a, b, c, d, e) # for a in [3] # for b in range(0,4) # for c in range(1,4) # for d in [1] # for e in range(0,2) # ] d = 1 e = 1 agents = [ aba.AlphaBetaAgent( "alpha-beta-" + str(a) + "-" + str(b) + "-" + str(c) + "-" + str(d) + "-" + str(e), depth, a, b, c, d, e) for a in range(3, 4) for b in range(0, 2) for c in range(1, 3) ] agents.append(agent.RandomAgent("poor-random-guy :(")) agents.append(aba.AlphaBetaAgent("defaults", depth)) # Run! play_tournament( 6, # board width 6, # board height 4, # tokens in a row to win 15, # time limit in seconds agents) # player list
print(v, k) ####################### # Run the tournament! # ####################### # Set random seed for reproducibility for i in range(0, 1000): seed = random.randint(1, 999999999) random.seed(seed) #random.seed(1) # Construct list of agents in the tournament agents = [ aba.AlphaBetaAgent("aba", 7), agent.RandomAgent("random1"), agent.RandomAgent("random2"), agent.RandomAgent("random3"), agent.RandomAgent("random4") ] # Run! play_tournament( 7, # board width 6, # board height 4, # tokens in a row to win 15, # time limit in seconds agents) # player list
sscores = sorted(((v, k.name) for k, v in scores.items()), reverse=True) print("\nSCORES:") for v, k in sscores: print(v, k) ####################### # Run the tournament! # ####################### # Set random seed for reproducibility random.seed(1) # Construct list of agents in the tournament agents = [ aba.AlphaBetaAgent("aba4", 4), aba.AlphaBetaAgent("aba5", 5), aba.AlphaBetaAgent("aba6", 6), agent.RandomAgent("random1"), agent.RandomAgent("random2"), agent.RandomAgent("random3"), agent.RandomAgent("random4") ] # Run! play_tournament( 7, # board width 6, # board height 4, # tokens in a row to win 15, # time limit in seconds agents) # player list
def test_count_horizontal(self): # 4 x 4 with 3 to win w = 4 h = 4 to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board( [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], w, h, to_win) found = agent.count_horizontal(b, 1, to_win) expect = [[0, 0, 0], [0, 0, 0]] self.assertEqual(found, expect) # Two horizontals w = 4 h = 4 to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board( [[1, 1, 1, 0], [0, 2, 2, 0], [0, 0, 0, 0], [0, 0, 0, 0]], w, h, to_win) found = agent.count_horizontal(b, 1, to_win) expect = [[0, 0, 1], [0, 1, 0]] self.assertEqual(found, expect) # larger in a row than to_win w = 4 h = 4 to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board( [[1, 1, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], w, h, to_win) found = agent.count_horizontal(b, 1, to_win) expect = [[0, 0, 1], [0, 0, 0]] self.assertEqual(found, expect) # cointing 1s w = 4 h = 4 to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board( [[1, 2, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], w, h, to_win) found = agent.count_horizontal(b, 1, to_win) expect = [[0, 0, 0], [0, 0, 0]] self.assertEqual(found, expect) # large rows w = 8 h = 4 to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board([[1, 1, 1, 1, 0, 1, 1, 1], [0, 2, 2, 2, 0, 0, 0, 0], [0, 0, 2, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], w, h, to_win) found = agent.count_horizontal(b, 1, to_win) expect = [[0, 0, 2], [0, 1, 1]] self.assertEqual(found, expect) # change to Player2 w = 8 h = 4 to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 # NOTE: count_horizontal will not depend on agent.player b = board.Board([[1, 1, 1, 1, 0, 1, 1, 1], [0, 2, 2, 2, 0, 0, 0, 0], [0, 0, 2, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], w, h, to_win) found = agent.count_horizontal(b, 2, to_win) expect = [[0, 1, 1], [0, 0, 2]] self.assertEqual(found, expect) # only 1 column w = 1 h = 4 to_win = 4 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board([[1], [1], [2], [1]], w, h, to_win) found = agent.count_horizontal(b, 1, to_win) expect = [[0, 0, 0, 0], [0, 0, 0, 0]] self.assertEqual(found, expect) # only 1 row w = 4 h = 1 to_win = 4 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board([[1, 2, 1, 1]], w, h, to_win) found = agent.count_horizontal(b, 1, to_win) expect = [[0, 1, 0, 0], [0, 0, 0, 0]] self.assertEqual(found, expect)
# # Human vs. AlphaBeta # ''' g = game.Game(7, # width 6, # height 4, # tokens in a row to win agent.InteractiveAgent("human"), # player 1 aba.AlphaBetaAgent("alphabeta", 4)) # player 2 ''' g = game.Game(7, # width 6, # height 4, # tokens in a row to win aba.AlphaBetaAgent("alphabeta", 4), agent.InteractiveAgent("human")) # # Human vs. Human # # g = game.Game(7, # width # 6, # height # 4, # tokens in a row to win # agent.InteractiveAgent("human1"), # player 1 # agent.InteractiveAgent("human2")) # player 2 # Jacked AI vs. Jacked AI
# Human vs. Random # # g = game.Game(4, # width # 4, # height # 4, # tokens in a row to win # agent.InteractiveAgent("human"), # player 1 # agent.RandomAgent("random")) # player 2 # # Random vs. AlphaBeta # g = game.Game( 7, # width 6, # height 4, # tokens in a row to win aba.AlphaBetaAgent("alphabeta2", 4), # player 1 aba.AlphaBetaAgent("alphabeta", 4)) # player 2 # # Human vs. AlphaBeta # # g = game.Game(7, # width # 6, # height # 4, # tokens in a row to win # agent.InteractiveAgent("human"), # player 1 # aba.AlphaBetaAgent("alphabeta", 5)) # player 2 # Human vs. Human # # g = game.Game(7, # width # 6, # height
def test_num_in_a_row(self): # control n_to_win = 2 agent = aba.AlphaBetaAgent("TEST_AI", 1, n_to_win) agent.player = 1 b = board.Board( [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], 4, 4, n_to_win) result = agent.num_in_a_row(b) expect = 0 self.assertEqual(result, expect) # only vertical n_to_win = 2 agent = aba.AlphaBetaAgent("TEST_AI", 1, n_to_win) agent.player = 1 agent.quad_scalar = lambda x: x b = board.Board( [[0, 2, 0, 0], [0, 2, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], 4, 4, n_to_win) result = agent.num_in_a_row(b) expect = -2 self.assertEqual(result, expect) # same weight for both players n_to_win = 4 agent = aba.AlphaBetaAgent("TEST_AI", 1, n_to_win) agent.player = 1 agent.quad_scalar = lambda x: x b = board.Board( [[0, 2, 0, 1], [0, 2, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0]], 4, 4, n_to_win) result = agent.num_in_a_row(b) expect = 0 self.assertEqual(result, expect) n_to_win = 4 agent = aba.AlphaBetaAgent("TEST_AI", 1, n_to_win) agent.player = 1 agent.quad_scalar = lambda x: x b = board.Board( [[0, 0, 1, 1], [0, 0, 2, 2], [0, 0, 0, 0], [0, 0, 0, 0]], 4, 4, n_to_win) result = agent.num_in_a_row(b) expect = 0 self.assertEqual(result, expect) # only horizontal n_to_win = 4 agent = aba.AlphaBetaAgent("TEST_AI", 1, n_to_win) agent.player = 1 agent.quad_scalar = lambda x: x b = board.Board( [[1, 1, 1, 1], [0, 0, 2, 2], [0, 0, 0, 0], [0, 0, 0, 0]], 4, 4, n_to_win) result = agent.num_in_a_row(b) expect = 2 self.assertEqual(result, expect) # only diagnal n_to_win = 4 agent = aba.AlphaBetaAgent("TEST_AI", 1, n_to_win) agent.player = 1 agent.quad_scalar = lambda x: x b = board.Board( [[1, 0, 0, 1], [0, 1, 2, 0], [0, 2, 1, 0], [0, 0, 0, 0]], 4, 4, n_to_win) result = agent.num_in_a_row(b) expect = 1 self.assertEqual(result, expect) # mix n_to_win = 4 agent = aba.AlphaBetaAgent("TEST_AI", 1, n_to_win) agent.player = 1 agent.quad_scalar = lambda x: x b = board.Board( [[1, 2, 2, 1], [0, 1, 2, 0], [0, 2, 1, 0], [0, 0, 0, 0]], 4, 4, n_to_win) result = agent.num_in_a_row(b) expect = -5 self.assertEqual(result, expect) n_to_win = 7 agent = aba.AlphaBetaAgent("TEST_AI", 1, n_to_win) agent.player = 1 agent.quad_scalar = lambda x: x b = board.Board([[1, 2, 2, 1, 1], [0, 1, 2, 1, 2], [0, 2, 1, 0, 1], [0, 0, 0, 0, 0]], 5, 4, n_to_win) result = agent.num_in_a_row(b) expect = 4 self.assertEqual(result, expect)
def test_count_diagnal(self): # 4 x 4 with 3 to win w = 4 h = 4 to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board( [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], w, h, to_win) found = agent.count_diagnal(b, 1, to_win) expect = [[0, 0, 0], [0, 0, 0]] self.assertEqual(found, expect) # 4 x 4 with 3 to win no diagnal w = 4 h = 4 to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board( [[0, 0, 1, 1], [0, 0, 2, 2], [0, 0, 0, 0], [0, 0, 0, 0]], w, h, to_win) found = agent.count_diagnal(b, 1, to_win) expect = [[0, 0, 0], [0, 0, 0]] self.assertEqual(found, expect) # count top-left to bot-right w = 4 h = 4 to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board( [[1, 2, 2, 0], [0, 1, 2, 0], [0, 0, 1, 0], [0, 0, 0, 0]], w, h, to_win) found = agent.count_diagnal(b, 1, to_win) expect = [[0, 0, 1], [0, 1, 0]] self.assertEqual(found, expect) # count top-right to bot-left w = 4 h = 4 to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board( [[0, 2, 1, 0], [2, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 0]], w, h, to_win) found = agent.count_diagnal(b, 1, to_win) expect = [[0, 0, 1], [0, 1, 0]] self.assertEqual(found, expect) # mix of both 4 x 4 w = 4 h = 4 to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board( [[1, 2, 1, 0], [2, 1, 2, 0], [1, 0, 0, 0], [0, 0, 0, 0]], w, h, to_win) found = agent.count_diagnal(b, 1, to_win) expect = [[0, 1, 1], [0, 2, 0]] self.assertEqual(found, expect) # mix of both 4 x 1 w = 4 h = 1 to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board([[1, 2, 1, 0]], w, h, to_win) found = agent.count_diagnal(b, 1, to_win) expect = [[0, 0, 0], [0, 0, 0]] self.assertEqual(found, expect) # mix of both 1 x 4 w = 1 h = 4 to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board([[1], [2], [2], [1]], w, h, to_win) found = agent.count_diagnal(b, 1, to_win) expect = [[0, 0, 0], [0, 0, 0]] self.assertEqual(found, expect) # mix of both 4 x 4 with 4 to win w = 4 h = 4 to_win = 4 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board( [[1, 2, 1, 0], [2, 1, 2, 0], [1, 0, 0, 0], [0, 0, 0, 0]], w, h, to_win) found = agent.count_diagnal(b, 1, to_win) expect = [[0, 1, 1, 0], [0, 2, 0, 0]] self.assertEqual(found, expect) # mix of both 4 x 4 with 4 to win w = 4 h = 4 to_win = 4 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board( [[1, 2, 1, 0], [2, 1, 2, 0], [1, 2, 2, 0], [0, 0, 2, 0]], w, h, to_win) found = agent.count_diagnal(b, 1, to_win) expect = [[0, 1, 1, 0], [0, 3, 1, 0]] self.assertEqual(found, expect) # mix of both 4 x 4 with 6 to win w = 6 h = 3 to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board( [[1, 2, 2, 1, 2, 2], [0, 1, 1, 2, 1, 0], [0, 0, 0, 1, 0, 0]], w, h, to_win) found = agent.count_diagnal(b, 1, to_win) expect = [[0, 5, 0], [0, 2, 0]] self.assertEqual(found, expect) # change AI to player2 w = 6 h = 3 to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 2 b = board.Board( [[1, 2, 2, 1, 2, 2], [0, 1, 1, 2, 1, 0], [0, 0, 0, 1, 0, 0]], w, h, to_win) found = agent.count_diagnal(b, 2, to_win) expect = [[0, 2, 0], [0, 5, 0]] self.assertEqual(found, expect)
def test_count_vertical(self): # 4 x 4 with 3 to win w = 4 h = 4 to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board( [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], w, h, to_win) found = agent.count_vertical(b, 1, to_win) expect = [[0, 0, 0], [0, 0, 0]] self.assertEqual(found, expect) # two vertical w = 4 h = 4 to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board( [[1, 0, 2, 0], [1, 0, 2, 0], [1, 0, 2, 0], [0, 0, 0, 0]], w, h, to_win) found = agent.count_vertical(b, 1, to_win) expect = [[0, 0, 1], [0, 0, 1]] self.assertEqual(found, expect) # multi verticals with larger than N to win w = 4 h = 4 to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board( [[1, 1, 2, 0], [1, 1, 2, 0], [1, 2, 2, 0], [1, 2, 2, 0]], w, h, to_win) found = agent.count_vertical(b, 1, to_win) expect = [[0, 1, 1], [0, 1, 1]] self.assertEqual(found, expect) # change AI to player2 w = 4 h = 4 to_win = 3 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 2 b = board.Board( [[1, 1, 2, 0], [1, 1, 2, 0], [1, 2, 2, 0], [1, 2, 2, 0]], w, h, to_win) found = agent.count_vertical(b, 2, to_win) expect = [[0, 1, 1], [0, 1, 1]] self.assertEqual(found, expect) # 4 to win w = 4 h = 4 to_win = 4 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board( [[1, 1, 2, 1], [1, 1, 2, 1], [1, 1, 2, 1], [1, 2, 2, 1]], w, h, to_win) found = agent.count_vertical(b, 1, to_win) expect = [[0, 0, 1, 2], [0, 0, 0, 1]] self.assertEqual(found, expect) # do NOT allow 1-in-a-row w = 2 h = 4 to_win = 4 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board([[1, 2], [2, 1], [1, 2], [2, 1]], w, h, to_win) found = agent.count_vertical(b, 1, to_win) expect = [[0, 0, 0, 0], [0, 0, 0, 0]] self.assertEqual(found, expect) # only 1 column w = 1 h = 4 to_win = 4 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board([[1], [1], [1], [1]], w, h, to_win) found = agent.count_vertical(b, 1, to_win) expect = [[0, 0, 0, 1], [0, 0, 0, 0]] self.assertEqual(found, expect) # only 1 row w = 4 h = 1 to_win = 4 agent = aba.AlphaBetaAgent("TEST_AI", 1, to_win) agent.player = 1 b = board.Board([[1, 2, 1, 1]], w, h, to_win) found = agent.count_vertical(b, 1, to_win) expect = [[0, 0, 0, 0], [0, 0, 0, 0]] self.assertEqual(found, expect)
import random import game import agent import alpha_beta_agent as aba # Set random seed for reproducibility # random.seed(1) games = [] games.append( game.Game(7, 6, 4, agent.InteractiveAgent("random"), aba.AlphaBetaAgent("alphabeta"))) # games.append(game.Game(7, 6, 5, aba.AlphaBetaAgent("alphabeta"), agent.RandomAgent("random"))) # games.append(game.Game(10, 8, 4, aba.AlphaBetaAgent("alphabeta"), agent.RandomAgent("random"))) # games.append(game.Game(10, 8, 5, aba.AlphaBetaAgent("alphabeta"), agent.RandomAgent("random"))) # for i in range(96): # width = random.randint(7, 10) # height = random.randint(6, 8) # token = random.randint(4, 5) # randomPlayer = random.randint(1, 2) # if randomPlayer == 1: # games.append(game.Game(width, height, token, agent.RandomAgent("random"), aba.AlphaBetaAgent("alphabeta"))) # else: # games.append(game.Game(width, height, token, aba.AlphaBetaAgent("alphabeta"), agent.RandomAgent("random"))) # Execute the game wins = 0 ties = 0 game_num = 0 max_times = [] for game in games: game_num += 1
# Calculate and print scores sscores = sorted(((v, k.name) for k, v in scores.items()), reverse=True) print("\nSCORES:") for v, k in sscores: print(v, k) ####################### # Run the tournament! # ####################### # Set random seed for reproducibility random.seed(1) # Construct list of agents in the tournament agents = [ aba.AlphaBetaAgent("aba", 4), agent.RandomAgent("random1"), agent.RandomAgent("random2"), agent.RandomAgent("random3"), agent.RandomAgent("random4") ] # Run! play_tournament( 7, # board width 6, # board height 4, # tokens in a row to win 15, # time limit in seconds agents) # player list
####################### # Run the tournament! # ####################### # Set random seed for reproducibility random.seed(1) # GAME CONFIGURATION depth = 4 tokens_to_win = 4 time_limit = 15 for i in range(1): random.seed(i) # Run! play_tournament( 7, # board width 6, # board height tokens_to_win, # tokens in a row to win time_limit, # time limit in seconds [ aba.AlphaBetaAgent("New AI", depth, tokens_to_win), oaba.OldAlphaBetaAgent("Old AI", depth, tokens_to_win), agent.RandomAgent("random1"), # agent.RandomAgent("random2"), # agent.RandomAgent("random3"), # agent.RandomAgent("random4"), ]) # player list
print(v,k) ####################### # Run the tournament! # ####################### # Set random seed for reproducibility random.seed(1) # Construct list of agents in the tournament agents = [ # aba.AlphaBetaAgent("aba", 4), agent.RandomAgent("random1"), agent.RandomAgent("random2"), agent.RandomAgent("random3"), agent.RandomAgent("random4"), agent.RandomAgent("random5"), #agent.RandomAgent("random6"), #agent.RandomAgent("random7"), #agent.RandomAgent("random8"), aba.AlphaBetaAgent("BOOM", 2, .77, 1), ] # Run! play_tournament(8, # board width 10, # board height 5, # tokens in a row to win 15, # time limit in seconds agents) # player list