棋盘未重置,井字棋游戏

问题描述 投票:0回答:1

我正在尝试制作一个 tic tac toe 游戏,而且我对 python 非常陌生,所以我想知道为什么我的棋盘没有被重置,尽管我的函数明确地重置了棋盘。

我尝试摆脱该功能并仅实现重置,其中每个值都替换为下划线,如原始板列表,但我想让它更有效。

代码

import random
score=0
AIscore=0
PlayerSym="X"
AIsymbol="O"
Win=False
ListTF=[True,False]
Board=[["_","_","_"], 
       ["_","_","_"],
       ["_","_","_"]]

def resetGame():
    Board=[["_","_","_"], 
       ["_","_","_"],
       ["_","_","_"]]
    print("resetting board ///////////////////////////////////////////")


def initiateGame(Win,AIsymbol):
    print("Welcome to tic tac toe")
    print("If you score 3 points you win")
    print("pick a row and column to place your symbol:")
    
    
def Symbols():
    TF=random.choice(ListTF)
    if Win==False:
        PlayerSym="X"
        print("player, you are 'X' so you go first")
        AIsymbol="O"
    else:
        if TF==True:
            PlayerSym="X"
            AIsymbol="O"
            print("player, you are 'X' so you go first")
        if TF==False:
            PlayerSym="O"
            AIsymbol="X"
            print("player, you are 'O' so you go second")    


def GameWinner(score,Board,AIscore):
    for row in range(0,3): #checks all columns for a win
        if(Board[row][0]==Board[row][1] == Board[row][2] == PlayerSym):
            Win=True
            print("congrats, you have earned a point")
            print("the score is:")
            score=score+1
            print("Player:",score)
            print("AI:",AIscore)
            resetGame()
        elif(Board[row][0]==Board[row][1] == Board[row][2] == AIsymbol):
            Win=False
            print("The AI has earned a point")
            print("the score is:")
            print("Player:",score)
            AIscore=AIscore+1
            print("AI:",AIscore)
            resetGame()  
    if(Board[0][0]==Board[1][1] == Board[2][2] == PlayerSym):
        Win=True
        print("congrats, you have earned a point")
        print("the score is:")
        score=score+1
        print("Player:",score)
        print("AI:",AIscore)
        resetGame()
    elif(Board[0][0]==Board[1][1] == Board[2][2] == AIsymbol):
        Win=False
        print("The AI has earned a point")
        print("the score is:")
        print("Player:",score)
        AIscore=AIscore+1
        print("AI:",AIscore)
        resetGame()
    if(Board[0][2]==Board[2][0] == Board[1][1] == PlayerSym):
        Win=True
        print("congrats, you have earned a point")
        print("the score is:")
        score=score+1
        print("Player:",score)
        print("AI:",AIscore)
        resetGame()
    elif(Board[0][2]==Board[2][0] == Board[1][1] == AIsymbol):
        Win=False
        print("The AI has earned a point")
        print("the score is:")
        print("Player:",score)
        AIscore=AIscore+1
        print("AI:",AIscore)
        resetGame() 
        
def Space(Board,PlayerSym,AIsymbol):
    print(Board[0])
    print(Board[1])
    print(Board[2])
    row=int(input("pick a row from (0-2):"))
    column=int(input("pick a column from (0-2)"))
    while (row>2 or row<0) or (column>2 or column<0):
        print("you entered a value that is not within the parameters, pick again")
        row=int(input("pick a row from (0-2):"))
        column=int(input("pick a column from (0-2)"))
    while (Board[row][column]==PlayerSym or Board[row][column]==AIsymbol):
        print("your row and column already has an 'X' or 'O', pick again") 
        row=int(input("pick a row from (0-2):"))
        column=int(input("pick a column from (0-2)"))
    Board[row][column]=PlayerSym
    

initiateGame(Win,AIsymbol)

def main():
        Symbols()
        GameWinner(score,Board,AIscore)
        Space(Board,PlayerSym,AIsymbol)
        

while True:
    main()
python tic-tac-toe
1个回答
1
投票

正如 @quamrana 所说,你的

resetGame()
函数正在生成一个新的
Board = [[ ... ]]
,但这就是它在这里所做的一切。您根本没有引用之前生成的
Board
,因此这个新的
Board
存在于
resetGame()
范围内,并且不会与您尝试重置的全局版本交互。解决此问题的一个简单方法是返回新板,并在您希望板重置时分配该值。例如:

def resetGame():
    newBoard =[["_","_","_"], 
               ["_","_","_"],
               ["_","_","_"]]
    return newBoard 

   ...

Board = resetGame()

或者,您可以使用

global
关键字直接定位全局变量,这会将相关变量添加到方法作用域中:

def resetGame():
    global Board
    Board= [["_","_","_"], 
            ["_","_","_"],
            ["_","_","_"]]

   ...

resetGame()

这里有一个关于 python 中作用域如何工作的好资源,可以帮助您:https://www.w3schools.com/python/python_scope.asp

干杯,祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.