def test_utility_with_min_winning(self): expected_utility = -1 winning_board = { (0, 0): 0, (0, 1): 0, (0, 2): 12, (0, 3): 0, (1, 0): 0, (1, 1): 0, (1, 2): 13, (1, 3): 0, (2, 0): 0, (2, 1): 0, (2, 2): 5, (2, 3): 0, (3, 0): 0, (3, 1): 0, (3, 2): 4, (3, 3): 0 } game = NumericalTicTacToe() utility = game.utility(GameState(winning_board, Max), Min) self.assertEqual(expected_utility, utility)
def test_empty_spots(self): expected_empty_spots = [(1, 0), (2, 3)] board = get_nearly_complete_board() state = GameState(board, Max) self.assertEqual(expected_empty_spots, state.empty_spots)
def test_utility_with_full_board(self): expected_utility = 0 full_board = { (0, 0): 10, (0, 1): 16, (0, 2): 12, (0, 3): 6, (1, 0): 3, (1, 1): 13, (1, 2): 11, (1, 3): 14, (2, 0): 1, (2, 1): 9, (2, 2): 15, (2, 3): 2, (3, 0): 5, (3, 1): 4, (3, 2): 7, (3, 3): 8 } game = NumericalTicTacToe() utility = game.utility(GameState(full_board, Min), Max) self.assertEqual(expected_utility, utility)
def test_result(self): expected_next_state = self.get_expected_next_state() nearly_complete_board = get_nearly_complete_board() state = GameState(nearly_complete_board, Max) game = NumericalTicTacToe() next_state = game.result(state, Action((1, 0), 3)) self.assertEqual(expected_next_state, next_state)
def test_actions_for_max(self): expected_actions = [Action((1, 0), 3), Action((2, 3), 3)] nearly_complete_board = get_nearly_complete_board() player = Max state = GameState(nearly_complete_board, player) game = NumericalTicTacToe() actions = game.actions(state) self.assertEqual(expected_actions, actions)
def test_available_numbers(self): expected_available_numbers = [3] board = get_nearly_complete_board() state = GameState(board, Max) game = NumericalTicTacToe() self.assertEqual(expected_available_numbers, game.available_numbers(state))
def get_expected_next_state(): board = { (0, 0): 10, (0, 1): 16, (0, 2): 12, (0, 3): 6, (1, 0): 3, (1, 1): 13, (1, 2): 11, (1, 3): 14, (2, 0): 1, (2, 1): 9, (2, 2): 15, (2, 3): 0, (3, 0): 5, (3, 1): 4, (3, 2): 7, (3, 3): 8 } player = Min return GameState(board, player)
def test_terminal_test_with_min_winning(self): winning_board = { (0, 0): 0, (0, 1): 0, (0, 2): 12, (0, 3): 0, (1, 0): 0, (1, 1): 0, (1, 2): 13, (1, 3): 0, (2, 0): 0, (2, 1): 0, (2, 2): 5, (2, 3): 0, (3, 0): 0, (3, 1): 0, (3, 2): 4, (3, 3): 0 } game = NumericalTicTacToe() is_over = game.terminal_test(GameState(winning_board, Max)) self.assertTrue(is_over)
def test_terminal_test_with_max_winning(self): winning_board = { (0, 0): 10, (0, 1): 16, (0, 2): 12, (0, 3): 6, (1, 0): 3, (1, 1): 11, (1, 2): 13, (1, 3): 0, (2, 0): 0, (2, 1): 9, (2, 2): 5, (2, 3): 2, (3, 0): 15, (3, 1): 7, (3, 2): 4, (3, 3): 8 } game = NumericalTicTacToe() is_over = game.terminal_test(GameState(winning_board, Min)) self.assertTrue(is_over)
def test_evaluate(self): board = { (0, 0): 2, (0, 1): 0, (0, 2): 0, (0, 3): 0, (1, 0): 0, (1, 1): 0, (1, 2): 0, (1, 3): 0, (2, 0): 0, (2, 1): 0, (2, 2): 0, (2, 3): 0, (3, 0): 0, (3, 1): 0, (3, 2): 0, (3, 3): 0 } player = Max state = GameState(board, player) game = NumericalTicTacToe(dimension=4) result = game.evaluate(state) self.assertEqual(result, 24)
def get_expected_initial_state(self): expected_initial_board = self.get_expected_initial_board() expected_initial_player = Max return GameState(expected_initial_board, expected_initial_player)
def test_equals(self): a = GameState([[1, 2], [3, 4]], Max) b = GameState([[1, 2], [3, 4]], Max) self.assertEqual(a, b)
def test_current_player(self): a = GameState([1], Max) self.assertEqual(a.player, Max)