Last active
July 25, 2023 00:06
-
-
Save winstxnhdw/59e845279f6ffc738fa35ee366158de8 to your computer and use it in GitHub Desktop.
An N-by-N tic-tac-toe game framework.
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
from enum import Enum | |
class Players(Enum): | |
""" | |
Summary | |
------- | |
an enum to represent the players | |
""" | |
NULL = 0 | |
PLAYER1 = 1 | |
PLAYER2 = 2 | |
class TicTacToe: | |
""" | |
Summary | |
------- | |
a class for holding the game state of a tic-tac-toe game | |
Attributes | |
---------- | |
size (int) : the size of the board | |
rows (list[int]) : the sum of each row | |
columns (list[int]) : the sum of each column | |
diagonal (int) : the sum of the diagonal | |
anti_diagonal (int) : the sum of the anti-diagonal | |
""" | |
__slots__ = 'size', 'rows', 'columns', 'diagonal', 'anti_diagonal' | |
def __init__(self, size: int): | |
self.size = size | |
self.rows = [0]*size | |
self.columns = [0]*size | |
self.diagonal = 0 | |
self.anti_diagonal = 0 | |
def move(game: TicTacToe, row: int, column: int, player: Players) -> Players: | |
""" | |
Summary | |
------- | |
the winning player is decided by whichever row/column/diagonal/anti-diagonal reach a score of N | |
Parameters | |
---------- | |
game (TicTacToe) : the game state | |
row (int) : the row to make the move in | |
column (int) : the column to make the move in | |
player (Players) : the player making the move | |
Returns | |
------- | |
player (Players) : the player that won the game, or Players.NULL if no one won | |
""" | |
score_increment = 1 if player == Players.PLAYER1 else -1 | |
game.rows[row] += score_increment | |
game.columns[column] += score_increment | |
if row == column: | |
game.diagonal += score_increment | |
if column == game.size - row - 1: | |
game.anti_diagonal += score_increment | |
return player | |
if any(abs(item) == game.size for item in (game.rows[row], game.columns[column], game.diagonal, game.anti_diagonal)) | |
else Players.NULL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment