Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Behind 2048 by TovarischZhukov
over = [['G', 'A', 'M', 'E'],
['O', 'V', 'E', 'R'],
['G', 'A', 'M', 'E'],
['O', 'V', 'E', 'R']]
win = [['U', 'W', 'I', 'N'],
['U', 'W', 'I', 'N'],
['U', 'W', 'I', 'N'],
['U', 'W', 'I', 'N']]
def replace(data,i,j,f,checked):
while j>=0 if f else i>=0:
l=i if f else i-1
k=j-1 if f else j
if (k>=0) if f else (l>=0):
if data[l][k]==0: data[l][k]=data[i][j]; data[i][j]=0
elif data[l][k]==data[i][j]and (l,k)not in checked: data[l][k]=data[i][j]*2; data[i][j]=0;checked.append((l,k));break
else: break
if f: j-=1
else: i-=1
def move2048(data, move):
a=["up","down","right"]
checked=[]
if move==a[1]: data=data[::-1]
for i,el in enumerate(data):
if move==a[2]: data[i]=data[i][::-1]
for j,_ in enumerate(el):
if data[i][j]: replace(data,i,j,move not in a[:2],checked)
if move==a[2]: data[i]=data[i][::-1]
if move==a[1]: data=data[::-1]
winflag = False
dataFlag = False
for i,el in enumerate(data):
for j,val in enumerate(el):
l=-1-i;k=-1-j
if val==2048: winflag=True
if data[l][k]==0 and not dataFlag: data[l][k]=2; dataFlag = True
if winflag: return win
if dataFlag: return data
return over
Jan. 22, 2016