From b5ab43b8b8d61facbae8908a112bfc556c8ada7a Mon Sep 17 00:00:00 2001 From: Pat Wick Date: Mon, 12 Feb 2024 21:48:06 -0500 Subject: [PATCH] added win conditions to check_winner in game.py What I changed I added the 8 possible tic-tac-toe win conditions to check_winner and compare the values in the state with the symbol passed to the method Why I changed it It took a WHILE to figure out how to actually read the current state of the game, but once I had that, I was able to figure out how to check it for the symbol being sought. Estimate for remaining time to finish assignment: [1 hour?] I thought it was "how long did it take" now "how long remaining" originally. --- play_ttt.py | 3 +++ ttt/game.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/play_ttt.py b/play_ttt.py index ef5530a..afce7cb 100644 --- a/play_ttt.py +++ b/play_ttt.py @@ -7,9 +7,12 @@ player1 = TTTHumanPlayer("Player 2") game = TTTGame() view = TTTView(player0, player1) +# creates blank board state = game.get_initial_state() view.greet() +# gets passed blank board at start while not game.is_over(state): + # take an action and update board action = view.get_action(state) state = game.get_next_state(state, action) view.conclude(state) diff --git a/ttt/game.py b/ttt/game.py index 2f0e302..26d6f35 100644 --- a/ttt/game.py +++ b/ttt/game.py @@ -26,6 +26,7 @@ class TTTGame: def is_over(self, state): "Checks whether the game is over." + # FALSE because blank FALSE because no markers FALSE because no markers return self.board_is_full(state) or self.check_winner(state, 'X') or self.check_winner(state, 'O') def get_reward(self, state): @@ -58,4 +59,17 @@ class TTTGame: def check_winner(self, state, symbol): "Checks whether the player with `symbol` has won the game." + currentState = state["board"] + # possible tic tac toes: 012 345 678 036 147 258 048 246 + for i in range(0,7,3): + if currentState[i] == symbol and currentState[i+1] == symbol and currentState[i+2] == symbol: + return True + for i in range(3): + if currentState[i] == symbol and currentState[i+3] == symbol and currentState[i+6] == symbol: + return True + if currentState[0] == symbol and currentState[4] == symbol and currentState[8] == symbol: + return True + if currentState[2] == symbol and currentState[4] == symbol and currentState[6] == symbol: + return True + return False