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 setUp(self): self.comp_player = ComputerPlayer() self.board = TestBoard(9) self.ttt_rules = TTTGameRules(WIN_SETS) self.X = self.board.valid_teams_list[0] self.O = self.board.valid_teams_list[1]
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.rules = TTTGameRules(WIN_SETS) self.board = TestBoard(9) self.X = self.board.valid_teams_list[0] self.O = self.board.valid_teams_list[1]
class TestTTTGameRules(unittest.TestCase): def setUp(self): self.rules = TTTGameRules(WIN_SETS) self.board = TestBoard(9) self.X = self.board.valid_teams_list[0] self.O = self.board.valid_teams_list[1] def test_game_ends_if_the_board_is_full(self): self.assertEqual(False, self.rules.is_game_over(self.board)) self.board.set_board({0: self.X, 1: self.O, 2: self.X, 3: self.O, 4: self.X, 5: self.O, 6: self.X, 7: self.O, 8: self.X}) self.assertEqual(True, self.rules.is_game_over(self.board)) def test_game_ends_if_a_team_controlls_a_win_set(self): self.assertEqual(False, self.rules.is_game_over(self.board)) self.board.set_board({0: self.X, 1: self.X, 2: self.X}) self.assertEqual(True, self.rules.is_game_over(self.board)) self.board.set_board({2: self.O, 4: self.O, 6: self.O}) self.assertEqual(True, self.rules.is_game_over(self.board)) def test_either_one_team_wins_or_no_teams_win(self): self.assertEqual(None, self.rules.get_winner(self.board)) self.board.set_board({0: self.X, 1: self.X, 2: self.X}) self.assertEqual(self.X, self.rules.get_winner(self.board)) self.board.set_board({2: self.O, 4: self.O, 6: self.O}) self.assertEqual(self.O, self.rules.get_winner(self.board)) def test_X_gets_the_first_turn(self): self.assertEqual(self.X, self.rules.active_team(self.board)) def test_X_and_O_alternate_turns(self): self.board.set_board({1: self.X}) self.assertEqual(self.O, self.rules.active_team(self.board)) self.board.set_board({1: self.X, 5: self.O}) self.assertEqual(self.X, self.rules.active_team(self.board)) def test_there_is_no_active_team_if_the_game_is_over(self): self.board.set_board({0: self.X, 1: self.O, 2: self.X, 3: self.O, 4: self.X, 5: self.O, 6: self.X, 7: self.O, 8: self.X}) self.assertEqual(None, self.rules.active_team(self.board)) def test_knows_all_available_moves(self): self.board.set_board({1: self.X}) self.assertEqual([0,2,3,4,5,6,7,8], self.rules.possible_moves(self.board)) self.board.set_board({1: self.X, 5: self.O}) self.assertEqual([0,2,3,4,6,7,8], self.rules.possible_moves(self.board))
class TestTTTGameRules(unittest.TestCase): def setUp(self): self.rules = TTTGameRules(WIN_SETS) self.board = TestBoard(9) self.X = self.board.valid_teams_list[0] self.O = self.board.valid_teams_list[1] def test_game_ends_if_the_board_is_full(self): self.assertEqual(False, self.rules.is_game_over(self.board)) self.board.set_board({ 0: self.X, 1: self.O, 2: self.X, 3: self.O, 4: self.X, 5: self.O, 6: self.X, 7: self.O, 8: self.X }) self.assertEqual(True, self.rules.is_game_over(self.board)) def test_game_ends_if_a_team_controlls_a_win_set(self): self.assertEqual(False, self.rules.is_game_over(self.board)) self.board.set_board({0: self.X, 1: self.X, 2: self.X}) self.assertEqual(True, self.rules.is_game_over(self.board)) self.board.set_board({2: self.O, 4: self.O, 6: self.O}) self.assertEqual(True, self.rules.is_game_over(self.board)) def test_either_one_team_wins_or_no_teams_win(self): self.assertEqual(None, self.rules.get_winner(self.board)) self.board.set_board({0: self.X, 1: self.X, 2: self.X}) self.assertEqual(self.X, self.rules.get_winner(self.board)) self.board.set_board({2: self.O, 4: self.O, 6: self.O}) self.assertEqual(self.O, self.rules.get_winner(self.board)) def test_X_gets_the_first_turn(self): self.assertEqual(self.X, self.rules.active_team(self.board)) def test_X_and_O_alternate_turns(self): self.board.set_board({1: self.X}) self.assertEqual(self.O, self.rules.active_team(self.board)) self.board.set_board({1: self.X, 5: self.O}) self.assertEqual(self.X, self.rules.active_team(self.board)) def test_there_is_no_active_team_if_the_game_is_over(self): self.board.set_board({ 0: self.X, 1: self.O, 2: self.X, 3: self.O, 4: self.X, 5: self.O, 6: self.X, 7: self.O, 8: self.X }) self.assertEqual(None, self.rules.active_team(self.board)) def test_knows_all_available_moves(self): self.board.set_board({1: self.X}) self.assertEqual([0, 2, 3, 4, 5, 6, 7, 8], self.rules.possible_moves(self.board)) self.board.set_board({1: self.X, 5: self.O}) self.assertEqual([0, 2, 3, 4, 6, 7, 8], self.rules.possible_moves(self.board))