I am new to Python (learning it for a little over 1 month) and I tried creating Tic Tac Toe. However once I finished it, I decide to expand the board (from 3x3 to 9x9 depending from the customer input) and allow a win by connecting 4 in a row, column or diagonal anywhere in the board.
Therefore I needed a function that search - depending from the customer input - in every directions on the board, without going overboard, for 3 connected cells with the same mark.
Then I realize that actually I need to check simultaneously in both opposite direction from the mark, as there is a possibility that the currently placed mark can connect other groups of the same mark and winning by doing this.
Below you can find my solution of the above winning condition, which is working, however it does not look pretty as there is a lot of repetitive almost identical for loops. But I have zero clue how to combine them.
There are 8 loops (as there are 8 directions), in 4 groups of opposite directions, and each loop check if the next cell is with the same value (mark) as the one that is currently placed by the customer (either 'O' or 'X').If it is the count variable is increased by 1. If not - the loop breaks. Then I am checking if the two opposite direction loops managed to made count == 4. If yes - we have a winner. If not - we go to the next pair.
I am sure there should be easier to maintain code, but I am not able to think of any.
Thank you for your help!
P.S. if there is a need I will post more from my code
def do_we_have_a_winner(position, current_mark):
global game_board
connect = 4
# unpacking the coordinates of the current position
x, y = coordinates[position]
length = len(game_board)
count = 0
for i in range(0, connect):
if (x-i in range(0, length)) and (y+i in range(0, length)):
if game_board[x-i][y+i] != current_mark:
break
else:
count += 1
for i in range(1, connect):
if (x+i in range(0, length)) and (y-i in range(0, length)):
if game_board[x+i][y-i] != current_mark:
break
else:
count += 1
if count == connect:
print("We have a winner!")
return True
count = 0
for i in range(0, connect):
if (x+i in range(0, length)) and (y+i in range(0, length)):
if game_board[x+i][y+i] != current_mark:
break
else:
count += 1
for i in range(1, connect):
if (x-i in range(0, length)) and (y-i in range(0, length)):
if game_board[x-i][y-i] != current_mark:
break
else:
count += 1
if count == connect:
print("We have a winner!")
return True
count = 0
for i in range(0, connect):
if (y+i in range(0, length)):
if game_board[x][y+i] != current_mark:
break
else:
count += 1
for i in range(1, connect):
if (y-i in range(0, length)):
if game_board[x][y-i] != current_mark:
break
else:
count += 1
if count == connect:
print("We have a winner!")
return True
count = 0
for i in range(0, connect):
if (x+i in range(0, length)):
if game_board[x+i][y] != current_mark:
break
else:
count += 1
for i in range(1, connect):
if (x-i in range(0, length)):
if game_board[x-i][y] != current_mark:
break
else:
count += 1
if count == connect:
print("We have a winner!")
return True
return False
See Question&Answers more detail:
os