Skip to content
Snippets Groups Projects
Commit 1750cc72 authored by Lukas Hoffleit's avatar Lukas Hoffleit
Browse files

Spielfeld in Farben

parent 90c9b6b9
No related branches found
No related tags found
No related merge requests found
......@@ -7,13 +7,25 @@ class Player(Enum):
two = 2
grid_full = 3
# Adjust the symbol or color for printing the grid
SYMBOLS = {
Player.one: "X",
Player.two: "O",
Player.undefined: " "
}
COLORS = {
Player.one: "\033[32m", # green
Player.two: "\033[94m", # blue
Player.undefined: ""
}
END_COLOR = "\033[0m"
def get_opponent(player:Player) -> Player:
if player == Player.one:
return Player.two
else:
return Player.one
class TicTacToeError(Exception):
"""TicTacToe Error"""
......@@ -37,7 +49,8 @@ class TicTacToe():
return player
def add(self, row:int, col:int, player:Player|int, grid:np.ndarray|None = None) -> bool:
""" Change the value of a undefined field.
"""
Change the value of a undefined field.
The player can be passed as Player Object or plain Int.
Returns True, if executed correctly.
Returns False, if the field is already occupied
......@@ -52,7 +65,9 @@ class TicTacToe():
return False
def check_field(self, row:int, col:int, grid:np.ndarray|None = None) -> bool:
"""Checks, if a field is occupied"""
"""
Checks, if a field is occupied
"""
if grid is None:
grid = self.grid
if self.grid[row][col] != Player.undefined:
......@@ -66,17 +81,12 @@ class TicTacToe():
"""
if grid is None:
grid = self.grid
symbols = {
Player.one: "X",
Player.two: "O",
Player.undefined: " "
}
print("\t┌───┬───┬───┐")
for i, row in enumerate(grid):
print("\t", end="")
for field in row:
print(f" {symbols[field]}", end="")
print(f" {COLORS[field]}{SYMBOLS[field]}{END_COLOR}", end="")
if i < len(grid) - 1:
print("\n\t├───┼───┼───┤")
print("\n\t└───┴───┴───┘\n\n")
......@@ -192,5 +202,3 @@ if __name__ == "__main__":
else:
current_player = Player.one
ttt.print_grid()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment