Skip to content

Instantly share code, notes, and snippets.

@Kamforka
Created February 23, 2022 16:53
Show Gist options
  • Save Kamforka/e6b80b016c9fdb3340dc37e2847b0fb3 to your computer and use it in GitHub Desktop.
Save Kamforka/e6b80b016c9fdb3340dc37e2847b0fb3 to your computer and use it in GitHub Desktop.
def check_rows_for_winner(board):
for i in range(3):
row = ""
for j in range(3):
row += board[i][j]
if row == "XXX":
return "X"
if row == "OOO":
return "O"
return None
def check_columns_for_winner(board):
for i in range(3):
col = ""
for j in range(3):
col += board[j][i]
if col == "XXX":
return "X"
if col == "OOO":
return "O"
return None
def check_first_diagonal_for_winner(board):
diagonal = ""
for i in range(3):
diagonal += board[i][i]
if diagonal == "XXX":
return "X"
if diagonal == "OOO":
return "O"
return None
def check_second_diagonal_for_winner(board):
diagonal = ""
for i in range(3):
diagonal += board[i][2 - i]
if diagonal == "XXX":
return "X"
if diagonal == "OOO":
return "O"
return None
def get_winner(board):
winner = check_rows_for_winner(board)
if not winner:
winner = check_columns_for_winner(board)
if not winner:
winner = check_first_diagonal_for_winner(board)
if not winner:
winner = check_second_diagonal_for_winner(board)
return winner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment