-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
48 lines (34 loc) · 1.26 KB
/
Copy pathapp.py
File metadata and controls
48 lines (34 loc) · 1.26 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
from flask import Flask, render_template, request, jsonify
from optimal_solver import allowed_rows_cols, state_to_num, num_to_state
app = Flask(__name__)
@app.route("/api", methods=['POST'])
def zoe_backend():
data = request.get_json()
r, c = data['rows'], data['cols']
if (r, c) not in allowed_rows_cols:
return jsonify({"message": "error"})
for i, k in enumerate(data['state']):
if k == 0:
data['state'][i] = 1
elif k == 1:
data['state'][i] = -1
elif k == -2:
data['state'][i] = 0
state_num = state_to_num(data['state'], data['turn'], r, c, state_dim=1)
if allowed_rows_cols[(r, c)]['par'][state_num] == -1:
return jsonify({"message": "game end"})
best_move = num_to_state(allowed_rows_cols[(r, c)]['par'][state_num], r, c)[0]
for i, k in enumerate(best_move):
if k == 1:
best_move[i] = 0
elif k == -1:
best_move[i] = 1
elif k == 0:
best_move[i] = -2
return jsonify(
{"message": "success", "state_num": allowed_rows_cols[(r, c)]['par'][state_num], "cpu_move": best_move})
@app.route("/")
def zoe_frontend():
return render_template("index.html")
if __name__ == '__main__':
app.run()