wrote the function check_winner in game.py so that the game can check whether player X or player O scoring three-in-a-row in any possible arrangement is true (which would lead to that player winning)

This commit is contained in:
kated
2026-06-01 10:55:36 -04:00
parent 57e83744c8
commit 2d0edd1798
4 changed files with 50 additions and 0 deletions

View File

@@ -58,4 +58,11 @@ class TTTGame:
def check_winner(self, state, symbol):
"Checks whether the player with `symbol` has won the game."
winning_combos = [(0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6)]
for combo in winning_combos:
#The syntax for refering to an element in the boards state is state["board"][index#] - in our case, our index# is the first item in the combo we are looking at for this iteration.
if state["board"][combo[0]] == symbol:
#check if the elements in the position on the board for each position in this combination are all equal to eachother
if state["board"][combo[0]] == state["board"][combo[1]] and state["board"][combo[0]] == state["board"][combo[2]]:
return True
return False