コード例 #1
0
ファイル: test_game.py プロジェクト: IvyBrandyn/tic-tac-toe
 def test_upward_diagonal_win(self):
     o_token = OToken()
     grid = Grid(3, 3)
     grid_controller = GridController()
     grid_controller.assign_grid(grid)
     grid.add_token_position(o_token, 0, 2)
     grid.add_token_position(o_token, 1, 1)
     grid.add_token_position(o_token, 2, 0)
     winner = grid_controller.check_for_win()
     assert winner == o_token
コード例 #2
0
ファイル: test_game.py プロジェクト: IvyBrandyn/tic-tac-toe
 def test_downward_diagonal_win(self):
     x_token = XToken()
     grid = Grid(3, 3)
     grid_controller = GridController()
     grid_controller.assign_grid(grid)
     grid.add_token_position(x_token, 0, 0)
     grid.add_token_position(x_token, 1, 1)
     grid.add_token_position(x_token, 2, 2)
     winner = grid_controller.check_for_win()
     assert winner == x_token
コード例 #3
0
ファイル: test_game.py プロジェクト: IvyBrandyn/tic-tac-toe
 def test_horizontal_win(self):
     x_token = XToken()
     grid = Grid(3, 3)
     grid_controller = GridController()
     grid_controller.assign_grid(grid)
     grid.add_token_position(x_token, 0, 0)
     grid.add_token_position(x_token, 0, 1)
     grid.add_token_position(x_token, 0, 2)
     winner = grid_controller.check_for_win()
     assert winner == x_token
コード例 #4
0
 def game_won(
     self,
     player_controllers: List[PlayerController],
     grid_controller: GridController,
 ) -> bool:
     winning_token = grid_controller.check_for_win()
     if winning_token is not None:
         for player_controller in player_controllers:
             if player_controller.retrieve_token() == winning_token:
                 player_controller.print_player()
                 print(data.WINNING_MESSAGE)
                 self.__game.game_over = True
                 return True
     else:
         return False
コード例 #5
0
ファイル: test_game.py プロジェクト: IvyBrandyn/tic-tac-toe
    def test_entire_game_with_two_AI(self):
        new_game = Game()
        new_game.game_mode = 2
        token_1 = XToken()
        token_2 = OToken()
        tokens = (token_1, token_2)
        new_game.add_token_options(tokens)

        game_controller = GameController()
        game_controller.assign_game(new_game)

        player_one = Player()
        player_two = Player()
        players = [player_one, player_two]
        game_controller.add_players_to_game(players)

        player_one_controller = AIPlayerController()
        player_two_controller = AIPlayerController()
        player_one_controller.assign_player(player_one)
        player_two_controller.assign_player(player_two)
        player_controllers = [player_one_controller, player_two_controller]

        player_one_controller.determine_name()
        player_two_controller.determine_name()

        random.shuffle(player_controllers)

        game_controller.assign_tokens(player_controllers)

        new_game.print_players()

        grid = Grid(3, 3)
        grid_controller = GridController()
        grid_controller.assign_grid(grid)
        while not new_game.game_over:
            game_controller.play_round(player_controllers, grid_controller)
コード例 #6
0
 def play_round(
     self,
     player_controllers: List[PlayerController],
     grid_controller: GridController,
 ):
     for player_controller in player_controllers:
         row = None
         column = None
         while not grid_controller.check_if_position_available(row, column):
             grid_controller.print_grid_with_prompt()
             if self.game_won(player_controllers, grid_controller):
                 return
             if grid_controller.grid_is_full():
                 self.__game.game_over = True
                 return
             player_controller.print_player()
             rows, columns = grid_controller.get_grid_dimensions()
             row = player_controller.select_row(rows)
             column = player_controller.select_column(columns)
         player_token = player_controller.retrieve_token()
         grid_controller.place_token_in_spot(player_token, row, column)
コード例 #7
0
ファイル: test_game.py プロジェクト: IvyBrandyn/tic-tac-toe
 def test_no_winner_board_full(self):
     x_token = XToken()
     o_token = OToken()
     grid = Grid(3, 3)
     grid_controller = GridController()
     grid_controller.assign_grid(grid)
     grid.add_token_position(x_token, 0, 0)
     grid.add_token_position(x_token, 0, 1)
     grid.add_token_position(x_token, 1, 2)
     grid.add_token_position(x_token, 2, 0)
     grid.add_token_position(x_token, 2, 1)
     grid.add_token_position(o_token, 0, 2)
     grid.add_token_position(o_token, 1, 0)
     grid.add_token_position(o_token, 1, 1)
     grid.add_token_position(o_token, 2, 2)
     winner = grid_controller.check_for_win()
     assert winner is None
     assert grid_controller.grid_is_full()
コード例 #8
0
            player_two_controller = AIPlayerController()
        else:
            player_one_controller = HumanPlayerController()
            player_two_controller = HumanPlayerController()
        player_one_controller.assign_player(player_one)
        player_two_controller.assign_player(player_two)
        player_controllers = [player_one_controller, player_two_controller]

        print(data.PLAYER_ONE_FIRST_MESSAGE)
        player_one_controller.determine_name()
        print(data.PLAYER_TWO_NEXT_MESSAGE)
        player_two_controller.determine_name()

        print(data.RANDOMLY_SELECT_FIRST_PLAYER)
        random.shuffle(player_controllers)

        game_controller.assign_tokens(player_controllers)

        new_game.print_players()

        grid = Grid(data.GRID_ROWS, data.GRID_COLUMNS)
        grid_controller = GridController()
        grid_controller.assign_grid(grid)

        while not new_game.game_over:
            game_controller.play_round(player_controllers, grid_controller)

        play_again = input(data.PLAY_AGAIN_MESSAGE)
        if str.lower(play_again) not in ["yes", "y"]:
            break