我使用了我想部署到Heroku的烧瓶制作了一个简单的Tic Tac Toe游戏。 我正在按照this视频中概述的步骤进行操作,但是我的应用无法正常工作。它在本地运行良好,但是当我在Heroku上测试时,单击“在此处播放X”并没有任何反应。如果再次单击几次,X突然出现并随机消失。如果我单击“重置游戏”,则什么也没有发生,或者出现“内部服务器错误”。
https://tictactoe--ai.herokuapp.com/
Python代码:
from flask import Flask, render_template, session, redirect, url_for
from flask_session import Session
from tempfile import mkdtemp
app = Flask(__name__)
app.config["SESSION_FILE_DIR"] = mkdtemp()
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
def tie(board):
# Returns false if board contains None values and True if all squares on board is full
for row in board:
if None in row:
return False
return True
@app.route("/")
def index():
if "board" not in session:
session["board"] = [[None, None, None], [None, None, None], [None, None, None]]
session["turn"] = "X"
return render_template("game.html", game=session["board"], turn=session["turn"])
@app.route("/play/<int:row>/<int:col>")
def play(row, col):
if session["turn"] == "X":
session["board"][row][col] = "X"
session["turn"] = "O"
else:
session["board"][row][col] = "O"
session["turn"] = "X"
return redirect(url_for("index"))
@app.route("/reset", methods=["POST"])
def reset():
if session["board"]:
session.pop("board")
return redirect(url_for("index"))
@app.context_processor
def winner():
# Columns
left = [ session["board"][0][0], session["board"][1][0], session["board"][2][0] ]
middle = [ session["board"][0][1], session["board"][1][1], session["board"][2][1] ]
right = [ session["board"][0][2], session["board"][1][2], session["board"][2][2] ]
# Rows
top_row = [ session["board"][0][0], session["board"][0][1], session["board"][0][2] ]
middle_row = [ session["board"][1][0], session["board"][1][1], session["board"][1][2] ]
bottom_row = [ session["board"][2][0], session["board"][2][1], session["board"][2][2] ]
# Horizontal = \ and /
horizontal1 = [ session["board"][0][0], session["board"][1][1], session["board"][2][2] ]
horizontal2 = [ session["board"][0][2], session["board"][1][1], session["board"][2][0] ]
check = left, middle, right, horizontal1, horizontal2, top_row, middle_row, bottom_row
for i in check:
if i == ["X", "X", "X"]:
return dict(winner="X is the winner")
if i == ["O", "O", "O"]:
return dict(winner="O is the winner")
full_board = tie(session["board"])
# Returns empty string if board is not full
if not full_board:
return dict(winner="")
# If all squares have values and nobody has won, it is a tie
return dict(winner="Tie!")
if __name__ == "main":
app.debug = True
app.run()
HTML
<!DOCTYPE html>
<html>
<head>
<title>Tic Tac Toe</title>
<style>
table {
border-collapse: collapse;
}
td {
border: 1px solid black;
width: 150px;
height: 150px;
font-size: 30px;
text-align: center;
}
td > a {
font-size: 18px;
}
</style>
</head>
<body>
<table>
{% for i in range(3) %}
<tr>
{% for j in range(3) %}
<td>
{% if game[i][j] %}
{{ game[i][j] }}
{% else %}
<a href="{{ url_for('play', row=i, col=j) }}">Play {{ turn }} here.</a>
{% endif %}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% if winner != 0 %}
{{winner}}
{% endif %}
<form action="/reset" method="post">
<button type="submit">Reset game</button>
</form>
</body>
</html>
日志:
2020-09-22T06:58:44.453325+00:00 app[web.1]: rv = self.dispatch_request()
2020-09-22T06:58:44.453325+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
2020-09-22T06:58:44.453326+00:00 app[web.1]: return self.view_functions[rule.endpoint](**req.view_args)
2020-09-22T06:58:44.453326+00:00 app[web.1]: File "/app/application.py", line 65, in reset
2020-09-22T06:58:44.453327+00:00 app[web.1]: if session["board"]:
2020-09-22T06:58:44.453327+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/werkzeug/local.py", line 377, in <lambda>
2020-09-22T06:58:44.453328+00:00 app[web.1]: __getitem__ = lambda x, i: x._get_current_object()[i]
2020-09-22T06:58:44.454027+00:00 app[web.1]: KeyError: 'board'
2020-09-22T06:58:44.460397+00:00 heroku[router]: at=info method=POST path="/reset" host=tictactoe--ai.herokuapp.com request_id=3384212e-52c7-4a7d-ab32-3ec58c133e29 fwd="178.164.96.83" dyno=web.1 connect=1ms service=13ms status=500 bytes=546 protocol=https
2020-09-22T06:58:44.461261+00:00 app[web.1]: 10.39.211.254 - - [22/Sep/2020:06:58:44 +0000] "POST /reset HTTP/1.1" 500 290 "https://tictactoe--ai.herokuapp.com/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36"
问题似乎出在session [“ board”]和session [“ turn”]的KeyErrors中。 当我演奏一些动作时,有时它会出现在板上,有时则不会。如果我刷新页面两次,它似乎已经存储了多个不同的板,并且每次重新加载页面时都加载了一个不同的板。 当它在本地运行时,我无法重新创建错误。任何想法是什么原因造成的?
答案 0 :(得分:0)
启动Flask应用程序绑定到Heroku提供的PORT
p = int(os.environ.get("PORT", 5000))
app.run(debug=True, port=p, host='0.0.0.0')