-
Notifications
You must be signed in to change notification settings - Fork 0
/
move.py
84 lines (69 loc) · 2.02 KB
/
move.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
72
73
74
75
76
77
78
79
80
81
82
83
def zeros_to_right(arr):
number_of_zeros = arr.count(0)
for i in range(0, number_of_zeros):
arr.remove(0)
arr.append(0)
def zeros_to_left(arr):
number_of_zeros = arr.count(0)
for i in range(0, number_of_zeros):
arr.remove(0)
for i in range(0, number_of_zeros):
arr.insert(0, 0)
def zeros_to_bottom(arr, column):
temp = []
for j in range(len(arr)):
temp.append(arr[j][column])
zeros_to_right(temp)
for j in range(len(arr)):
arr[j][column] = temp[j]
def zeros_to_top(arr, column):
temp = []
for j in range(len(arr)):
temp.append(arr[j][column])
zeros_to_left(temp)
for j in range(len(arr)):
arr[j][column] = temp[j]
def right(arr):
score = 0
for i in range(len(arr)):
zeros_to_left(arr[i])
for j in range(len(arr) - 1, 0, -1):
if arr[i][j] == arr[i][j - 1]:
arr[i][j] *= 2
score += arr[i][j]
arr[i][j - 1] = 0
zeros_to_left(arr[i])
return score
def left(arr):
score = 0
for i in range(len(arr)):
zeros_to_right(arr[i])
for j in range(len(arr) - 1):
if arr[i][j] == arr[i][j + 1]:
arr[i][j] *= 2
score += arr[i][j]
arr[i][j + 1] = 0
zeros_to_right(arr[i])
return score
def down(arr):
score = 0
for i in range(len(arr)):
zeros_to_top(arr, i)
for j in range(len(arr) - 1, 0, -1):
if arr[j][i] == arr[j - 1][i]:
arr[j][i] *= 2
score += arr[j][i]
arr[j - 1][i] = 0
zeros_to_top(arr, i)
return score
def up(arr):
score = 0
for i in range(len(arr)):
zeros_to_bottom(arr, i)
for j in range(len(arr) - 1):
if arr[j][i] == arr[j + 1][i]:
arr[j][i] *= 2
score += arr[j][i]
arr[j + 1][i] = 0
zeros_to_bottom(arr, i)
return score