-
Notifications
You must be signed in to change notification settings - Fork 0
/
webapp.py
executable file
·59 lines (47 loc) · 1.81 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
import sys
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"
if len(sys.argv) == 2:
agent_name = sys.argv[1].split("=")[-1]
if agent_name == "emagent":
from game2048.agents import ExpectiMaxAgent as TestAgent
elif agent_name == "pagent":
from task.agents import PlanningAgent as TestAgent
elif agent_name == "cnnagent":
from task.agents import CNNAgent as TestAgent
else:
print("WARNING: Agent class doesn't exist.")
else:
from game2048.agents import RandomAgent as TestAgent
print("WARNING: You are now using a RandomAgent.")
from game2048.game import Game
game = Game(size=GAME_SIZE, score_to_win=SCORE_TO_WIN)
agent = TestAgent(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