-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
294 lines (227 loc) · 8.87 KB
/
app.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
from flask import Flask, render_template, request, redirect, jsonify, current_app, url_for
from game.PuzzleGenerator import PuzzleGenerator
from game.GameManager import GameManager
from game.hai import HAI
from game.BasicAI import ComputerAI as BasicAI
from game.RandomAI import RandomAI
from game.ComputerAI import ComputerAI as MRV
import os
from copy import deepcopy
from time import process_time
app = Flask(__name__)
SECRET_KEY = os.urandom(32)
app.config['SECRET_KEY'] = SECRET_KEY
@app.route('/', methods=['GET','POST'])
def home():
if request.method == 'POST':
level = request.form['level']
app.config['LEVEL'] = level
return redirect(url_for('game'))
else:
return render_template('home.html')
@app.route('/game')
def game():
app.config['GAME'] = GameManager(None, int(current_app.config['LEVEL']))
grid = current_app.config['GAME'].puzzle
grid.displayGrid()
colValues = grid.colValues
content = []
for r in range(grid.size):
row = [grid.rowValues[r]]
cells = [[grid.getCellValue((r, c)), (r,c)] for c in range(len(grid.map[r]))]
row.append(cells)
content.append(row)
size = grid.size
return render_template('game.html', colValues=colValues, content=content, size=size)
@app.route('/get_move')
def get_move():
pos = request.args.get('pos', '')[1:-1]
pos = pos.split(',')
pos = (int(pos[0]), int(pos[1]))
game = current_app.config['GAME']
game.play(pos)
grid = game.puzzle
status = game.checkSolved()
return jsonify(content=grid.getCellValue(pos), status=status)
############################# Random AI Page ###############################
@app.route('/random-home', methods=['GET','POST'])
def random_home():
if request.method == 'POST':
level = request.form['random-level']
app.config['RANDOM_LEVEL'] = level
return redirect(url_for('random'))
else:
return render_template('random-home.html')
@app.route('/random')
def random():
app.config['RANDOM-AI'] = RandomAI()
app.config['GAME'] = GameManager(current_app.config['RANDOM-AI'], int(current_app.config['RANDOM_LEVEL']))
grid = current_app.config['GAME'].puzzle
grid.displayGrid()
colValues = grid.colValues
content = []
for r in range(grid.size):
row = [grid.rowValues[r]]
cells = [[grid.getCellValue((r, c)), (r,c)] for c in range(len(grid.map[r]))]
row.append(cells)
content.append(row)
size = grid.size
return render_template('random.html', colValues=colValues, content=content, size=size)
@app.route('/solve_random')
def solve_random():
t0 = process_time()
app.config['GAME'].start()
t0 = process_time() - t0
tents = app.config['GAME'].solution.getTents()
return jsonify(solved_content=tents, time=t0)
############################# Backtracking AI Page ###############################
@app.route('/backtracking-home', methods=['GET','POST'])
def backtracking_home():
if request.method == 'POST':
level = request.form['bt-level']
app.config['BT_LEVEL'] = level
return redirect(url_for('backtracking'))
else:
return render_template('backtracking-home.html')
@app.route('/backtracking')
def backtracking():
app.config['BT-AI'] = BasicAI()
app.config['GAME'] = GameManager(current_app.config['BT-AI'], int(current_app.config['BT_LEVEL']))
grid = current_app.config['GAME'].puzzle
grid.displayGrid()
colValues = grid.colValues
content = []
for r in range(grid.size):
row = [grid.rowValues[r]]
cells = [[grid.getCellValue((r, c)), (r,c)] for c in range(len(grid.map[r]))]
row.append(cells)
content.append(row)
size = grid.size
return render_template('backtracking.html', colValues=colValues, content=content, size=size)
@app.route('/solve_backtracking')
def solve_backtracking():
t0 = process_time()
app.config['GAME'].start()
t0 = process_time() - t0
tents = [tent[1] for tent in current_app.config['BT-AI'].solvedTents]
return jsonify(solved_content=tents, time=t0)
############################# Heuristic Backtracking AI Page ###############################
@app.route('/heuristic-ai-home', methods=['GET','POST'])
def heuristic_ai_home():
if request.method == 'POST':
level = request.form['hai-level']
app.config['HAI_LEVEL'] = level
return redirect(url_for('heuristic_ai'))
else:
return render_template('heuristic-ai-home.html')
@app.route('/heuristic-ai')
def heuristic_ai():
app.config['GAME'] = GameManager(None, int(current_app.config['HAI_LEVEL']))
grid = current_app.config['GAME'].puzzle
grid.displayGrid()
colValues = grid.colValues
content = []
for r in range(grid.size):
row = [grid.rowValues[r]]
cells = [[grid.getCellValue((r, c)), (r,c)] for c in range(len(grid.map[r]))]
row.append(cells)
content.append(row)
size = grid.size
return render_template('heuristic-ai.html', colValues=colValues, content=content, size=size)
@app.route('/solve_heuristic_ai')
def solve_heuristic_ai():
game = deepcopy(current_app.config['GAME'].puzzle)
hai = HAI(game)
t0 = process_time()
app.config['HAI'] = hai.solve()
t0 = process_time() - t0
solved_grid = current_app.config['HAI']
tents = solved_grid.getTents()
return jsonify(solved_content=tents, time=t0)
############################# MRV AI Page ###############################
@app.route('/mrv-ai-home', methods=['GET','POST'])
def mrv_ai_home():
if request.method == 'POST':
level = request.form['mrv-level']
app.config['MRV_LEVEL'] = level
return redirect(url_for('mrv_ai'))
else:
return render_template('mrv-ai-home.html')
@app.route('/mrv-ai')
def mrv_ai():
app.config['MRV-AI'] = MRV()
app.config['GAME'] = GameManager(current_app.config['MRV-AI'], int(current_app.config['MRV_LEVEL']))
grid = current_app.config['GAME'].puzzle
grid.displayGrid()
colValues = grid.colValues
content = []
for r in range(grid.size):
row = [grid.rowValues[r]]
cells = [[grid.getCellValue((r, c)), (r,c)] for c in range(len(grid.map[r]))]
row.append(cells)
content.append(row)
size = grid.size
return render_template('mrv-ai.html', colValues=colValues, content=content, size=size)
@app.route('/solve_mrv_ai')
def solve_mrv_ai():
t0 = process_time()
app.config['GAME'].start()
t0 = process_time() - t0
tents = [tent[1] for tent in current_app.config['MRV-AI'].solvedTents]
return jsonify(solved_content=tents, time=t0)
############################# ALL AI Page ###############################
@app.route('/all-home', methods=['GET','POST'])
def all_home():
if request.method == 'POST':
level = request.form['all-level']
app.config['ALL_LEVEL'] = level
return redirect(url_for('all_ai'))
else:
return render_template('all-home.html')
@app.route('/all-ai')
def all_ai():
app.config['GAME'] = GameManager(None, int(current_app.config['ALL_LEVEL']))
grid = current_app.config['GAME'].puzzle
colValues = grid.colValues
content = []
for r in range(grid.size):
row = [grid.rowValues[r]]
cells = [[grid.getCellValue((r, c)), (r,c)] for c in range(len(grid.map[r]))]
row.append(cells)
content.append(row)
size = grid.size
return render_template('all-ai.html', colValues=colValues, bt_content=content, hai_content=content, mrv_content=content, random_content=content, size=size)
@app.route('/solve_all')
def solve_all():
# Backtracking AI
app.config['BT-AI'] = BasicAI()
app.config['GAME'].player = app.config['BT-AI']
t0 = process_time()
app.config['GAME'].start()
t_backtracking = process_time() - t0
bt_tents = [tent[1] for tent in current_app.config['BT-AI'].solvedTents]
# Heuristic Backtracking AI
current_app.config['GAME'].restart()
hai = HAI(current_app.config['GAME'].puzzle)
t0 = process_time()
app.config['HAI'] = hai.solve()
t_hai = process_time() - t0
hai_tents = current_app.config['HAI'].getTents()
# MRV AI
current_app.config['GAME'].restart()
app.config['MRV-AI'] = MRV()
current_app.config['GAME'].player = app.config['MRV-AI']
t0 = process_time()
app.config['GAME'].start()
t_mrv = process_time() - t0
mrv_tents = [tent[1] for tent in current_app.config['MRV-AI'].solvedTents]
# Random
# current_app.config['GAME'].restart()
# app.config['RANDOM-AI'] = RandomAI()
# current_app.config['GAME'].player = app.config['RANDOM-AI']
# t0 = process_time()
# app.config['GAME'].start()
# t_random = process_time() - t0
# random_tents = app.config['GAME'].solution.getTents()
return jsonify(bt_content=bt_tents, hai_content=hai_tents, mrv_content=mrv_tents,
bt_time=t_backtracking, hai_time=t_hai, mrv_time=t_mrv)