在Python中,connect4游戏中,check for win函数不起作用

2024-09-28 05:42:44 发布

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

我正在用python编写一个connect 4游戏,但是checkforwin函数不起作用,它好像因为某种原因不存在,当它被调用并且应该返回True时,它什么也不起作用,下面是我的代码(我已经为不起作用的函数添加了一些注释):

# "\u25A1" == small square
mainList = [["\u25A1","\u25A1","\u25A1","\u25A1","\u25A1","\u25A1","\u25A1"],
         ["\u25A1","\u25A1","\u25A1","\u25A1","\u25A1","\u25A1","\u25A1"],
         ["\u25A1","\u25A1","\u25A1","\u25A1","\u25A1","\u25A1","\u25A1"],
         ["\u25A1","\u25A1","\u25A1","\u25A1","\u25A1","\u25A1","\u25A1"],
         ["\u25A1","\u25A1","\u25A1","\u25A1","\u25A1","\u25A1","\u25A1"],
         ["\u25A1","\u25A1","\u25A1","\u25A1","\u25A1","\u25A1","\u25A1"]]

def drawBoardFunc():
    for i in range(6):
        for j in range(7):
            print(mainList[i][j],end='')
        print("")

def checkForWinX():  #Here is the problem this function does nothing
    #Horizontal win for "X"
    for i in range(5,-1,-1):
        for j in range(4):
            if (mainList[i][j]=="X" and mainList[i][j+1]=="X"
            and mainList[i][j+2]=="X" and mainList[i][j+3]=="X"):
                return True

def checkForWinO():  #Here again the same problem
    #Horizontal win for "O"
    for i in range(5,-1,-1):
        for j in range(4):
            if (mainList[i][j]=="O" and mainList[i][j+1]=="O" and
                mainList[i][j+2]=="O" and mainList[i][j+3]=="O"):
                return True
        

drawBoardFunc()
#This list stores the row that its about to put a mark
checkRowList = [5,5,5,5,5,5,5]
player = 1
while True:
    print("Player's",player,"turn")
    chosenColumn = int(input("Please choose a column from 0 to 6 to play:\n"))
    if checkRowList[chosenColumn] == -1:
        print("Column full, please choose another column")
    else:
        if player == 1:
            row = checkRowList[chosenColumn]
            mainList[row][chosenColumn] = "Χ"
            checkRowList[chosenColumn] -= 1
            player = 2
            drawBoardFunc()
            win = checkForWinX()  #Here it is called for "X"
            if win==True:
                print("Player one win!")
                break
        elif player == 2:
            row = checkRowList[chosenColumn]
            mainList[row][chosenColumn] = "Ο"
            checkRowList[chosenColumn] -= 1
            player = 1
            drawBoardFunc()
            if checkForWinO()==True:  #Here it is called for "O" (in a little different way)
                print("Player two win!")
                break
    

我有两个相同的checkforwin函数,一个用于player“X”,另一个用于player“O”,所以我只提到了一个,我希望这是清楚的,如果您想要更多的细节,请让我知道,并请帮助。 编辑:我忘了说这个程序是一个正在进行的工作,我只检查横向赢的条件


Tags: andintrueforifrangewinrow
1条回答
网友
1楼 · 发布于 2024-09-28 05:42:44

这是一个非常不幸的错误,但似乎如下所示

在while循环中,有以下行

mainList[row][chosenColumn] = "Χ"

在检查X玩家是否获胜的函数中,检查单元格是否等于X,如下所示

mainList[i][j]=="X"

现在,函数不返回true的原因是,在while循环中指定的X与在checkForWinX函数中检查的X不同。不知何故,你不小心从其他地方复制/使用了一个不寻常的X

>>> "X" == "Χ"
False

>>> "X".encode('utf-8') # this is the one you use in checkForWinX
b'X'
>>> "Χ".encode('utf-8') # this is the one you assign to mainList[row][chosenColumn]
b'\xce\xa7'

根据浏览器的不同,您可以使用ctrl+f(查找)来查找X,您将看到并非每个X都亮起

相关问题 更多 >

    热门问题