From 27a436e3719575b68aec54a1b35134b6319e1a0f Mon Sep 17 00:00:00 2001 From: Michael Rossetti Date: Thu, 25 Jul 2024 17:20:51 -0400 Subject: [PATCH] Square taken validations (#11) closes #10 --- app/board.py | 7 ++++++- app/game.py | 7 +++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/board.py b/app/board.py index 7fd3f75..b73a872 100644 --- a/app/board.py +++ b/app/board.py @@ -21,6 +21,9 @@ ["A1", "B2", "C3"], # [3,5,7], # Diagonal DESC ] +class SquareTakenError(ValueError): + pass + class Board: def __init__(self): self.squares = [Square(square_name) for square_name in SQUARE_NAMES] @@ -59,7 +62,9 @@ def get_squares(self, square_names): def set_square(self, square_name, player_letter): square = self.get_square(square_name) - if not square.letter: + if square.letter: + raise SquareTakenError(f"OOPS, square '{square_name}' already taken. please try again...") + else: square.letter = player_letter diff --git a/app/game.py b/app/game.py index b5b5c82..6d5dcc8 100644 --- a/app/game.py +++ b/app/game.py @@ -1,7 +1,7 @@ from itertools import cycle -from app.board import Board +from app.board import Board, SquareTakenError from app.player import select_player from app.move import Move @@ -91,8 +91,11 @@ def play(self): turn = (self.active_player.letter, square_name) self.take_turn(turn) break # break out of the input loop to conclude the turn and go to the next player + except SquareTakenError as err: + print("...", err) + next except: - print(f"OOPS UNRECOGNIZED SQUARE NAME '{square_name}'. PLEASE TRY AGAIN...") + print(f"... OOPS UNRECOGNIZED SQUARE NAME '{square_name}'. PLEASE TRY AGAIN...") next # ask the player for another input (this is only applicable for human players) print(self.board) print(self.outcome)