-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathapp.py
More file actions
278 lines (247 loc) · 8.05 KB
/
app.py
File metadata and controls
278 lines (247 loc) · 8.05 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python
# encoding: utf-8
"""
The minigame 2048 in python with Flask Web GUI
"""
import random
from flask import Flask, render_template, jsonify, request, session
import os
app = Flask(__name__)
app.secret_key = os.urandom(24)
# 颜色配置 - 为不同数字设置背景色
TILE_COLORS = {
0: {'bg': '#cdc1b4', 'color': '#776e65'},
2: {'bg': '#eee4da', 'color': '#776e65'},
4: {'bg': '#ede0c8', 'color': '#776e65'},
8: {'bg': '#f2b179', 'color': '#f9f6f2'},
16: {'bg': '#f59563', 'color': '#f9f6f2'},
32: {'bg': '#f67c5f', 'color': '#f9f6f2'},
64: {'bg': '#f65e3b', 'color': '#f9f6f2'},
128: {'bg': '#edcf72', 'color': '#f9f6f2'},
256: {'bg': '#edcc61', 'color': '#f9f6f2'},
512: {'bg': '#edc850', 'color': '#f9f6f2'},
1024: {'bg': '#edc53f', 'color': '#f9f6f2'},
2048: {'bg': '#edc22e', 'color': '#f9f6f2'},
}
def get_tile_style(value):
"""获取数字格子的样式"""
if value in TILE_COLORS:
return TILE_COLORS[value]
# 对于更大的数字,使用默认颜色
return {'bg': '#3c3a32', 'color': '#f9f6f2'}
def init():
"""初始化游戏矩阵"""
matrix = [0 for i in range(16)]
random_lst = random.sample(range(16), 2)
matrix[random_lst[0]] = matrix[random_lst[1]] = 2
return matrix
def get_random_number(matrix):
"""
根据游戏规则获取随机数
- 基础随机数:2, 4
- 当矩阵中有数字超过128时,可能出现16、32
"""
max_val = max(matrix)
if max_val > 128:
# 有数字超过128,可以出现16、32
weights = [50, 30, 15, 5] # 2, 4, 8, 16, 32 的权重
choices = [2, 4, 8, 16, 32]
return random.choices(choices, weights=weights + [5])[0]
else:
# 基础随机数 2 和 4
weights = [80, 20]
choices = [2, 4]
return random.choices(choices, weights=weights)[0]
def move(matrix, direction):
"""移动矩阵"""
matrix = list(matrix) # 创建副本
mergedList = []
changed = False
if direction == 'w': # 上
for i in range(16):
j = i
while j - 4 >= 0:
if matrix[j-4] == 0 and matrix[j] != 0:
matrix[j-4] = matrix[j]
matrix[j] = 0
changed = True
elif matrix[j-4] == matrix[j] and matrix[j] != 0 and j not in mergedList:
matrix[j-4] *= 2
matrix[j] = 0
mergedList.append(j-4)
mergedList.append(j)
changed = True
j -= 4
elif direction == 's': # 下
for i in range(15, -1, -1):
j = i
while j + 4 < 16:
if matrix[j+4] == 0 and matrix[j] != 0:
matrix[j+4] = matrix[j]
matrix[j] = 0
changed = True
elif matrix[j+4] == matrix[j] and matrix[j] != 0 and j not in mergedList:
matrix[j+4] *= 2
matrix[j] = 0
mergedList.append(j)
mergedList.append(j+4)
changed = True
j += 4
elif direction == 'a': # 左
for i in range(16):
j = i
while j % 4 != 0:
if matrix[j-1] == 0 and matrix[j] != 0:
matrix[j-1] = matrix[j]
matrix[j] = 0
changed = True
elif matrix[j-1] == matrix[j] and matrix[j] != 0 and j not in mergedList:
matrix[j-1] *= 2
matrix[j] = 0
mergedList.append(j-1)
mergedList.append(j)
changed = True
j -= 1
elif direction == 'd': # 右
for i in range(15, -1, -1):
j = i
while j % 4 != 3:
if matrix[j+1] == 0 and matrix[j] != 0:
matrix[j+1] = matrix[j]
matrix[j] = 0
changed = True
elif matrix[j+1] == matrix[j] and matrix[j] != 0 and j not in mergedList:
matrix[j+1] *= 2
matrix[j] = 0
mergedList.append(j)
mergedList.append(j+1)
changed = True
j += 1
return matrix, changed
def insert(matrix):
"""在空位置插入随机数"""
getZeroIndex = []
for i in range(16):
if matrix[i] == 0:
getZeroIndex.append(i)
if getZeroIndex:
randomZeroIndex = random.choice(getZeroIndex)
matrix[randomZeroIndex] = get_random_number(matrix)
return matrix
def is_over(matrix):
"""检查游戏是否结束"""
if 0 in matrix:
return False
else:
for i in range(16):
if i % 4 != 3:
if matrix[i] == matrix[i+1]:
return False
if i < 12:
if matrix[i] == matrix[i+4]:
return False
return True
def is_win(matrix):
"""检查是否获胜"""
return 2048 in matrix
@app.route('/')
def index():
"""渲染游戏页面"""
return render_template('index.html')
@app.route('/api/init', methods=['POST'])
def api_init():
"""初始化游戏API"""
matrix = init()
session['matrix'] = matrix
session['score'] = 0
session['history'] = [list(matrix)]
# 为每个格子添加样式信息
tiles = []
for i, val in enumerate(matrix):
style = get_tile_style(val)
tiles.append({
'value': val,
'index': i,
'bg': style['bg'],
'color': style['color']
})
return jsonify({
'matrix': matrix,
'tiles': tiles,
'score': 0,
'game_over': False,
'win': False
})
@app.route('/api/move', methods=['POST'])
def api_move():
"""移动API"""
data = request.get_json()
direction = data.get('direction')
matrix = session.get('matrix', [0]*16)
score = session.get('score', 0)
history = session.get('history', [])
new_matrix, changed = move(matrix, direction)
if changed:
# 计算得分(合并产生的数值)
for i in range(16):
if new_matrix[i] != matrix[i] and matrix[i] != 0:
# 这是一个合并操作
pass
new_matrix = insert(new_matrix)
history.append(list(new_matrix))
session['matrix'] = new_matrix
session['history'] = history
session['score'] = score
# 为每个格子添加样式信息
tiles = []
for i, val in enumerate(new_matrix):
style = get_tile_style(val)
tiles.append({
'value': val,
'index': i,
'bg': style['bg'],
'color': style['color']
})
return jsonify({
'matrix': new_matrix,
'tiles': tiles,
'score': score,
'changed': changed,
'game_over': is_over(new_matrix),
'win': is_win(new_matrix)
})
@app.route('/api/back', methods=['POST'])
def api_back():
"""撤销操作API"""
history = session.get('history', [])
if len(history) > 1:
history.pop()
matrix = list(history[-1])
session['matrix'] = matrix
session['history'] = history
# 为每个格子添加样式信息
tiles = []
for i, val in enumerate(matrix):
style = get_tile_style(val)
tiles.append({
'value': val,
'index': i,
'bg': style['bg'],
'color': style['color']
})
return jsonify({
'matrix': matrix,
'tiles': tiles,
'score': session.get('score', 0),
'success': True
})
return jsonify({
'success': False,
'message': '无法继续撤销'
})
@app.route('/api/new_game', methods=['POST'])
def api_new_game():
"""新游戏API"""
return api_init()
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)