Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Behind 2048 by tokiojapan55
def move2048(state, move):
def shift(line):
b = 0
for s in range(3):
for e in range(s+1, 4):
if s>=b and line[s]>0 and line[e]>0:
if line[s]==line[e]:
line[s] *= 2
line[e] = 0
b = s+2
else:
break
return ([line[n] for n in range(4) if line[n]>0]+[0,0,0,0])[:4]
VECTOR = {
'up':((0,0),(0,1),(1,0)),
'down':((3,0),(0,1),(-1,0)),
'left':((0,0),(1,0),(0,1)),
'right':((0,3),(1,0),(0,-1))}
start,dr,dc = VECTOR[move]
for y in range(4):
r,c = start
line = shift([state[r+dr[0]*y+dc[0]*x][c+dr[1]*y+dc[1]*x] for x in range(4)])
for x in range(4):
state[r+dr[0]*y+dc[0]*x][c+dr[1]*y+dc[1]*x] = line[x]
zero = False
for r,c in [(r,c) for r in range(3,-1,-1) for c in range(3,-1,-1)]:
if state[r][c]==2048:
return [list('UWIN')]*4
if state[r][c]==0 and zero==False:
state[r][c] = 2
zero = True
return state if zero else [list('GAME'),list('OVER')]*2
June 8, 2020