Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Straightforward solution in Clear category for Behind 2048 by nickie
X = list("UWINGAMEOVER")
WIN, LOSE = [X[:4]]*4, [X[4:8],X[8:]]*2
def reverse(state):
return list(map(list, map(reversed, state)))
def transpose(state):
return list(map(list, zip(*state)))
def shift_left(row):
j, canMerge = 0, False
for i, col in enumerate(row):
if canMerge and col == row[j-1]:
row[j-1] *= 2
canMerge = False
elif col > 0:
row[j] = col
j += 1
canMerge = True
for i in range(j, len(row)):
row[i] = 0
return row
def left(state):
return list(map(shift_left, state))
def right(state):
return reverse(map(shift_left, reverse(state)))
def up(state):
return transpose(left(transpose(state)))
def down(state):
return transpose(right(transpose(state)))
def move2048(state, move):
s = list(eval(move)(state))
added = False
for i, row in reversed(list(enumerate(s))):
for j, col in reversed(list(enumerate(row))):
if col == 2048:
return WIN
if not added and col == 0:
s[i][j] = 2
added = True
if not added:
return LOSE
return s
Sept. 14, 2014