def test_a_ttt_board_can_only_have_9_spaces(self): try: self.board = TTTBoard(8) except Exception as err: self.assertEqual('TTTBoardError', err.__class__.__name__) self.assertRegex(err.args[0], 'A TTTBoard can only be initialized with 9 spaces') else: self.fail('BoardError not thrown when it should be')
def test_board_gets_displayed_properly(self): board = TTTBoard(9) io = TerminalTTTIO() expected = "-------\n|0|1|2|\n-------\n|3|4|5|\n-------\n|6|7|8|\n-------\n" self.assertEqual(expected, io._board_to_str(board)) board.fill_space(0, 'X') board.fill_space(7, 'O') expected = "-------\n|X|1|2|\n-------\n|3|4|5|\n-------\n|6|O|8|\n-------\n" self.assertEqual(expected, io._board_to_str(board))
class TTTGameRunner(): def __init__(self): WIN_SETS = [[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6]] self.board = TTTBoard(9) self.rules = TTTGameRules(WIN_SETS) self.io = TerminalTTTIO() self.ai = ComputerPlayer() def _get_valid_human_team(self): selected_team = self.io.get_human_team(self.board) while not selected_team in self.board.valid_teams_list: self.io.invalid_team() selected_team = self.io.get_human_team(self.board) return selected_team def _make_valid_move(self): try: move_location = int(self.io.get_next_move()) self.board.fill_space(move_location, self.rules.active_team(self.board)) except Exception as e: self.io.invalid_move(e.args[0]) self._make_valid_move() def _take_turn(self, current_team): if current_team == self.human_team: self._make_valid_move() if current_team != self.human_team: self.io.thinking() move_location = self.ai.get_next_move(self.board, self.rules) self.board.fill_space(move_location, current_team) def run_game(self): self.human_team = self._get_valid_human_team() while not self.rules.is_game_over(self.board): self.io.show_board(self.board) self._take_turn(self.rules.active_team(self.board)) self.game_over() def game_over(self): self.io.show_board(self.board) winner = self.rules.get_winner(self.board) if winner == None: self.io.tie_game() if winner != None: self.io.show_winner(winner)
def __init__(self): WIN_SETS = [[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6]] self.board = TTTBoard(9) self.rules = TTTGameRules(WIN_SETS) self.io = TerminalTTTIO() self.ai = ComputerPlayer()
def setUp(self): self.board = TTTBoard(9) self.X = self.board.valid_teams_list[0] self.O = self.board.valid_teams_list[1]
class TestTTTBoard(unittest.TestCase): def setUp(self): self.board = TTTBoard(9) self.X = self.board.valid_teams_list[0] self.O = self.board.valid_teams_list[1] def test_a_ttt_board_can_only_have_9_spaces(self): try: self.board = TTTBoard(8) except Exception as err: self.assertEqual('TTTBoardError', err.__class__.__name__) self.assertRegex(err.args[0], 'A TTTBoard can only be initialized with 9 spaces') else: self.fail('BoardError not thrown when it should be') def test_a_ttt_board_is_square(self): self.assertNotEqual(None, self.board.num_rows) self.assertEqual(self.board.num_rows, self.board.num_cols) def test_an_empty_space_is_represented_by_an_empty_string(self): self.assertEqual('', self.board.EMPTY_SPACE) def test_by_default_there_are_only_two_teams_allowed_to_fill_spaces_on_this_board(self): self.assertTrue('X' in self.board.valid_teams_list) self.assertTrue('O' in self.board.valid_teams_list) def test_has_an_empty_board_by_default(self): self.assertEqual(0, len(self.board.to_dict())) def test_can_fill_spaces_on_the_board(self): self.board.fill_space(1, self.X) self.assertEqual({1: self.X}, self.board.to_dict()) self.board.fill_space(0, self.O) self.assertEqual({1: self.X, 0: self.O}, self.board.to_dict()) def test_knows_how_many_moves_have_been_made(self): self.assertEqual(0, self.board.num_full_spaces()) self.board.fill_space(1, self.X) self.assertEqual(1, self.board.num_full_spaces()) def test_can_erase_spaces_on_the_board(self): self.board.fill_space(1, self.X) self.board.erase_space(1) self.assertEqual(0, self.board.num_full_spaces()) def test_can_only_fill_space_with_X_or_O(self): try: self.board.fill_space(1, 'invalid') except Exception as err: self.assertEqual('TTTBoardError', err.__class__.__name__) self.assertRegex(err.args[0], 'Invalid Team') else: self.fail('BoardError not thrown when it should be') def test_can_only_fill_spaces_from_0_to_one_less_than_the_num_spaces_on_the_board(self): try: self.board.fill_space(-1, self.X) except Exception as err: self.assertEqual('TTTBoardError', err.__class__.__name__) self.assertRegex(err.args[0], 'Invalid Move') else: self.fail('BoardError not thrown when it should be') try: self.board.fill_space(9, self.X) except Exception as err: self.assertEqual('TTTBoardError', err.__class__.__name__) self.assertRegex(err.args[0], 'Invalid Move') else: self.fail('BoardError not thrown when it should be') try: self.board.fill_space('A', self.X) except Exception as err: self.assertEqual('TTTBoardError', err.__class__.__name__) self.assertRegex(err.args[0], 'Invalid Move') else: self.fail('BoardError not thrown when it should be') def test_can_only_fill_an_empty_space(self): space = 1 try: self.board.fill_space(space, self.X) self.board.fill_space(space, self.O) except Exception as err: self.assertEqual('TTTBoardError', err.__class__.__name__) self.assertRegex(err.args[0], "Space '{0}' is already full".format(space)) else: self.fail('BoardError not thrown when it should be') def test_can_retrieve_space_contents(self): self.assertEqual('', self.board.space_contents(0), "We haven't set the space yet") self.board.fill_space(0, self.O) self.assertEqual(self.O, self.board.space_contents(0), "After we made a move") def test_doesnt_create_a_key_val_pair_on_inspection_of_a_missing_key_like_a_regular_default_dict_does(self): self.board.space_contents(0) self.assertEqual(0, self.board.num_full_spaces()) def test_doesnt_throw_an_error_if_you_erase_a_non_existent_space(self): self.board.erase_space(0) self.assertEqual(0, self.board.num_full_spaces()) def test_can_retrieve_the_board_in_dict_form(self): self.board.fill_space(4, self.X) self.board.fill_space(2, self.O) self.assertEqual({4: self.X, 2: self.O}, self.board.to_dict()) self.board.fill_space(6, self.X) self.assertEqual({4: self.X, 2: self.O, 6: self.X}, self.board.to_dict()) def test_returns_a_list_of_all_empty_spaces(self): self.board.fill_space(4, self.X) self.board.fill_space(2, self.O) self.assertEqual([0,1,3,5,6,7,8], self.board.empty_spaces())