# 27 Sep 2014, 16:37

Recently I tried implementing a cool game called 2048 in Python and make it as short as possible. Thinking I did a decent job, I challenged a friend to give it a go. Blown away how much shorter it turned out to be, all I can say is - kudos to Veky.

``````
import random as R,tkinter as T
def M(n,q=lambda m:list(map(list,zip(*m[::-1]))),u={"key":bool,"reverse":1}):
m=b
for _ in range(4-n):m=q(m)
for r in m:
r.sort(**u)
for j in range(3):
if r[j+1]==r[j]:r[j:j+2]=2*r[j],0
r.sort(**u)
for _ in range(n):m=q(m)
return m*(m!=b)
def A(q=8**8-1):
k,l=R.choice([divmod(i,4)for i,j in enumerate(sum(b,[]))if not j])
b[k][l]=R.choice((2,2,4))
for l,d in zip(L,sum(b,[])):l.config(text=d or'',bg='#'+format(q-d*999,"06x"))
def K(e):
c=M(dict(Left=0,Up=1,Right=2,Down=3)[e.keysym])
if c:b[:]=c;A()
any(map(M,F))or[x.config(bg='red',text='<E2><98><B9>')for x in L]
F=(0,1,2,3);b,t=[4*[0]for _ in F],T.Tk();t.grid()
L=[T.Button(t,height=1,width=3,font=("Helvetica",24))for _ in 4*F]
[L[i*4+j].grid(row=i,column=j)for i in F for j in F]
A();t.bind('<Key>',K);t.mainloop()

``````