def test_move_turn_alternate(self): a = ConnectGame() self.assertTrue(a.move_turn() == 1) b = a.move_immutable(0) self.assertTrue(b.move_turn() == 0) c = b.move_immutable(0) self.assertTrue(c.move_turn() == 1)
def test_connect_game_new_vanilla(self): a = ConnectGame() self.assertTrue(a.width == 7) self.assertTrue(a.height == 6) self.assertTrue(a.win_length == 4) self.assertTrue(len(a.board.board) == 6) self.assertTrue(len(a.board.board[0]) == 7)
def test_that_wins_dont_happen(self): num_tests = 150 zero_wins = 0 one_wins = 0 for t in range(num_tests): tmp = ConnectGame() for n in range(6): moves = tmp.move_list() move = random.choice(moves) tmp = tmp.move_immutable(move) winner = tmp.game_winner() if winner == 1: one_wins = one_wins + 1 if winner == 0: zero_wins = zero_wins + 1 self.assertTrue(zero_wins == 0) self.assertTrue(one_wins == 0)
def test_connect_game_nonnormal_moves(self): a = ConnectGame() b = a.move_immutable(0) c = b.move_immutable(0) d = c.move_immutable(0) e = d.move_immutable(0) f = e.move_immutable(0) g = f.move_immutable(0) h = g.move_list() self.assertEqual(h, [1, 2, 3, 4, 5, 6]) i = g.move_immutable(3) j = i.move_immutable(3) k = j.move_immutable(3) l = k.move_immutable(3) m = l.move_immutable(3) n = m.move_immutable(3) o = n.move_list() self.assertEqual(o, [1, 2, 4, 5, 6])
def test_dd_wants_center(self): print "Testing wants center..." for n in range(NUMBER_FOR_EACH): print "Passed " + str(n) inst = AbstractGame(ConnectGame()) distribution = MCTS(inst, iteration_number=3000) highest = max(distribution) highest_index = distribution.index(highest) self.assertTrue(highest_index == 3)
def test_random_simulation(self): total_plays = 100 win_total = 0 for n in range(total_plays): g = AbstractGame(ConnectGame()) p = random_simulation(g) if p == 1: win_total = win_total + 1 if p == 0: win_total = win_total - 1 self.assertTrue(win_total > -5) self.assertTrue(win_total < 50)
def test_cc_several_moves_ahead(self): print "Testing can look a few moves ahead..." for n in range(NUMBER_FOR_EACH): print "Passed " + str(n) inst = AbstractGame(ConnectGame()) a = inst.move_immutable(3) b = a.move_immutable(3) c = b.move_immutable(4) d = c.move_immutable(4) distribution = MCTS(d, iteration_number=700) highest = max(distribution) highest_index = distribution.index(highest) self.assertTrue(highest_index == 5 or highest_index == 2)
def test_bb_wants_block_death(self): print "Testing can block opponent victory." for n in range(NUMBER_FOR_EACH): print "Passed " + str(n) inst = AbstractGame(ConnectGame()) a = inst.move_immutable(4) b = a.move_immutable(1) c = b.move_immutable(4) d = c.move_immutable(1) e = d.move_immutable(4) distribution = MCTS(e, iteration_number=700) highest = max(distribution) highest_index = distribution.index(highest) self.assertTrue(highest_index == 4)
def test_aa_wants_wim(self): print "Testing can win." for n in range(NUMBER_FOR_EACH): print "Passed " + str(n) inst = AbstractGame(ConnectGame()) a = inst.move_immutable(4) b = a.move_immutable(1) c = b.move_immutable(4) d = c.move_immutable(1) e = d.move_immutable(4) f = e.move_immutable(1) distribution = MCTS(f, iteration_number=170) highest = max(distribution) highest_index = distribution.index(highest) self.assertTrue(highest_index == 4)
def test_that_wins_happen(self): num_tests = 50 zero_wins = 0 one_wins = 0 for t in range(num_tests): tmp = ConnectGame() while not tmp.game_over(): moves = tmp.move_list() move = random.choice(moves) tmp = tmp.move_immutable(move) winner = tmp.game_winner() if winner == 1: one_wins = one_wins + 1 if winner == 0: zero_wins = zero_wins + 1 self.assertTrue(zero_wins > num_tests * 0.25) self.assertTrue(one_wins > num_tests * 0.25)
def game_creator(): return AbstractGame(ConnectGame())
def test_connect_game_normal_moves(self): a = ConnectGame() b = a.move_list() self.assertTrue(len(b) == 7) self.assertEqual(b, [0, 1, 2, 3, 4, 5, 6])
import unittest from abstractgame import AbstractGame from connectgame import ConnectGame from agent import Agent from mcts import MCTS # # Unfortunately, I'd either need to write a gigantic # stub for AbstractGame, or do integration tests, # so for now I'm just doing integration test # agent_creators = [lambda: Agent(MCTS)] game_creators = [lambda: AbstractGame(ConnectGame())] class TestAbstractGame(unittest.TestCase): # Can make some kind of instance def test_agent_creation_new(self): for creator in agent_creators: inst = creator() self.assertTrue(True) def test_can_make_moves(self): for a_creator in agent_creators: for g_creator in game_creators: