I am trying to write a code that determines the winner of a tic-tac-toe game. (This is for a college assignment)
I have written the following function to do so:
This code only checks for horizontal lines, I haven't added the rest. I feel that this is something that needs a bit of hardcoding.
def iswinner(board, decorator):
win = True
for row in range(len(board)):
for col in range(len(board)):
if board[row][col] == decorator:
win = True
else:
win = False
break
Where "board" is a 2D array of size n^2 and "decorator" is the "X" or "O" value
What I hope to accomplish is that the function loops through the 2D array's rows. Then loops through the values in each row. If that element matches the "decorator" then it continues and checks the next but if it doesn't, then it breaks from the first loop and goes to the next row. It does this until it finds n elements in the same row. Then it would give a bool value of True otherwise False.
The code doesn't seem to do that and even when I checked with the following "board" it gave me an output of "True"
check_list = [['O', 'X', 'X'], ['O', 'X', 'O'], ['O', 'X', 'X']]
Thank you so much!
Best, Seyed