-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.py
53 lines (39 loc) · 1.44 KB
/
tictactoe.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
from flask import Flask, jsonify, render_template, request
from typing import List
import json
from algorithms.finished_game import FinishedGame
from algorithms.tic_tac_toe import TicTacToe
ttt = TicTacToe()
app = Flask(__name__, static_url_path='/static')
@app.route('/')
def index():
return render_template('main.html')
@app.route('/game_tree', methods=['GET', 'POST'])
async def parse_request():
# Grab the state of the map plus the move.
board = json.loads(request.form['map'])
try:
indexes = await ttt.move(tuple("-12".index(value) for value in board))
except FinishedGame as e:
if e is FinishedGame.WON:
return jsonify({"status": "A.I win's!", "data": board})
elif e is FinishedGame.TIED:
return jsonify({"status": "Tied", "data": board})
else:
return jsonify({"status": "You Win", "data": board})
new_board = [
"-12"[index]
for index in indexes
]
try:
await ttt.move(indexes)
return jsonify({"status": "continue", "data": new_board})
except FinishedGame as e:
if e is FinishedGame.WON:
return jsonify({"status": "You Win", "data": new_board})
elif e is FinishedGame.TIED:
return jsonify({"status": "Tied", "data": new_board})
else:
return jsonify({"status": "A.I win's!", "data": new_board})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)