def setUpClass(self): # # 0 1 2 3 4 5 6 7 # # grid = (' . . . . . . . . ' # 0 ' . . . . . . . . ' # 1 ' . . . . . . . . ' # 2 ' . . . ● ○ . . . ' # 3 ' . . . ○ ● . . . ' # 4 ' . . . . . . . . ' # 5 ' . . . . . . . . ' # 6 ' . . . . . . . . ') #7 self.grid_dark = Reversi(hash_grid(to_grid(grid), player=0)) self.grid_light = Reversi(hash_grid(to_grid(grid), player=1))
def assertReconstructs(self, expected, expd_player, grid_hash): grid, player = Reversi.reconstruct(grid_hash) self.assertEqual(expected, grid) self.assertEqual(expd_player, player)
class IsValidActionTest(unittest.TestCase): @classmethod def setUpClass(self): # # 0 1 2 3 4 5 6 7 # # grid = (' . . . . . . . . ' # 0 ' . . . . . . . . ' # 1 ' . . . . . . . . ' # 2 ' . . . ● ○ . . . ' # 3 ' . . . ○ ● . . . ' # 4 ' . . . . . . . . ' # 5 ' . . . . . . . . ' # 6 ' . . . . . . . . ') #7 self.grid_dark = Reversi(hash_grid(to_grid(grid), player=0)) self.grid_light = Reversi(hash_grid(to_grid(grid), player=1)) def assertValidActionDark(self, action): self.assertTrue(self.grid_dark.is_valid_action(action)) def test_valid_dark_action_1(self): self.assertValidActionDark(action=(2, 3)) def test_valid_dark_action_2(self): self.assertValidActionDark(action=(3, 2)) def test_valid_dark_action_3(self): self.assertValidActionDark(action=(4, 5)) def test_valid_dark_action_4(self): self.assertValidActionDark(action=(5, 4)) def assertValidActionLight(self, action): self.assertTrue(self.grid_light.is_valid_action(action)) def test_valid_light_action_1(self): self.assertValidActionLight(action=(2, 4)) def test_valid_light_action_2(self): self.assertValidActionLight(action=(3, 5)) def test_valid_light_action_3(self): self.assertValidActionLight(action=(4, 2)) def test_valid_light_action_4(self): self.assertValidActionLight(action=(5, 3)) def assertInvalidActions(self, remaining): """Assert that the queue `remaining` contains only invalid actions""" if len(remaining) == 0: return action = remaining.pop(0) fail_message = '{} (Not tested: {})'.format(action, remaining) self.assertFalse(self.grid_dark.is_valid_action(action), fail_message) self.assertFalse(self.grid_light.is_valid_action(action), fail_message) self.assertInvalidActions(remaining) def test_no_other_action_is_valid(self): """Test that no other action is valid for ANY of the players.""" valid = [(2, 4), (3, 5), (4, 2), (5, 3), (2, 3), (3, 2), (4, 5), (5, 4)] rows, cols = Reversi.grid_size action_queue = list() for row in range(rows): for col in range(cols): if (row, col) not in valid: action_queue.append((row, col)) self.assertInvalidActions(action_queue)
def assertLight(self, expected, grid_str, player=1): game_state = Reversi(hash_grid(to_grid(grid_str), player)) self.assertEqual(expected, game_state.find_actions())
def assertValue(self, expected, grid_str): game_state = Reversi(hash_grid(to_grid(grid_str))) self.assertEqual(expected, game_state.utility())
def assertResultsIn(self, expd_str, expd_player, grid_str, player, move): game_state_before = Reversi(hash_grid(to_grid(grid_str), player)) game_state_after = Reversi(hash_grid(to_grid(expd_str), expd_player)) result_state = Reversi(game_state_before.result(move)) self.assertEqual(result_state.grid, game_state_after.grid) self.assertEqual(result_state.player, expd_player)