I am writing code for a tick-tac-toe game that uses grids. I am trying to check the condition where all the cells of a row match with each other, so that players can be declared winners. Also, I am using dynamic sizes of board (3 by 3, 4 by 4, 5 by 5....). So for this purpose, I am using if condition in a for loop. The problem is that I can't figure out a way to add arguments dynamically to the if statement. The if defined is static and I can't add the condition of the last cell of the row (if size increases from 3 by 3 to 4 by 4).
expected outcome:
3 by 3 grid:
count = 0
for i in range(dim):
if grid[count]=="X" and grid[1+ count]=="X" and grid[2+ count]=="X":
count1 = 0
print ("Player X win")
print ("-------------")
for i in range(dim):
print(grid[0 + count1 : dim + count1])
count1 =+ dim
print ("-------------")
count += dim
I am trying to loop for all rows to check the match.
What I want is to change the if grid[count]=="X" and grid[1+ count]=="X" and grid[2+ count]=="X": to incorporate the change of the grid size from
3 by 3 to
4 by 4 if grid[count]=="X" and grid[1+ count]=="X" and grid[2+ count]=="X" and grid[3+ count]=="X":
5 by 5 if grid[count]=="X" and grid[1+ count]=="X" and grid[2+ count]=="X" and grid[3+ count]=="X" and grid[4+ count]=="X":
is there a better way to do this? Thank you!
Aucun commentaire:
Enregistrer un commentaire