Fixed TTTGame.check_winner.

What I changed
Added 8 cases to check for to see if someone won: three rows, three columns and two diagonals.

Why I changed it
TTTGame.check_winner always reported false, so the game only ended when the board was full even if someone should have won.
Estimate for remaining time to finish assignment: [Like a few hours assuming my students don't rip a door off its hinges in the bathroom again...]
This commit is contained in:
Cory
2024-03-13 13:38:20 -04:00
parent ba0cc09a82
commit 9500a68ae7
2 changed files with 18 additions and 0 deletions

View File

@@ -58,4 +58,20 @@ class TTTGame:
def check_winner(self, state, symbol):
"Checks whether the player with `symbol` has won the game."
if state["board"][0] == symbol and state["board"][1] == symbol and state["board"][2] == symbol:
return True
elif state["board"][3] == symbol and state["board"][4] == symbol and state["board"][5] == symbol:
return True
elif state["board"][6] == symbol and state["board"][7] == symbol and state["board"][8] == symbol:
return True
elif state["board"][0] == symbol and state["board"][3] == symbol and state["board"][6] == symbol:
return True
elif state["board"][1] == symbol and state["board"][4] == symbol and state["board"][7] == symbol:
return True
elif state["board"][2] == symbol and state["board"][5] == symbol and state["board"][8] == symbol:
return True
elif state["board"][0] == symbol and state["board"][4] == symbol and state["board"][8] == symbol:
return True
elif state["board"][2] == symbol and state["board"][4] == symbol and state["board"][6] == symbol:
return True
return False