generated from mwc/lab_tic_tac_toe
I just used the coordiantes of the grid to check all possible winning combinations.
This commit is contained in:
10
ttt/game.py
10
ttt/game.py
@@ -58,4 +58,14 @@ class TTTGame:
|
||||
|
||||
def check_winner(self, state, symbol):
|
||||
"Checks whether the player with `symbol` has won the game."
|
||||
board = state["board"]
|
||||
# All winning triplets (rows, columns, diagonals)
|
||||
winning_combinations = [
|
||||
(0, 1, 2), (3, 4, 5), (6, 7, 8), # rows
|
||||
(0, 3, 6), (1, 4, 7), (2, 5, 8), # columns
|
||||
(0, 4, 8), (2, 4, 6), # diagonals
|
||||
]
|
||||
for i, j, k in winning_combinations:
|
||||
if board[i] == board[j] == board[k] == symbol:
|
||||
return True
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user