-
Notifications
You must be signed in to change notification settings - Fork 0
/
2048.py
75 lines (59 loc) · 1.45 KB
/
2048.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# All of the real work is here
def left(grid):
new = []
for line in grid:
#print(line)
# 0s aren't needed
while 0 in line:
line.remove(0)
#print(line)
i = 0
# combine like tiles
while i < len(line)-1:
#print(line[i], line[i+1])
if line[i] == line[i+1]:
line[i] += line[i+1]
del line[i+1]
i += 1
line.extend([0] * (4 - len(line)))
new.append(line)
return new
def right(grid):
# reverse
for i in range(4):
grid[i].reverse()
# send left
left(grid)
# reverse again
for i in range(4):
grid[i].reverse()
return grid
def up(grid):
# transpose, make 2d list, push left
new = left(list(map(list, list(zip(*grid)))))
# transpose back, make list, return
return list(map(list, list(zip(*new))))
def down(grid):
# transpose, make 2d list, push right
new = right(list(map(list, list(zip(*grid)))))
# transpose back, make list, return
return list(map(list, list(zip(*new))))
grid = []
# read grid
for i in range(4):
grid.append(list(map(int, input().split())))
move = int(input())
# apply move
if move == 0:
new = left(grid)
elif move == 1:
new = up(grid)
elif move == 2:
new = right(grid)
elif move == 3:
new = down(grid)
# print result
for line in new:
for num in line:
print(num, end=' ')
print()