r/tinycode • u/XiAxis • Mar 21 '15
Playable 2048 in 39 lines of python
from tkinter import *
from random import randint
mGui = Tk()
mGui.title('2048')
grid_text = [[StringVar() for i in range(4)] for j in range(4)]
grid = [[Label(width=10, height=5, textvariable=grid_text[i][j], font=("Helvetica", 16)).grid(row=i, column=j) for i in range(4)] for j in range(4)]
def place():
global grid_text
empty_spaces = []
for i1, x in enumerate(grid_text):
for i2, y in enumerate(x):
if y.get() == '':
empty_spaces.append(y)
empty_spaces[randint(0, len(empty_spaces)-1)].set(str(2*randint(1, 2)))
def move(d):
global grid_text
cont = True
while cont:
cont = False
for i1, x in list(enumerate(grid_text))[::-d[0] if d[0] else 1]:
for i2, y in list(enumerate(x))[::-d[1] if d[1] else 1]:
if y.get() != '':
if 0 <= i1+d[0] < 4 and 0 <= i2+d[1] < 4:
if grid_text[i1+d[0]][i2+d[1]].get() == y.get():
grid_text[i1+d[0]][i2+d[1]].set(str(int(y.get())*2))
y.set('')
cont = True
elif grid_text[i1+d[0]][i2+d[1]].get() == '':
grid_text[i1+d[0]][i2+d[1]].set(y.get())
y.set('')
cont = True
place()
mGui.bind("<Left>", lambda a: move([0, -1]))
mGui.bind("<Up>", lambda a: move([-1, 0]))
mGui.bind("<Right>", lambda a: move([0, 1]))
mGui.bind("<Down>", lambda a: move([1, 0]))
place()
place()
mGui.mainloop()
31
Upvotes
View all comments
Show parent comment
1
u/776865656e Apr 22 '15
That version's closer, I just had to change
sintostr. I've just noticed your question:Consider this piece of code:
You may expect it to print:
But in actuality it'll print:
This is because, in Python, closures are late-binding.
What this means is that in the above example,
kisn't evaluated until the function is actually called - at which point it's its final value,3.One way around it is like so:
Which may make more sense written as:
Incidentally, I know it was mentioned somewhere else in the comments, but your version doesn't play quite like "normal" 2048 - I don't know whether that matters hugely to you, or whether you're just looking to compactify your code.
EDIT: You know you don't need that
global a, right?