def test_get_previous_state(self): game = Game(4, Game.Modes.human_human, Game.DifficultyLevels.easy, True, 5) state = (game.mover.board, s.WHITE) my_state = game.get_next_state(state, (1, 0)) game.next_move('a3') self.assertEqual(game.get_current_state(), my_state) opp_state = game.get_next_state(my_state, (2, 0)) game.next_move('a2') self.assertEqual(game.get_current_state(), opp_state) my_prev_state = game.get_previous_state(opp_state, (2, 0)) self.assertEqual(my_state, my_prev_state)
class TestMonteCarloAI(unittest.TestCase): def setUp(self): self.game = Game(3, Game.Modes.human_human, Game.DifficultyLevels.hard, True, 5) self.ai = MonteCarloAI(self.game, WHITE, Game.DifficultyLevels.hard) first_move = (1, 2) first_state = Game.get_next_state((self.game.mover.board, WHITE), first_move) self.root_possible_moves = self.game.mover.board.get_moves(WHITE) self.root = Node(first_state, first_move, len(self.root_possible_moves)) def test_get_best_move(self): node = Node(self.root.state, (1, 2), 2) children = [Node(node.state, (2, 3), 1), Node(node.state, (2, 1), 2)] children[0].plays, children[0].wins = 3, 2 children[1].plays, children[1].wins = 2, 2 for child in children: node.add_child(child) self.assertEqual(self.ai.get_best_move(node), children[0].move) node.children[0].plays, node.children[0].wins = 2, 1 self.assertEqual(self.ai.get_best_move(node), children[1].move) def test_back_propagate(self): node = Node(self.root.state, (1, 2), 2) child = Node(node.state, (2, 3), 1) child.plays, child.wins = 2, 1 node.add_child(child) self.assertTrue(node.plays == 0) self.assertTrue(node.wins == 0) self.ai.back_propagate(child, 1) self.assertTrue(child.plays == 3) self.assertTrue(child.wins == 2) self.assertTrue(node.plays == 1) self.assertTrue(node.wins == 1) def test_get_best_child(self): children = [Node(self.game.get_next_state(self.root.state, (1, 2)), (1, 2), 3), Node(self.game.get_next_state(self.root.state, (2, 1)), (2, 1), 3)] children[0].plays, children[0].wins = 2, 1 children[1].plays, children[1].wins = 1, 0 self.root.plays = 3 for child in children: self.root.add_child(child) self.assertEqual(self.ai.get_best_child(self.root), children[1])
def test_get_next_state(self): game = Game(3, Game.Modes.human_human, Game.DifficultyLevels.easy, True, 5) state = (game.mover.board, s.WHITE) self.assertEqual(game.mover.board, state[0]) next_state = game.get_next_state(state, (2, 1)) self.assertNotEqual(game.mover.board, next_state[0]) game.mover.board.make_move((2, 1), s.WHITE) self.assertEqual(game.mover.board, next_state[0])
def setUp(self): self.game = Game(3, Game.Modes.human_human, Game.DifficultyLevels.easy, True, 5) first_move = (1, 2) first_state = Game.get_next_state( (self.game.mover.board, WHITE), first_move) self.root_possible_moves = self.game.mover.board.get_moves(WHITE) self.root = Node(first_state, first_move, len(self.root_possible_moves))
class TestNode(unittest.TestCase): def setUp(self): self.game = Game(3, Game.Modes.human_human, Game.DifficultyLevels.easy, True, 5) first_move = (1, 2) first_state = Game.get_next_state( (self.game.mover.board, WHITE), first_move) self.root_possible_moves = self.game.mover.board.get_moves(WHITE) self.root = Node(first_state, first_move, len(self.root_possible_moves)) def test_propagate_completion(self): root_moves_to_expand = len(self.root_possible_moves) self.assertTrue(self.root.moves_left_to_expand == root_moves_to_expand) state = self.game.get_next_state(self.root.state, (2, 1)) child = Node(state, (2, 1), 3) self.root.add_child(child) self.assertTrue(child.moves_left_to_expand > 0) child_moves_to_expand = child.moves_left_to_expand child.propagate_completion() self.assertTrue(child.moves_left_to_expand == child_moves_to_expand - 1) self.assertTrue(self.root.moves_left_to_expand == root_moves_to_expand) def test_add_child(self): child = self.get_child(self.root) self.assertFalse(self.root.children) self.root.add_child(child) self.assertTrue(len(self.root.children) == 1) self.assertEqual(self.root.children[0], child) def test_has_children(self): child = self.get_child(self.root) self.assertFalse(child.has_children()) self.root.add_child(child) self.assertTrue(self.root.has_children()) def get_child(self, node): possible_moves = self.game.mover.board.get_moves(node.state[1]) move = choice(possible_moves) state = Game.get_next_state(self.root.state, move) return Node(state, move, len(state[0].get_moves(state[1])))
def get_child(self, node): possible_moves = self.game.mover.board.get_moves(node.state[1]) move = choice(possible_moves) state = Game.get_next_state(self.root.state, move) return Node(state, move, len(state[0].get_moves(state[1])))