我用Python编写了一个零和交叉的游戏,但是没有正确地声明赢家

2024-05-15 23:21:07 发布

您现在位置:Python中文网/ 问答频道 /正文

游戏代码如下:

# oxo_main_shell.py
# code needed to test integrity of main shell
player = "X"
oxo = [' ',' ',' ',' ',' ',' ',' ',' ',' ']
win = ""
turns = 9

# main shell
def setup():
    global win, player, turns, oxo, good, allwins
    win = ""
    player = "X"
    turns = 9
    oxo = [" "," "," "," "," "," "," "," "," "]
    good = [1,2,3,4,5,6,7,8,9]
    allwins = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]]

def msgintro():
    msg = "Press [Enter] to continue"
    print(msg)
    input()

def printrow(a,b,c,d,e,f):
    print(a+' '+b+' |'+c+' '+d+' |'+e+' '+f+' ')

def printline():
    print('-----+-----+-----')

def printboard(oxo):
    printrow('1 ',' ','2 ',' ','3 ',' ')
    printrow('  ',oxo[0],'  ',oxo[1],'  ',oxo[2])
    printrow('  ',' ','  ',' ','  ',' ')
    printline()
    printrow('4 ',' ','5 ',' ','6 ',' ')
    printrow('  ',oxo[3],'  ',oxo[4],'  ',oxo[5])
    printrow('  ',' ','  ',' ','  ',' ')
    printline()
    printrow('7 ',' ','8 ',' ','9 ',' ')
    printrow('  ',oxo[6],'  ',oxo[7],'  ',oxo[8])
    printrow('  ',' ','  ',' ','  ',' ')

def play():
    pos = 0
    while pos not in good:
        pos = input()
        try:
            pos = int(pos)
        except ValueError:
            pos = 0
    oxo[pos-1] = player
    good.remove(pos)

def getplaces(player,oxo):
    places = []
    for i in range(0,len(oxo)):
        if player == oxo[i]: places.append(i+1)
    return places

def issubset(win,places):
    for i in range(0,len(places)-2):
        if win == places[i:i+3]: return True
    return False

def checkwin(player,oxo):
    places = getplaces(player,oxo)
    for win in allwins:
        if issubset(win,places): return True
    return False

def swap(player):
    if player == 'X': return '0'
    return 'X'

def declarewinner(win):
    if win != "":
        print(win,"wins")
    else:
        print("Stalemate")

# main program
setup()
msgintro()
printboard(oxo)

# turn
while turns > 0:
    play()
    printboard(oxo)
    if checkwin(player,oxo): break
    player = swap(player)
    turns -= 1
declarewinner(player)

它基本上是有效的,当我输入位置号时,让我把棋子放在位置上,如果游戏结束时棋盘没有填满,就可以正确地宣布赢家。然而,如果棋盘在比赛结束时被填满,那么它就开始错误地宣布胜利者,并且永远不会宣布僵局。例如,我测试了一个棋子在这些位置的游戏:

^{2}$

这本应该是一个僵局,但0被宣布获胜。在

另一个例子是在一个棋子处于以下位置的游戏中:

1    |2    |3    
   X |   0 |   X 
     |     |     
-----+-----+-----
4    |5    |6    
   0 |   X |   0 
     |     |     
-----+-----+-----
7    |8    |9    
   0 |   X |   X 
     |     |     

0被宣布为优胜者,而X则应被宣布为优胜者。在

我有什么办法可以让它正确地宣布获胜者吗?在


Tags: inpos游戏returnifmaindefwin
2条回答

问题出在主游戏循环中,在发布代码的末尾。在

如果您完全填写了棋盘,但没有人获胜,则游戏退出while循环,但在循环退出后不会执行checkwin,并且在循环结束时自动获胜的玩家是declarewinner(player)。因此,在僵局中,0总是获胜。在

同样的道理,如果你赢了最后一步,在填补游戏板。否checkwin,0始终获胜。在

所以,只需在末尾添加一些checkwin。将最后一行替换为:

if checkwin("0", oxo):
    declarewinner("0")
elif checkwin("X", oxo):
    declarewinner("X")
else: # stalemate
    declarewinner("")

问题出在你最后调用declarewinner(player)declarewinner函数只有在传入的变量是空字符串时才会声明相持。但由于您的player变量是为每一个回合设置的,它永远不会是空字符串。在

相反,您需要的是在checkwin中设置全局win变量。然后,修改您的declarewinner函数以读取全局win变量,而不是接受参数。在

示例:

def checkwin(player,oxo):
    global win  # since you are modifying a global variable
    places = getplaces(player,oxo)
    for w in allwins:  # use `w` instead of `win` to avoid namespace collision with your global variable
        if issubset(w,places):
            win = player  # set the `win` variable as to your winning player 
            return True
    return False

...

def declarewinner():
    if win != "":
        print(win,"wins")
    else:
        print("Stalemate")

...

while turns > 0:
    play()
    printboard(oxo)
    if checkwin(player,oxo): break
    player = swap(player)
    turns -= 1
declarewinner()

相关问题 更多 >