+ "text": "In this post, we will show a simple implementation of Tic-Tac-Toe generated by ChatGPT4\nHere is the initial class definition:\n# tic_tac_toe.py\n\nclass TicTacToe:\nHere is a short description of each method within the TicTacToe class in the provided Python file.\n\n__init__(self): The constructor for the TicTacToe class. It initializes the game board as a 3x3 grid of spaces and sets the current player to ‘X’.\ndef __init__(self):\n self.board = [[' ' for _ in range(3)] for _ in range(3)]\n self.current_turn = 'X'\nprint_board(self): Prints the current state of the game board to the console, including the grid lines.\ndef print_board(self):\n for row in self.board:\n print('|'.join(row))\n print('-'*5)\nis_valid_move(self, row, col): Checks whether the specified move (by row and column indices) is valid; that is, if the chosen cell on the board is empty (’ ’).\ndef is_valid_move(self, row, col):\n return self.board[row][col] == ' '\nplace_mark(self, row, col): Places the current player’s mark (‘X’ or ‘O’) on the board at the specified location if the move is valid, and returns False if the move is invalid (i.e., if the spot is already taken).\ndef place_mark(self, row, col):\n if not self.is_valid_move(row, col):\n return False\n self.board[row][col] = self.current_turn\n return True\nswitch_player(self): Switches the current player from ‘X’ to ‘O’ or ‘O’ to ‘X’, toggling back and forth after each valid move.\ndef switch_player(self):\n self.current_turn = 'O' if self.current_turn == 'X' else 'X'\ncheck_winner(self): Checks all possible winning combinations (rows, columns, and diagonals) to see if either player has won the game. It returns the winning player’s mark (‘X’ or ‘O’) if there is a winner, or None if there isn’t one yet.\ndef check_winner(self):\n # Check rows, columns and diagonals\n for i in range(3):\n if self.board[i][0] == self.board[i][1] == self.board[i][2] != ' ':\n return self.board[i][0]\n if self.board[0][i] == self.board[1][i] == self.board[2][i] != ' ':\n return self.board[0][i]\n if self.board[0][0] == self.board[1][1] == self.board[2][2] != ' ':\n return self.board[0][0]\n if self.board[0][2] == self.board[1][1] == self.board[2][0] != ' ':\n return self.board[0][2]\n return None\nis_board_full(self): Checks whether the board is completely filled with players’ marks; returns True if full, indicating a tie if there’s no winner, or False if there are still empty spaces.\ndef is_board_full(self):\n return all(self.board[row][col] != ' ' for row in range(3) for col in range(3))\nplay_game(self): The main game loop that repeatedly asks the current player for their move, checks for a win or a tie, and switches players. This method controls the game flow, displaying the board and prompting the players until the game ends with a winner or a tie.\ndef play_game(self):\n while True:\n self.print_board()\n\n # Try to place a mark, if the move is invalid, retry.\n try:\n row = int(input(f\"Player {self.current_turn}, enter your move row (0-2): \"))\n col = int(input(f\"Player {self.current_turn}, enter your move column (0-2): \"))\n except ValueError:\n print(\"Please enter numbers between 0 and 2.\")\n continue\n\n if row < 0 or row > 2 or col < 0 or col > 2:\n print(\"Invalid move. Try again.\")\n continue\n\n if not self.place_mark(row, col):\n print(\"This spot is taken. Try another spot.\")\n continue\n\n winner = self.check_winner()\n if winner:\n self.print_board()\n print(f\"Player {winner} wins!\")\n break\n elif self.is_board_full():\n self.print_board()\n print(\"It's a tie!\")\n break\n\n self.switch_player()\n\n\nHere is the main game execution:\nif __name__ == \"__main__\":\n game = TicTacToe()\n game.play_game()\nFinally, here is the full implementation:\n# tic_tac_toe.py\n\nclass TicTacToe:\n # [TicTacToe init start]\n def __init__(self):\n self.board = [[' ' for _ in range(3)] for _ in range(3)]\n self.current_turn = 'X'\n # [TicTacToe init end]\n\n def print_board(self):\n for row in self.board:\n print('|'.join(row))\n print('-'*5)\n\n def is_valid_move(self, row, col):\n return self.board[row][col] == ' '\n\n def place_mark(self, row, col):\n if not self.is_valid_move(row, col):\n return False\n self.board[row][col] = self.current_turn\n return True\n\n def switch_player(self):\n self.current_turn = 'O' if self.current_turn == 'X' else 'X'\n\n def check_winner(self):\n # Check rows, columns and diagonals\n for i in range(3):\n if self.board[i][0] == self.board[i][1] == self.board[i][2] != ' ':\n return self.board[i][0]\n if self.board[0][i] == self.board[1][i] == self.board[2][i] != ' ':\n return self.board[0][i]\n if self.board[0][0] == self.board[1][1] == self.board[2][2] != ' ':\n return self.board[0][0]\n if self.board[0][2] == self.board[1][1] == self.board[2][0] != ' ':\n return self.board[0][2]\n return None\n\n def is_board_full(self):\n return all(self.board[row][col] != ' ' for row in range(3) for col in range(3))\n\n def play_game(self):\n while True:\n self.print_board()\n\n # Try to place a mark, if the move is invalid, retry.\n try:\n row = int(input(f\"Player {self.current_turn}, enter your move row (0-2): \"))\n col = int(input(f\"Player {self.current_turn}, enter your move column (0-2): \"))\n except ValueError:\n print(\"Please enter numbers between 0 and 2.\")\n continue\n\n if row < 0 or row > 2 or col < 0 or col > 2:\n print(\"Invalid move. Try again.\")\n continue\n\n if not self.place_mark(row, col):\n print(\"This spot is taken. Try another spot.\")\n continue\n\n winner = self.check_winner()\n if winner:\n self.print_board()\n print(f\"Player {winner} wins!\")\n break\n elif self.is_board_full():\n self.print_board()\n print(\"It's a tie!\")\n break\n\n self.switch_player()\n\n# Main game execution\nif __name__ == \"__main__\":\n game = TicTacToe()\n game.play_game()"
0 commit comments