diff --git a/tictactoe.py b/tictactoe.py index de29f06083f342ebae0e7813af3d6fe026b6d66a..1f552474313d4e3d3118afc2612f7267e1ac632a 100644 --- a/tictactoe.py +++ b/tictactoe.py @@ -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() - -