Checkpoint 2

To decide on a winner, the game needs to check each of the winning
combinations on the board for either x symbols or o symbols and
then return the correct winner. To do this, I utilized 2 functions, a
check_winning_combo, which checked each index in each combination to see
if an x or o symbol was present, and then a check_winner which sent each
possible winning combination through check_winning combo. If the combo
did not have all x's or all o's (a mix of x's and o's and/or blanks)
the check_winning combo returned false otherwise it returned true.
This commit is contained in:
Chris Mekelburg 2024-11-16 21:03:08 -05:00
parent 18ceeb60b7
commit 2d470c3c89
2 changed files with 22 additions and 0 deletions

View File

@ -30,6 +30,7 @@ and it's your turn, which action would you take? Why?
---+---+--- ---+---+--- ---+---+--- ---+---+---
| | | | O | | | |
### Initial game state
You can get the inital game state using game.get_initial_state().

View File

@ -58,4 +58,25 @@ class TTTGame:
def check_winner(self, state, symbol):
"Checks whether the player with `symbol` has won the game."
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 combos:
if self.check_winning_combo(state,symbol,combo):
return True
return False
def check_winning_combo(self, state, symbol, combo):
"""Checks whether all three spaces in a combo eg[1,2,3] are
filled with a symbol"""
for index in combo:
if state["board"][index] != symbol:
return False
return True