Skip to content

Commit

Permalink
Square taken validations (#11)
Browse files Browse the repository at this point in the history
closes #10
  • Loading branch information
s2t2 authored Jul 25, 2024
1 parent 6289468 commit 27a436e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 6 additions & 1 deletion app/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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


Expand Down
7 changes: 5 additions & 2 deletions app/game.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 27a436e

Please sign in to comment.