-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebapp.py
59 lines (44 loc) · 1.71 KB
/
webapp.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
from flask import Flask, jsonify, request
def get_flask_app(game, agent):
app = Flask(__name__)
@app.route("/")
def index():
return app.send_static_file('board.html')
@app.route("/board", methods=['GET', 'POST'])
def get_board():
direction = -1
control = "USER"
if request.method == "POST":
direction = request.json
if direction == -1:
direction = agent.step()
control = 'AGENT'
game.move(direction)
return jsonify({"board": game.board.tolist(),
"score": game.score,
"end": game.end,
"direction": direction,
"control": control})
return app
if __name__ == "__main__":
GAME_SIZE = 4
SCORE_TO_WIN = 2048
APP_PORT = 5005
APP_HOST = "localhost"
from game2048.game import Game
game = Game(size=GAME_SIZE, score_to_win=SCORE_TO_WIN)
# try:
# from game2048.agents import ExpectiMaxAgent
# agent = ExpectiMaxAgent(game=game)
# except:
# from game2048.agents import RandomAgent
# print("WARNING: Please compile the ExpectiMaxAgent first following the README.")
# print("WARNING: You are now using a RandomAgent.")
# agent = RandomAgent(game=game)
from myAgent import myAgent
agent = myAgent(game=game)
# from game2048.agents import RandomAgent
# agent = RandomAgent(game=game)
print("Run the webapp at http://<any address for your local host>:%s/" % APP_PORT)
app = get_flask_app(game, agent)
app.run(port=APP_PORT, threaded=False, host=APP_HOST) # IMPORTANT: `threaded=False` to ensure correct behavior