From 920ae1ac49622c89a5b5e93073e5d8d99474b860 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sat, 30 Sep 2017 09:34:46 -0700 Subject: [PATCH] Check if it is O's turn --- tictactoe.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tictactoe.py b/tictactoe.py index 4362db6..fbe99cd 100644 --- a/tictactoe.py +++ b/tictactoe.py @@ -17,6 +17,24 @@ class Board: # Process the board into an array. self.board = string.lower() + + @property + def is_o_turn(self): + num_x, num_o = self._tally_pieces() + empty_board = (num_x == 0 and num_o == 0) + full_board = (num_x + num_o == BOARD_SIZE) + x_ahead_one = (num_x == num_o + 1) + return (empty_board or x_ahead_one) and not full_board + + def _tally_pieces(self): + num_x = 0 + num_o = 0 + for c in self.board: + if c == Board.X: + num_x += 1 + elif c == Board.O: + num_o += 1 + return num_x, num_o def __repr__(self): return "".format(self.board) @@ -38,6 +56,9 @@ def hello(): try: board = Board(request.args.get('board', None)) except ValueError: - abort(400) + return ("Invalid board.", 400, {'Content-type': 'text/plain'}) + + if not board.is_o_turn: + return ("It isn't O's turn.\n\n{}".format(board), 400, {'Content-type': 'text/plain'}) return (str(board), 200, {'Content-type': 'text/plain'})