From 7793db1dcf9860ad1cf62611a9d78d6e77dea13e Mon Sep 17 00:00:00 2001 From: Shailesh Tripathi <45033205+shailesht003@users.noreply.github.com> Date: Sun, 18 Jan 2026 15:54:14 -0500 Subject: [PATCH 1/2] AI-generated implementation for Create a Tic Tac Toe Game using HTML, CSS --- crm_5_implementation.py | 524 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 524 insertions(+) create mode 100644 crm_5_implementation.py diff --git a/crm_5_implementation.py b/crm_5_implementation.py new file mode 100644 index 0000000..7b55a46 --- /dev/null +++ b/crm_5_implementation.py @@ -0,0 +1,524 @@ +""" +Tic Tac Toe Game Implementation + +This module provides a complete implementation of the Tic Tac Toe game +with HTML, CSS, and JavaScript components. The game follows MVC architecture +and includes proper state management, win condition checking, and player turn handling. +""" + +from enum import Enum +from typing import List, Optional, Tuple, Dict, Any +import json + + +class Player(Enum): + """Enumeration for game players.""" + X = "X" + O = "O" + EMPTY = " " + + +class GameStatus(Enum): + """Enumeration for game statuses.""" + PLAYING = "playing" + X_WON = "x_won" + O_WON = "o_won" + DRAW = "draw" + + +class TicTacToeGame: + """ + Main game controller for Tic Tac Toe implementation. + + This class manages the game state, player turns, win condition checking, + and maintains the game board. + """ + + def __init__(self) -> None: + """ + Initialize the Tic Tac Toe game. + + Sets up the game board, initializes player turns, and prepares + the game state for play. + """ + self.board: List[List[Player]] = [ + [Player.EMPTY, Player.EMPTY, Player.EMPTY], + [Player.EMPTY, Player.EMPTY, Player.EMPTY], + [Player.EMPTY, Player.EMPTY, Player.EMPTY] + ] + self.current_player: Player = Player.X + self.game_status: GameStatus = GameStatus.PLAYING + self.move_history: List[Tuple[int, int]] = [] + + def make_move(self, row: int, col: int) -> bool: + """ + Make a move on the game board at specified position. + + Args: + row (int): The row index (0-2) where the move should be made + col (int): The column index (0-2) where the move should be made + + Returns: + bool: True if the move was successful, False otherwise + + Raises: + ValueError: If row or col are outside valid range (0-2) + """ + if not (0 <= row <= 2 and 0 <= col <= 2): + raise ValueError("Row and column must be between 0 and 2") + + if self.board[row][col] != Player.EMPTY: + return False + + # Make the move + self.board[row][col] = self.current_player + self.move_history.append((row, col)) + + # Check for win or draw + self._check_game_status() + + # Switch player if game is still playing + if self.game_status == GameStatus.PLAYING: + self.current_player = Player.O if self.current_player == Player.X else Player.X + + return True + + def _check_game_status(self) -> None: + """ + Check the current game status for win conditions or draw. + + This method evaluates the board state to determine if there is a winner + or if the game has ended in a draw. + """ + # Check rows + for row in self.board: + if row[0] == row[1] == row[2] != Player.EMPTY: + self.game_status = GameStatus.X_WON if row[0] == Player.X else GameStatus.O_WON + return + + # Check columns + for col in range(3): + if self.board[0][col] == self.board[1][col] == self.board[2][col] != Player.EMPTY: + self.game_status = GameStatus.X_WON if self.board[0][col] == Player.X else GameStatus.O_WON + return + + # Check diagonals + if self.board[0][0] == self.board[1][1] == self.board[2][2] != Player.EMPTY: + self.game_status = GameStatus.X_WON if self.board[0][0] == Player.X else GameStatus.O_WON + return + + if self.board[0][2] == self.board[1][1] == self.board[2][0] != Player.EMPTY: + self.game_status = GameStatus.X_WON if self.board[0][2] == Player.X else GameStatus.O_WON + return + + # Check for draw + is_board_full = all( + self.board[row][col] != Player.EMPTY + for row in range(3) + for col in range(3) + ) + + if is_board_full: + self.game_status = GameStatus.DRAW + + def reset_game(self) -> None: + """ + Reset the game to initial state. + + Clears the board, resets player turns, and sets game status back to playing. + """ + self.board = [ + [Player.EMPTY, Player.EMPTY, Player.EMPTY], + [Player.EMPTY, Player.EMPTY, Player.EMPTY], + [Player.EMPTY, Player.EMPTY, Player.EMPTY] + ] + self.current_player = Player.X + self.game_status = GameStatus.PLAYING + self.move_history.clear() + + def get_board_state(self) -> List[List[Player]]: + """ + Get the current state of the game board. + + Returns: + List[List[Player]]: A copy of the current board state + """ + return [row[:] for row in self.board] + + def get_current_player(self) -> Player: + """ + Get the current player making a move. + + Returns: + Player: The current player (X or O) + """ + return self.current_player + + def get_game_status(self) -> GameStatus: + """ + Get the current game status. + + Returns: + GameStatus: The current status of the game (playing, won, draw) + """ + return self.game_status + + def get_move_history(self) -> List[Tuple[int, int]]: + """ + Get the move history of the current game. + + Returns: + List[Tuple[int, int]]: List of moves made (row, col) + """ + return self.move_history.copy() + + def get_winner(self) -> Optional[Player]: + """ + Get the winner of the game if there is one. + + Returns: + Optional[Player]: The winning player or None if no winner + """ + if self.game_status == GameStatus.X_WON: + return Player.X + elif self.game_status == GameStatus.O_WON: + return Player.O + else: + return None + + def get_game_state(self) -> Dict[str, Any]: + """ + Get the complete game state as a dictionary. + + Returns: + Dict[str, Any]: Dictionary containing all game state information + """ + return { + "board": self.get_board_state(), + "current_player": self.current_player.value, + "game_status": self.game_status.value, + "winner": self.get_winner().value if self.get_winner() else None, + "move_history": self.move_history + } + + +class GameRenderer: + """ + Handles rendering of the Tic Tac Toe game to HTML. + + This class generates the HTML and CSS for displaying the game board + and managing user interaction. + """ + + def __init__(self) -> None: + """ + Initialize the game renderer. + + Sets up the basic HTML structure and CSS styling for the game. + """ + self.game_state = None + + def render_html(self, game: TicTacToeGame) -> str: + """ + Render the complete HTML for the Tic Tac Toe game. + + Args: + game (TicTacToeGame): The current game instance + + Returns: + str: Complete HTML string for the game display + """ + self.game_state = game + + html_content = f""" + + + + + + Tic Tac Toe + + + +
+

Tic Tac Toe

+
Current Player: {game.get_current_player().value}
+
+ {self._render_board(game)} +
+
+ +
+
+ + + + + """ + + return html_content + + def _render_board(self, game: TicTacToeGame) -> str: + """ + Render the game board as HTML cells. + + Args: + game (TicTacToeGame): The current game instance + + Returns: + str: HTML string representing the board cells + """ + board_html = "" + + for i in range(3): + for j in range(3): + player = game.get_board_state()[i][j] + cell_value = player.value if player != Player.EMPTY else "" + + # Determine if the game is over to disable further moves + is_game_over = game.get_game_status() != GameStatus.PLAYING + disabled_class = "disabled" if is_game_over and player == Player.EMPTY else "" + + board_html += f''' +
+ {cell_value} +
+ ''' + + return board_html + + +class GameEngine: + """ + Main engine that coordinates the Tic Tac Toe game. + + This class acts as the central coordinator, managing the game lifecycle, + user interaction, and state updates. + """ + + def __init__(self) -> None: + """ + Initialize the game engine. + + Creates instances of the game controller and renderer. + """ + self.game = TicTacToeGame() + self.renderer = GameRenderer() + + def play_move(self, row: int, col: int) -> Dict[str, Any]: + """ + Process a player move and return updated game state. + + Args: + row (int): The row index where the move is made + col (int): The column index where the move is made + + Returns: + Dict[str, Any]: Updated game state after the move + """ + try: + success = self.game.make_move(row, col) + + if not success: + return { + "success": False, + "error": "Invalid move - cell already occupied", + "state": self.game.get_game_state() + } + + return { + "success": True, + "state": self.game.get_game_state() + } + + except ValueError as e: + return { + "success": False, + "error": str(e), + "state": self.game.get_game_state() + } + + def reset(self) -> Dict[str, Any]: + """ + Reset the game to initial state. + + Returns: + Dict[str, Any]: Game state after reset + """ + self.game.reset_game() + return { + "success": True, + "state": self.game.get_game_state() + } + + def get_html(self) -> str: + """ + Generate complete HTML for the game. + + Returns: + str: Complete HTML page for the Tic Tac Toe game + """ + return self.renderer.render_html(self.game) + + def get_game_state(self) -> Dict[str, Any]: + """ + Get the current game state. + + Returns: + Dict[str, Any]: Current game state as dictionary + """ + return self.game.get_game_state() + + +# Example usage and testing functions +def create_sample_game() -> GameEngine: + """ + Create a sample game instance for demonstration. + + Returns: + GameEngine: A configured game engine instance + """ + return GameEngine() + + +def test_game_logic() -> None: + """ + Test the core game logic with sample moves. + + This function demonstrates basic functionality of the Tic Tac Toe engine. + """ + game_engine = create_sample_game() + + # Test basic moves + result1 = game_engine.play_move(0, 0) + print(f"Move (0,0): {result1['success']}") + + result2 = game_engine.play_move(1, 1) + print(f"Move (1,1): {result2['success']}") + + # Test invalid move + result3 = game_engine.play_move(0, 0) + print(f"Move (0,0) again: {result3['success']}") + + # Test reset + reset_result = game_engine.reset() + print(f"Reset successful: {reset_result['success']}") + + # Print final state + state = game_engine.get_game_state() + print(f"Final game status: {state['game_status']}") + + +if __name__ == "__main__": + # Run basic tests + test_game_logic() + + # Create and display game HTML + engine = create_sample_game() + html_output = engine.get_html() + + # Save to file for demonstration + with open("tic_tac_toe.html", "w") as f: + f.write(html_output) + + print("Tic Tac Toe HTML generated successfully as 'tic_tac_toe.html'") \ No newline at end of file From 59053a35fd072a3187ca07034f1dd1d496d8c201 Mon Sep 17 00:00:00 2001 From: Shailesh Tripathi <45033205+shailesht003@users.noreply.github.com> Date: Sun, 18 Jan 2026 15:55:31 -0500 Subject: [PATCH 2/2] Add AI-generated unit tests for crm_5_implementation.py --- test_crm_5_implementation.py | 326 +++++++++++++++++++++++++++++++++++ 1 file changed, 326 insertions(+) create mode 100644 test_crm_5_implementation.py diff --git a/test_crm_5_implementation.py b/test_crm_5_implementation.py new file mode 100644 index 0000000..f73023c --- /dev/null +++ b/test_crm_5_implementation.py @@ -0,0 +1,326 @@ +import pytest +from unittest.mock import patch +from crm_5_implementation import ( + Player, + GameStatus, + TicTacToeGame, + GameRenderer, + GameEngine +) + + +@pytest.fixture +def tic_tac_toe_game(): + """Fixture to create a fresh TicTacToeGame instance.""" + return TicTacToeGame() + + +@pytest.fixture +def game_renderer(): + """Fixture to create a fresh GameRenderer instance.""" + return GameRenderer() + + +@pytest.fixture +def game_engine(): + """Fixture to create a fresh GameEngine instance.""" + return GameEngine() + + +class TestPlayerEnum: + """Test Player enumeration values.""" + + def test_player_enum_values(self): + """Test that Player enum has correct values.""" + assert Player.X.value == "X" + assert Player.O.value == "O" + assert Player.EMPTY.value == " " + + +class TestGameStatusEnum: + """Test GameStatus enumeration values.""" + + def test_game_status_enum_values(self): + """Test that GameStatus enum has correct values.""" + assert GameStatus.PLAYING.value == "playing" + assert GameStatus.X_WON.value == "x_won" + assert GameStatus.O_WON.value == "o_won" + assert GameStatus.DRAW.value == "draw" + + +class TestTicTacToeGame: + """Test TicTacToeGame class functionality.""" + + def test_initialization(self, tic_tac_toe_game): + """Test game initialization.""" + assert tic_tac_toe_game.board == [ + [Player.EMPTY, Player.EMPTY, Player.EMPTY], + [Player.EMPTY, Player.EMPTY, Player.EMPTY], + [Player.EMPTY, Player.EMPTY, Player.EMPTY] + ] + assert tic_tac_toe_game.current_player == Player.X + assert tic_tac_toe_game.game_status == GameStatus.PLAYING + assert tic_tac_toe_game.move_history == [] + + def test_make_move_valid(self, tic_tac_toe_game): + """Test making a valid move.""" + result = tic_tac_toe_game.make_move(0, 0) + assert result is True + assert tic_tac_toe_game.board[0][0] == Player.X + assert tic_tac_toe_game.current_player == Player.O + assert tic_tac_toe_game.move_history == [(0, 0)] + + def test_make_move_invalid_coordinates(self, tic_tac_toe_game): + """Test making a move with invalid coordinates.""" + with pytest.raises(ValueError): + tic_tac_toe_game.make_move(-1, 0) + + with pytest.raises(ValueError): + tic_tac_toe_game.make_move(0, 3) + + def test_make_move_occupied_cell(self, tic_tac_toe_game): + """Test making a move on an occupied cell.""" + # Make first move + tic_tac_toe_game.make_move(0, 0) + + # Try to make move on same cell + result = tic_tac_toe_game.make_move(0, 0) + assert result is False + assert tic_tac_toe_game.board[0][0] == Player.X + + def test_check_game_status_x_wins_row(self, tic_tac_toe_game): + """Test checking game status when X wins by row.""" + # Make moves for X to win in first row + tic_tac_toe_game.make_move(0, 0) # X + tic_tac_toe_game.make_move(1, 0) # O + tic_tac_toe_game.make_move(0, 1) # X + tic_tac_toe_game.make_move(1, 1) # O + tic_tac_toe_game.make_move(0, 2) # X + + assert tic_tac_toe_game.game_status == GameStatus.X_WON + + def test_check_game_status_o_wins_column(self, tic_tac_toe_game): + """Test checking game status when O wins by column.""" + # Make moves for O to win in first column + tic_tac_toe_game.make_move(0, 0) # X + tic_tac_toe_game.make_move(1, 0) # O + tic_tac_toe_game.make_move(0, 1) # X + tic_tac_toe_game.make_move(2, 0) # O + tic_tac_toe_game.make_move(0, 2) # X + tic_tac_toe_game.make_move(1, 1) # O + + assert tic_tac_toe_game.game_status == GameStatus.O_WON + + def test_check_game_status_x_wins_diagonal(self, tic_tac_toe_game): + """Test checking game status when X wins by diagonal.""" + # Make moves for X to win in main diagonal + tic_tac_toe_game.make_move(0, 0) # X + tic_tac_toe_game.make_move(1, 0) # O + tic_tac_toe_game.make_move(1, 1) # X + tic_tac_toe_game.make_move(0, 2) # O + tic_tac_toe_game.make_move(2, 2) # X + + assert tic_tac_toe_game.game_status == GameStatus.X_WON + + def test_check_game_status_draw(self, tic_tac_toe_game): + """Test checking game status when game is a draw.""" + # Fill board with alternating moves to create draw + moves = [ + (0, 0), (1, 1), (0, 1), (1, 0), (0, 2), + (2, 0), (2, 1), (2, 2), (1, 2) + ] + + for i, move in enumerate(moves): + if i % 2 == 0: + tic_tac_toe_game.make_move(move[0], move[1]) # X + else: + tic_tac_toe_game.make_move(move[0], move[1]) # O + + assert tic_tac_toe_game.game_status == GameStatus.DRAW + + def test_reset_game(self, tic_tac_toe_game): + """Test resetting the game.""" + # Make some moves + tic_tac_toe_game.make_move(0, 0) + tic_tac_toe_game.make_move(1, 1) + + # Reset game + tic_tac_toe_game.reset_game() + + assert tic_tac_toe_game.board == [ + [Player.EMPTY, Player.EMPTY, Player.EMPTY], + [Player.EMPTY, Player.EMPTY, Player.EMPTY], + [Player.EMPTY, Player.EMPTY, Player.EMPTY] + ] + assert tic_tac_toe_game.current_player == Player.X + assert tic_tac_toe_game.game_status == GameStatus.PLAYING + assert tic_tac_toe_game.move_history == [] + + def test_get_board_state(self, tic_tac_toe_game): + """Test getting board state.""" + # Make a move + tic_tac_toe_game.make_move(0, 0) + + board_state = tic_tac_toe_game.get_board_state() + assert board_state[0][0] == Player.X + assert board_state[1][0] == Player.EMPTY + + def test_get_current_player(self, tic_tac_toe_game): + """Test getting current player.""" + assert tic_tac_toe_game.get_current_player() == Player.X + + # Make a move + tic_tac_toe_game.make_move(0, 0) + assert tic_tac_toe_game.get_current_player() == Player.O + + def test_get_game_status(self, tic_tac_toe_game): + """Test getting game status.""" + assert tic_tac_toe_game.get_game_status() == GameStatus.PLAYING + + # Make moves to win + tic_tac_toe_game.make_move(0, 0) + tic_tac_toe_game.make_move(1, 0) + tic_tac_toe_game.make_move(0, 1) + tic_tac_toe_game.make_move(1, 1) + tic_tac_toe_game.make_move(0, 2) + + assert tic_tac_toe_game.get_game_status() == GameStatus.X_WON + + def test_get_move_history(self, tic_tac_toe_game): + """Test getting move history.""" + # Make some moves + tic_tac_toe_game.make_move(0, 0) + tic_tac_toe_game.make_move(1, 1) + + history = tic_tac_toe_game.get_move_history() + assert history == [(0, 0), (1, 1)] + + def test_get_winner(self, tic_tac_toe_game): + """Test getting winner.""" + assert tic_tac_toe_game.get_winner() is None + + # Make moves to win + tic_tac_toe_game.make_move(0, 0) + tic_tac_toe_game.make_move(1, 0) + tic_tac_toe_game.make_move(0, 1) + tic_tac_toe_game.make_move(1, 1) + tic_tac_toe_game.make_move(0, 2) + + assert tic_tac_toe_game.get_winner() == Player.X + + def test_get_game_state(self, tic_tac_toe_game): + """Test getting complete game state.""" + state = tic_tac_toe_game.get_game_state() + + assert state["board"] == [ + [Player.EMPTY, Player.EMPTY, Player.EMPTY], + [Player.EMPTY, Player.EMPTY, Player.EMPTY], + [Player.EMPTY, Player.EMPTY, Player.EMPTY] + ] + assert state["current_player"] == "X" + assert state["game_status"] == "playing" + assert state["winner"] is None + assert state["move_history"] == [] + + +class TestGameRenderer: + """Test GameRenderer class functionality.""" + + def test_initialization(self, game_renderer): + """Test renderer initialization.""" + assert game_renderer.game_state is None + + def test_render_html(self, game_renderer, tic_tac_toe_game): + """Test rendering HTML.""" + html_content = game_renderer.render_html(tic_tac_toe_game) + + assert "Tic Tac Toe" in html_content + assert "Current Player: X" in html_content + assert '