-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathapp.py
More file actions
160 lines (142 loc) · 4.5 KB
/
app.py
File metadata and controls
160 lines (142 loc) · 4.5 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
#!/usr/bin/env python
# encoding: utf-8
"""
2048 Game with Flask GUI
"""
import random
from flask import Flask, render_template, jsonify, request, session
app = Flask(__name__)
app.secret_key = '2048_game_secret_key'
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 move(matrix, direction):
mergedList = []
score = 0
if direction == 'w':
for i in range(16):
j = i
while j - 4 >= 0:
if matrix[j-4] == 0:
matrix[j-4] = matrix[j]
matrix[j] = 0
elif matrix[j-4] == matrix[j] and j not in mergedList and j-4 not in mergedList:
matrix[j-4] *= 2
score += matrix[j-4]
matrix[j] = 0
mergedList.append(j-4)
mergedList.append(j)
j -= 4
elif direction == 's':
for i in range(15, -1, -1):
j = i
while j + 4 < 16:
if matrix[j+4] == 0:
matrix[j+4] = matrix[j]
matrix[j] = 0
elif matrix[j+4] == matrix[j] and j not in mergedList and j+4 not in mergedList:
matrix[j+4] *= 2
score += matrix[j+4]
matrix[j] = 0
mergedList.append(j)
mergedList.append(j+4)
j += 4
elif direction == 'a':
for i in range(16):
j = i
while j % 4 != 0:
if matrix[j-1] == 0:
matrix[j-1] = matrix[j]
matrix[j] = 0
elif matrix[j-1] == matrix[j] and j not in mergedList and j-1 not in mergedList:
matrix[j-1] *= 2
score += matrix[j-1]
matrix[j] = 0
mergedList.append(j-1)
mergedList.append(j)
j -= 1
else:
for i in range(15, -1, -1):
j = i
while j % 4 != 3:
if matrix[j+1] == 0:
matrix[j+1] = matrix[j]
matrix[j] = 0
elif matrix[j+1] == matrix[j] and j not in mergedList and j+1 not in mergedList:
matrix[j+1] *= 2
score += matrix[j+1]
matrix[j] = 0
mergedList.append(j)
mergedList.append(j+1)
j += 1
return matrix, score
def insert(matrix):
getZeroIndex = []
for i in range(16):
if matrix[i] == 0:
getZeroIndex.append(i)
if not getZeroIndex:
return matrix
randomZeroIndex = random.choice(getZeroIndex)
max_num = max(matrix)
if max_num > 128:
candidates = [2, 4, 8, 16, 32]
weights = [60, 25, 10, 4, 1]
else:
candidates = [2, 4, 8]
weights = [70, 25, 5]
total = sum(weights)
r = random.randint(1, total)
for num, w in zip(candidates, weights):
if r <= w:
matrix[randomZeroIndex] = num
break
r -= w
return matrix
def isOver(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 has_2048(matrix):
return 2048 in matrix
def get_game_state():
if 'matrix' not in session:
session['matrix'] = init()
session['score'] = 0
return {
'matrix': session['matrix'],
'score': session['score'],
'game_over': isOver(session['matrix']),
'win': has_2048(session['matrix'])
}
@app.route('/')
def index():
return render_template('index.html')
@app.route('/new_game', methods=['POST'])
def new_game():
session['matrix'] = init()
session['score'] = 0
return jsonify(get_game_state())
@app.route('/move', methods=['POST'])
def make_move():
data = request.get_json()
direction = data.get('direction')
old_matrix = list(session['matrix'])
matrix, add_score = move(session['matrix'], direction)
if matrix != old_matrix:
insert(matrix)
session['score'] += add_score
session['matrix'] = matrix
return jsonify(get_game_state())
if __name__ == '__main__':
app.run(debug=True)