-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
192 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
*_files/ | ||
/.luarc.json | ||
|
||
/.quarto/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
project: | ||
type: website | ||
output-dir: .quarto | ||
preview: | ||
browser: true | ||
watch-inputs: true | ||
|
||
website: | ||
title: "Quarto + Include-Code-Files" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
--- | ||
title: Test of Include-Code-Files Quarto Extension | ||
filters: | ||
- include-code-files | ||
--- | ||
|
||
**In this post, we will show a simple implementation of `Tic-Tac-Toe` generated by ChatGPT4** | ||
|
||
Here is the initial class definition: | ||
|
||
```{.python include="tic_tac_toe.py" end-before="[TicTacToe init start]"} | ||
``` | ||
|
||
|
||
Here is a short description of each method within the TicTacToe class in the provided Python file. | ||
|
||
|
||
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'. | ||
|
||
```{.python include="tic_tac_toe.py" dedent=4 start-after="[TicTacToe init start]" end-before="[TicTacToe init end]"} | ||
``` | ||
1. `print_board(self)`: Prints the current state of the game board to the console, including the grid lines. | ||
```{.python include="tic_tac_toe.py" dedent=4 start-line="def print_board(self)" end-line=13} | ||
``` | ||
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 (' '). | ||
```{.python include="tic_tac_toe.py" dedent=4 start-line=15 end-line=16} | ||
``` | ||
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). | ||
```{.python include="tic_tac_toe.py" dedent=4 start-line=18 end-before=23} | ||
``` | ||
1. `switch_player(self)`: Switches the current player from 'X' to 'O' or 'O' to 'X', toggling back and forth after each valid move. | ||
```{.python include="tic_tac_toe.py" dedent=4 start-after=23 end-line=25} | ||
``` | ||
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. | ||
```{.python include="tic_tac_toe.py" dedent=4 start-line=27 end-line="return None"} | ||
``` | ||
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. | ||
```{.python include="tic_tac_toe.py" dedent=4 start-line=40 end-line=41} | ||
``` | ||
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. | ||
```{.python include="tic_tac_toe.py" dedent=4 start-line="def play_game(self)" end-before="# Main game execution"} | ||
``` | ||
Here is the main game execution: | ||
```{.python include="tic_tac_toe.py" start-after="# Main game execution"} | ||
``` | ||
|
||
|
||
Finally, here is the full implementation: | ||
|
||
```{.python include="tic_tac_toe.py" code-line-numbers="true"} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# tic_tac_toe.py | ||
|
||
class TicTacToe: | ||
# [TicTacToe init start] | ||
def __init__(self): | ||
self.board = [[' ' for _ in range(3)] for _ in range(3)] | ||
self.current_turn = 'X' | ||
# [TicTacToe init end] | ||
|
||
def print_board(self): | ||
for row in self.board: | ||
print('|'.join(row)) | ||
print('-'*5) | ||
|
||
def is_valid_move(self, row, col): | ||
return self.board[row][col] == ' ' | ||
|
||
def place_mark(self, row, col): | ||
if not self.is_valid_move(row, col): | ||
return False | ||
self.board[row][col] = self.current_turn | ||
return True | ||
|
||
def switch_player(self): | ||
self.current_turn = 'O' if self.current_turn == 'X' else 'X' | ||
|
||
def check_winner(self): | ||
# Check rows, columns and diagonals | ||
for i in range(3): | ||
if self.board[i][0] == self.board[i][1] == self.board[i][2] != ' ': | ||
return self.board[i][0] | ||
if self.board[0][i] == self.board[1][i] == self.board[2][i] != ' ': | ||
return self.board[0][i] | ||
if self.board[0][0] == self.board[1][1] == self.board[2][2] != ' ': | ||
return self.board[0][0] | ||
if self.board[0][2] == self.board[1][1] == self.board[2][0] != ' ': | ||
return self.board[0][2] | ||
return None | ||
|
||
def is_board_full(self): | ||
return all(self.board[row][col] != ' ' for row in range(3) for col in range(3)) | ||
|
||
def play_game(self): | ||
while True: | ||
self.print_board() | ||
|
||
# Try to place a mark, if the move is invalid, retry. | ||
try: | ||
row = int(input(f"Player {self.current_turn}, enter your move row (0-2): ")) | ||
col = int(input(f"Player {self.current_turn}, enter your move column (0-2): ")) | ||
except ValueError: | ||
print("Please enter numbers between 0 and 2.") | ||
continue | ||
|
||
if row < 0 or row > 2 or col < 0 or col > 2: | ||
print("Invalid move. Try again.") | ||
continue | ||
|
||
if not self.place_mark(row, col): | ||
print("This spot is taken. Try another spot.") | ||
continue | ||
|
||
winner = self.check_winner() | ||
if winner: | ||
self.print_board() | ||
print(f"Player {winner} wins!") | ||
break | ||
elif self.is_board_full(): | ||
self.print_board() | ||
print("It's a tie!") | ||
break | ||
|
||
self.switch_player() | ||
|
||
# Main game execution | ||
if __name__ == "__main__": | ||
game = TicTacToe() | ||
game.play_game() |