generated from mwc/lab_tic_tac_toe
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:
parent
ba0cc09a82
commit
9500a68ae7
2
notes.md
2
notes.md
|
@ -30,6 +30,8 @@ and it's your turn, which action would you take? Why?
|
|||
---+---+--- ---+---+--- ---+---+--- ---+---+---
|
||||
| | | | O | | | |
|
||||
|
||||
For the first one, I would put an X on the rightmost cell in the middle row. This would allow me to get a win. For the second one, I would put it in the same position as I would have in the first. This prevents O from winning on the next turn and puts me one turn away from winning, assuming the opponent doesn't block that route. For the third one, I would put it in the top left. This gives me two possible ways to win, and since the opponent only has one turn after mine before I get to go again, they cannot stop me from winning. For the last one, I would put it in the center. The opponent has to block by placing an O in the bottom right, and then I can put an X in the leftmost cell of the middle row. This leads to two possible ways to win again.
|
||||
|
||||
### Initial game state
|
||||
|
||||
You can get the inital game state using game.get_initial_state().
|
||||
|
|
16
ttt/game.py
16
ttt/game.py
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue