generated from mwc/lab_tic_tac_toe
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user