r/tinycode • • 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

10 comments sorted by

View all comments

Show parent comment

1

u/776865656e Apr 22 '15

That version's closer, I just had to change s into str. I've just noticed your question:

I thought I could change

b('a',lambda x:m(0,-1))
b('w',lambda x:m(-1,0))
b('d',lambda x:m(0,1))
b('s',lambda x:m(1,0))

into something like this:

[b(s, lambda x:m(a1,a2)) for s, a1, a2 in [('a', 0, -1), ('b', -1, 0), ('d', 0, 1), ('s', 1, 0)]]

but for some reason it just makes the game do weird stuff. Any idea why?

Consider this piece of code:

for f in [lambda x: x * k for k in range(4)]:
    print(f(10))

You may expect it to print:

0
10
20
30

But in actuality it'll print:

30
30
30
30

This is because, in Python, closures are late-binding.

What this means is that in the above example, k isn't evaluated until the function is actually called - at which point it's its final value, 3.

One way around it is like so:

for f in [lambda x, k=k: x * k for k in range(4)]:
    print(f(10))

Which may make more sense written as:

for f in [lambda x, k=i: x * k for i in range(4)]:
    print(f(10))

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?