|
| 1 | +--- |
| 2 | +title: Test of Include-Code-Files Quarto Extension |
| 3 | +filters: |
| 4 | + - include-code-files |
| 5 | +--- |
| 6 | + |
| 7 | +**In this post, we will show a simple implementation of `Tic-Tac-Toe` generated by ChatGPT4** |
| 8 | + |
| 9 | +Here is the initial class definition: |
| 10 | + |
| 11 | +```{.python include="tic_tac_toe.py" end-before="[TicTacToe init start]"} |
| 12 | +``` |
| 13 | + |
| 14 | + |
| 15 | +Here is a short description of each method within the TicTacToe class in the provided Python file. |
| 16 | + |
| 17 | + |
| 18 | +1. `__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'. |
| 19 | + |
| 20 | + ```{.python include="tic_tac_toe.py" dedent=4 start-after="[TicTacToe init start]" end-before="[TicTacToe init end]"} |
| 21 | + ``` |
| 22 | +
|
| 23 | +1. `print_board(self)`: Prints the current state of the game board to the console, including the grid lines. |
| 24 | +
|
| 25 | + ```{.python include="tic_tac_toe.py" dedent=4 start-line="def print_board(self)" end-line=13} |
| 26 | + ``` |
| 27 | +
|
| 28 | +1. `is_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 (' '). |
| 29 | +
|
| 30 | + ```{.python include="tic_tac_toe.py" dedent=4 start-line=15 end-line=16} |
| 31 | + ``` |
| 32 | +
|
| 33 | +1. `place_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). |
| 34 | +
|
| 35 | + ```{.python include="tic_tac_toe.py" dedent=4 start-line=18 end-before=23} |
| 36 | + ``` |
| 37 | +
|
| 38 | +1. `switch_player(self)`: Switches the current player from 'X' to 'O' or 'O' to 'X', toggling back and forth after each valid move. |
| 39 | +
|
| 40 | + ```{.python include="tic_tac_toe.py" dedent=4 start-after=23 end-line=25} |
| 41 | + ``` |
| 42 | +
|
| 43 | +1. `check_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. |
| 44 | +
|
| 45 | + ```{.python include="tic_tac_toe.py" dedent=4 start-line=27 end-line="return None"} |
| 46 | + ``` |
| 47 | +
|
| 48 | +1. `is_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. |
| 49 | +
|
| 50 | + ```{.python include="tic_tac_toe.py" dedent=4 start-line=40 end-line=41} |
| 51 | + ``` |
| 52 | +
|
| 53 | +1. `play_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. |
| 54 | +
|
| 55 | + ```{.python include="tic_tac_toe.py" dedent=4 start-line="def play_game(self)" end-before="# Main game execution"} |
| 56 | + ``` |
| 57 | +
|
| 58 | +Here is the main game execution: |
| 59 | +
|
| 60 | +```{.python include="tic_tac_toe.py" start-after="# Main game execution"} |
| 61 | +``` |
| 62 | + |
| 63 | + |
| 64 | +Finally, here is the full implementation: |
| 65 | + |
| 66 | +```{.python include="tic_tac_toe.py" code-line-numbers="true"} |
| 67 | +``` |
0 commit comments