I am working on a tic tack toe game in python (to improve my python skills) and am stuck on the following problem: I have a list of lists like below which determine whether or not a player has won depending on if all values either, vertically, horizontally, or diagonally have the same value.
win_horizontal = [[board[0], board[1], board[2]], [board[3], board[4], board[5]], [board[6], board[7], board[8]]]
win_vertical = [[board[0], board[3], board[6]], [board[1], board[4], board[7]], [board[2], board[5], board[8]]]
win_diagonal = [[board[0], board[4], board[8]], [board[2], board[4], board[6]]]
So at present, I have the following logic:
result = False;
result = all(elem == mark for elem in win_horizontal[1])
if result :
print("All Elements in List are Equal")
else:
print("All Elements in List are Not Equal")
This works as I am only checking one list within the list. However, I am not sure how to check against all of the list of lists to determine whether or not the player has won diagonally, horizontally, or vertically. I want to be able to check against all of the list of lists.
I tried the following
def win_check(board, mark):
win_horizontal = [[board[0], board[1], board[2]], [board[3], board[4], board[5]], [board[6], board[7], board[8]]]
win_vertical = [[board[0], board[3], board[6]], [board[1], board[4], board[7]], [board[2], board[5], board[8]]]
win_diagonal = [[board[0], board[4], board[8]], [board[2], board[4], board[6]]]
winner = [win_horizontal, win_vertical, win_diagonal]
result = False;
result = all(elem == mark for elem in any(winner))
if result :
print("All Elements in List are Equal")
else:
print("All Elements in List are Not Equal")
But I just get the following error TypeError: 'bool' object is not iterable which I assume is being raised from any(winner))
What is the best way to check for the winning condition?