我的井字游戏的列检查器有什么问题?

时间:2019-11-29 04:00:02

标签: python

这是我的井字游戏Web应用程序。 game.html未包含在内,但可以根据需要添加。下面的python代码可以正确识别玩家X或O连续有3个符号,并为他们赢得胜利。但是,它无法识别玩家是否在某栏中有3个符号,尽管仔细地理解了它,我也不明白为什么。

谁能告诉我我的列检查算法出了什么问题?

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)


@app.route("/")
def index():
    if "board" not in session:
        session["board"] = [[None, None, None], [None, None, None], [None, None, None]]
        session["turn"] = "X"
    winner = None
    for row in session["board"]:
        if set(row) == set("X"):
            winner = "X"
        if set(row) == set("O"):
            winner = "O"
    for i in range(3):
        col_lst = []
        for row in session["board"]:
            col_lst.append(row[i])
        if set(col_lst) == "X":
            winner = "X"
        if set(col_lst) == "O":
            winner = "O"
    return render_template("game.html", game=session["board"], turn=session["turn"], winner=winner)


@app.route("/play/<int:row>/<int:col>")
def play(row, col):
    session["board"][row][col] = session["turn"]
    if session["turn"] == "X":
        session["turn"] = "O"
    else:
        session["turn"] = "X"
    return redirect(url_for("index"))

1 个答案:

答案 0 :(得分:0)

我发现了问题所在。我必须将“ X”和“ O”放在类似于行算法的集合函数中。