-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculate.py
More file actions
185 lines (168 loc) · 6.71 KB
/
Calculate.py
File metadata and controls
185 lines (168 loc) · 6.71 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# 类名:Calculate
# 功能:计算
# 时间:2018-5-1 晚
# 作者:beants
class Calculate:
def __init__(self):
pass
def set_a_random(self, matrix):
import random
num_kong = 0
list_kong = []
for i in range(4):
for j in range(4):
if matrix[i][j] == 0:
list_kong.append([i, j])
num_kong += 1
if num_kong == 0:
return False
num = random.randint(0, num_kong - 1)
random_num = random.randint(1, 2) * 2
matrix[list_kong[num][0]][list_kong[num][1]] = random_num
return matrix
def up(self, matrix):
# 循环计算
# 相加
# 竖着的
temp_score = 0
for i in range(4):
# 横着的
for j in range(4):
# 如果有数的这个位置在最上面一行
if j == 0:
pass
else:
# 如果两个数相等,则相加
if matrix[j][i] == matrix[j - 1][i] and matrix[j][i] is not 0:
matrix[j - 1][i] += matrix[j][i]
matrix[j][i] = 0
# 计算分数
temp_score += matrix[j - 1][i]
for i in range(4):
# 横着的
for t in range(4):
for j in range(4):
# 相加
# 如果J置在最上面一行
if j == 0:
pass
else:
# 向上移动
if matrix[j - 1][i] == 0 and matrix[j][i] != 0:
matrix[j - 1][i] = matrix[j][i]
matrix[j][i] = 0
# 向上移动之后还需要判断是不是继续相加
if matrix[j - 1][i] == matrix[j - 1 - 1][i] and matrix[j - 1][i] is not 0:
matrix[j - 1 - 1][i] += matrix[j - 1][i]
matrix[j - 1][i] = 0
# 计算分数
temp_score += matrix[j - 1 - 1][i]
return temp_score
def down(self, matrix):
# 循环计算
# 相加
# 竖着的
temp_score = 0
for i in range(4):
# 横着的
for j in range(4):
# 如果有数的这个位置在最上面一行
if j == 3:
pass
else:
# 如果两个数相等,则相加
if matrix[3 - j][i] == matrix[3 - j - 1][i] and matrix[3 - j][i] != 0:
matrix[3 - j][i] += matrix[3 - j - 1][i]
matrix[3 - j - 1][i] = 0
temp_score += matrix[3 - j][i]
for i in range(4):
# 横着的
for t in range(4):
for j in range(4):
# 相加
# 如果J置在最上面一行
if j == 3:
pass
else:
# 向下移动
if matrix[j + 1][i] == 0 and matrix[j][i] != 0:
matrix[j + 1][i] = matrix[j][i]
matrix[j][i] = 0
# +1 是因为元素交换了位置
if matrix[3 - j + 1][i] == matrix[3 - j - 1 + 1][i] and matrix[3 - j + 1][i] != 0:
matrix[3 - j + 1][i] += matrix[3 - j - 1 + 1][i]
matrix[3 - j - 1 + 1][i] = 0
temp_score += matrix[3 - j + 1][i]
return temp_score
def left(self, matrix):
# 循环计算
# 相加
# 横着的
temp_score = 0
for j in range(4):
# 竖着的
for i in range(4):
# 如果有数的这个位置在最上面一行
if i == 0:
pass
else:
# 如果两个数相等,则相加
if matrix[j][i] == matrix[j][i - 1] and matrix[j][i] != 0:
matrix[j][i - 1] += matrix[j][i]
matrix[j][i] = 0
temp_score += matrix[j][i - 1]
for j in range(4):
# 横着的
for t in range(4):
for i in range(4):
# 相加
# 如果I置在最上面一列
if i == 0:
pass
else:
# 向左移动
if matrix[j][i - 1] == 0 and matrix[j][i] != 0:
matrix[j][i - 1] = matrix[j][i]
matrix[j][i] = 0
if matrix[j][i - 1] == matrix[j][i - 1 - 1] and matrix[j][i - 1] != 0:
matrix[j][i - 1 - 1] += matrix[j][i - 1]
matrix[j][i - 1] = 0
temp_score += matrix[j][i - 1 - 1]
return temp_score
def right(self, matrix):
# 循环计算
# 相加
# 横着的
temp_score = 0
for j in range(4):
# 竖着的
for i in range(4):
# 如果有数的这个位置在最上面一行
if i == 3:
pass
else:
# 如果两个数相等,则相加
if matrix[j][3 - i] == matrix[j][3 - i - 1] and matrix[j][3 - i] != 0:
matrix[j][3 - i - 1] += matrix[j][3 - i]
matrix[j][3 - i] = 0
temp_score += matrix[j][3 - i - 1]
for j in range(4):
# 横着的
for t in range(4):
for i in range(4):
# 相加
# 如果I置在最上面一列
if i == 3:
pass
else:
# 向上移动
if matrix[j][i + 1] == 0 and matrix[j][i] != 0:
matrix[j][i + 1] = matrix[j][i]
matrix[j][i] = 0
if matrix[j][3 - i + 1] == matrix[j][3 - i - 1 + 1] and matrix[j][3 - i + 1] != 0:
matrix[j][3 - i - 1 + 1] += matrix[j][3 - i + 1]
matrix[j][3 - i + 1] = 0
temp_score += matrix[j][3 - i - 1 + 1]
return temp_score
if __name__ == '__main__':
temp = Calculate().up(matrix=[[0, 0, 0, 0], [4, 0, 0, 0], [2, 0, 0, 0], [2, 0, 0, 0]])