Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
14 lines solution in Creative category for Behind 2048 by CDG.Axel
def transpose(state):
return list(map(list, zip(*state)))
def line_left(line):
line = list(filter(None, line)) + [0]*line.count(0)
for i in range(3):
if line[i] == line[i+1]!=0: line=line[:i]+[2*line[i]]+line[i+2:]+[0]
return line
def move2048(state, move):
rotate, stp = transpose if move in 'up down' else list, (-1) ** (move in 'right down')
state = rotate(j[::stp] for j in [line_left(i[::stp]) for i in rotate(state)])
if (win := any(2048 in i for i in state)) or not any(0 in i for i in state):
return [list('U'+'WIN')]*4 if win else [list('GAME'), list('OVER')]*2
state[(i := next(iter(i for i in range(16)[::-1] if not state[i // 4][i % 4]))) // 4][i % 4] = 2
return state
Oct. 15, 2021
Comments: