Я кодировал игру 2048 (сетка 4 * 4) в python, используя Tkinter в качестве GUI, и дошел до конечной части кода, где мне нужно проверить все блоки, чтобы увидеть, является ли блок то же самое или другое, и если блок отличается, проверьте, что там ничего нет. Мои другие попытки решить проблему привели к тому, что игра не заканчивалась или не заканчивалась на старте. Как реализована доска:
placesThatAreEpmty=[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
Как выглядит финал в данный момент:
def ending():
print(placesThatAreEpmty)
counter=0
full=True
while counter!=17 and full==True:
full=False
counter=0
for i in range(4):
for j in range(4):
if placesThatAreEpmty[i][j]==0:
return False
elif placesThatAreEpmty[i][j]!=0:
if placesThatAreEpmty[i][j]==2048:
return 2048
elif j==1 or j==2 and i==1 or i==2:
print(i,j)
if placesThatAreEpmty[i][j]!=placesThatAreEpmty[i][j+1] or placesThatAreEpmty[i][j+1]!=0:
full=True
elif placesThatAreEpmty[i][j]!=placesThatAreEpmty[i][j-1] or placesThatAreEpmty[i][j-1]!=0:
full=True
elif placesThatAreEpmty[i][j]!=placesThatAreEpmty[i+1][j] or placesThatAreEpmty[i+1][j]!=0:
full=True
elif placesThatAreEpmty[i][j]!=placesThatAreEpmty[i-1][j] or placesThatAreEpmty[i-1][j]!=0:
full=True
elif i==0 and j==0:
if placesThatAreEpmty[i][j]!=placesThatAreEpmty[i][j+1] or placesThatAreEpmty[i][j+1]!=0:
full=True
elif placesThatAreEpmty[i][j]!=placesThatAreEpmty[i+1][j] or placesThatAreEpmty[i+1][j]!=0:
full=True
elif i==3 and j==3:
if placesThatAreEpmty[i][j]!=placesThatAreEpmty[i][j-1] or placesThatAreEpmty[i][j-1]!=0:
full=True
elif placesThatAreEpmty[i][j]!=placesThatAreEpmty[i-1][j] or placesThatAreEpmty[i-1][j]!=0:
full=True
elif i==0 and j==0:
if placesThatAreEpmty[i][j]!=placesThatAreEpmty[i][j-1] or placesThatAreEpmty[i][j+1]!=0:
full=True
elif placesThatAreEpmty[i][j]!=placesThatAreEpmty[i+1][j] or placesThatAreEpmty[i+1][j]!=0:
full=True
elif i==3 and j==3:
if placesThatAreEpmty[i][j]!=placesThatAreEpmty[i][j+1] or placesThatAreEpmty[i][j-1]!=0:
full=True
elif placesThatAreEpmty[i][j]!=placesThatAreEpmty[i-1][j] or placesThatAreEpmty[i-1][j]!=0:
full=True
elif i==0:
if placesThatAreEpmty[i][j]!=placesThatAreEpmty[i][j+1] or placesThatAreEpmty[i][j+1]!=0:
full=True
elif placesThatAreEpmty[i][j]!=placesThatAreEpmty[i][j-1] or placesThatAreEpmty[i][j-1]!=0:
full=True
elif placesThatAreEpmty[i][j]!=placesThatAreEpmty[i+1][j] or placesThatAreEpmty[i+1][j]!=0:
full=True
elif i==3:
if placesThatAreEpmty[i][j]!=placesThatAreEpmty[i][j+1] or placesThatAreEpmty[i][j+1]!=0:
full=True
elif placesThatAreEpmty[i][j]!=placesThatAreEpmty[i][j-1] or placesThatAreEpmty[i][j-1]!=0:
full=True
elif placesThatAreEpmty[i][j]!=placesThatAreEpmty[i-1][j] or placesThatAreEpmty[i+1][j]!=0:
full=True
elif j==3:
if placesThatAreEpmty[i][j]!=placesThatAreEpmty[i+1][j] or placesThatAreEpmty[i][j+1]!=0:
full=True
elif placesThatAreEpmty[i][j]!=placesThatAreEpmty[i][j-1] or placesThatAreEpmty[i][j-1]!=0:
full=True
elif placesThatAreEpmty[i][j]!=placesThatAreEpmty[i-1][j] or placesThatAreEpmty[i+1][j]!=0:
full=True
elif j==0:
if placesThatAreEpmty[i][j]!=placesThatAreEpmty[i+1][j] or placesThatAreEpmty[i][j+1]!=0:
full=True
elif placesThatAreEpmty[i][j]!=placesThatAreEpmty[i][j+1] or placesThatAreEpmty[i][j-1]!=0:
full=True
elif placesThatAreEpmty[i][j]!=placesThatAreEpmty[i-1][j] or placesThatAreEpmty[i+1][j]!=0:
full=True
return full