from efinfo import * import copy tiles = [[4, -1, 4, 8], [16, 32, 64, 128], [256, -1, -1, 2048], [-1, -1, -1, -1]] def shiftAndCollapseLine(inline): ##Idee: Kippen, ersetzen, kippen #einmal kippen #idee: alle nicht -1 sammeln, dann -1 wieder anhängen #leere tiles temp = [] #sammeln for j in inline: if j != -1: temp.append(j) #anhängen: Python'esquer Weg zum Anhängen temp += [-1] * inline.count(-1) #tiles umdrehen temp.reverse() #identicshe zellen verdoppeln und durch -1 ersetzen for j in range(len(temp) - 1): if temp[j] == temp[j + 1] and temp[j] != -1: temp[j+1] = 2 * temp[j] temp[j] = -1 inline=[] #wieder sammeln und anhängen for j in temp: if j != -1: inline.append(j) inline+=[-1] * temp.count(-1) #tiles umdrehen inline.reverse() return(inline) def shiftAndCollapseGrid(tiles): # 'leere' tiles mit länge von tiles temp = [-1]*len(tiles) for i in range(len(tiles)): temp[i] = shiftAndCollapseLine(tiles[i]) return(temp) def rotateGrid(tiles): returnlist = copy.deepcopy(tiles) #sehr Pythonish return(list(map(list, zip(*returnlist[::-1])))) printDoubleLists(tiles) print repeat 4: tiles = rotateGrid(tiles) printDoubleLists(tiles)