You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python九子莫里斯游戏:调用gameplay_board时变量值不更新

九子莫里斯游戏开发问题:变量修改后界面不更新

我在用Python开发九子莫里斯(nine men’s morris)游戏,遇到一个问题:当满足条件时,调用gameplay_board()函数后,字符串没有按预期替换。我想把a1的值从' '改为'○',但始终没生效。

我的代码:

#ring 1
a1="1"
b1="2"
c1="3"
d1="4"
e1="5"
f1="6"
g1="7"
h1="8"

#ring 2
a2="1"
b2="2"
c2="3"
d2="4"
e2="5"
f2="6"
g2="7"
h2="8"

#ring 3
a3="1"
b3="2"
c3="3"
d3="4"
e3="5"
f3="6"
g3="7"
h3="8"

def example_board():
    global a1, b1, c1, d1, e1, f1, g1, h1, a2, b2, c2, d2, e2, f2, g2, h2, a3, b3, c3, d3, e3, f3, g3, h3
    print("\n" + 
    a1 + "----------" + b1 + "----------" + c1 + "\n" + 
    "|          |          |" + "<---Ring 1" + "\n" + 
    "|  " + a2 + "-------" + b2 + "-------" + c2 + "  |" + "\n" + 
    "|  |       |       |<---Ring 2" + "\n" + 
    "|  |  " + a3 + "----" + b3 + "----" + c3 + "  |  |" + "\n" +
    "|  |  |         |<---Ring 3" + "\n" +
    d1 + "--" + d2 + "--" + d3 + "         " + e3 + "--" + e2 + "--" + e1 + "\n" + 
    "|  |  |         |  |  |"  + "\n" + 
    "|  |  " + f3 + "----" + g3 + "----" + h3 + "  |  |" + "\n" +
    "|  |       |       |  |" + "\n" + 
    "|  " + f2 + "-------" + g2 + "-------" + h2 + "  |" + "\n" + 
    "|          |          |" + "\n" +
    f1 + "----------" + g1 + "----------" + h1 + "\n" + "_______________________" + "\n")
    a1=" "
    b1=" "
    c1=" "
    d1=" "
    e1=" "
    f1=" "
    g1=" "
    h1=" "
    a2=" "
    b2=" "
    c2=" "
    d2=" "
    e2=" "
    f2=" "
    g2=" "
    h2=" "
    a3=" "
    b3=" "
    c3=" "
    d3=" "
    e3=" "
    f3=" "
    g3=" "
    h3=" "
example_board()

def gameplay_board(): 
    print("\n" + 
    " " + "----------" + " " + "----------" + " " + "\n" + 
    "|          |          |" + "\n" + 
    "|  " + " " + "-------" + " " + "-------" + " " + "  |" + "\n" + 
    "|  |       |       |  |" + "\n" + 
    "|  |  " + " " + "----" + " " + "----" + " " + "  |  |" + "\n" +
    "|  |  |         |  |  |" + "\n" +
    d1 + "--" + d2 + "--" + d3 + "         " + e3 + "--" + e2 + "--" + e1 + "\n" + 
    "|  |  |         |  |  |"  + "\n" + 
    "|  |  " + f3 + "----" + g3 + "----" + h3 + "  |  |" + "\n" +
    "|  |       |       |  |" + "\n" + 
    "|  " + f2 + "-------" + g2 + "-------" + h2 + "  |" + "\n" + 
    "|          |          |" + "\n" +
    f1 + "----------" + g1 + "----------" + h1 + "\n" + "_______________________" + "\n")

#gameplay_board()
def update(): 
    gameplay_board()
turn=1
bpp=0
wpp=0
def place_pieces(): 
    ringchoice=int(input("Player 1, please select a ring to place your piece on. "))
    if ringchoice==1: #and ringchoice=="one": 
        spotchoice=input("Player 1, please chose a spot to place your piece on. ")
        if spotchoice==1: 
            a1="○"
gameplay_board()

问题原因及修复方案:

  1. 变量作用域问题:place_pieces()函数里的a1="○"创建的是局部变量,没有修改全局的a1。需要在函数内声明global a1,才能操作全局变量:
def place_pieces(): 
    global a1
    ringchoice=int(input("Player 1, please select a ring to place your piece on. "))
    if ringchoice==1: 
        spotchoice=input("Player 1, please chose a spot to place your piece on. ")
        if spotchoice=="1":  # 注意这里input返回的是字符串,要和字符串"1"比较
            a1="○"
  1. 输入类型不匹配:input()返回的是字符串,你用spotchoice==1(整数)比较永远不会成立,要么把spotchoice转成整数int(spotchoice)==1,要么直接和字符串"1"比较。
  2. 未调用修改函数:当前代码只定义了place_pieces(),但没有调用它,所以永远不会执行修改a1的逻辑,需要在合适的位置调用place_pieces(),比如在调用gameplay_board()之前:
place_pieces()
gameplay_board()

内容的提问来源于stack exchange,提问作者waluigi_the_stripper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 23:45:39