Tic-tac-toe Python - 切换用户

时间:2018-02-18 19:43:16

标签: python tic-tac-toe

我正在为Tic Tac Tow游戏编写python代码。 我将用户从Palyerx切换到了playero。 以下是我的代码的一部分。 我不确定哪个部分我做错了,在playero生成有效结果后,它没有切换到playerx。 我真的很感谢你的帮助! 提前谢谢大家。

board_list=["0","1","2","3","4","5","6","7","8"]

playerLetter = 'o'

# Print out the tic tac toe board
# Input: list representing the tic tac toe board
# Return value: none
def printUglyBoard(board_list):
    print()
    counter = 0
    for i in range(3):
        for j in range(3):
        print(board_list[counter], end="  ")
        counter += 1
    print()


def isValidMove(board_list,spot):
    if 0<= spot <= 8 and board_list[spot]!='x' and board_list[spot]!='o':
        print("True")
        return True
    else:
        print("Invaild. Enter another value.")
        return False

def updateBoard(board_list,spot,playerLetter):
    result=isValidMove(board_list,spot)
    if result==True and playerLetter=='x':
        board_list[spot]='x'
        playerLetter='o'
    elif result==True and playerLetter=='o':
        board_list[spot]='o'
        playerLetter='x'
    print(board_list)
    print(playerLetter)
    printUglyBoard(board_list)




def play():
    printUglyBoard(board_list)
    while True:
        if playerLetter=='x':
            spot = int(input('Player x,enter the value:'))
        elif playerLetter=='o':
            spot = int(input('Player o,enter the value:'))
        isValidMove(board_list,spot)
        updateBoard(board_list,spot,playerLetter)



play()

1 个答案:

答案 0 :(得分:0)

函数内部的变量是本地的。基本上你需要返回playerLetter以便在下一个循环中使用。试试这个:

board_list=["0","1","2","3","4","5","6","7","8"]

# Print out the tic tac toe board
# Input: list representing the tic tac toe board
# Return value: none
def printUglyBoard(board_list):
    print()
    counter = 0
    for i in range(3):
        for j in range(3):
            print(board_list[counter], end="  ")
            counter += 1
    print()


def isValidMove(board_list,spot):
    if 0<= spot <= 8 and board_list[spot]!='x' and board_list[spot]!='o':
        print("True")
        return True
    else:
        print("Invaild. Enter another value.")
        return False

def updateBoard(board_list,spot,playerLetter):
    result=isValidMove(board_list,spot)
    if result==True and playerLetter=='x':
        board_list[spot]='x'
        playerLetter='o'
    elif result==True and playerLetter=='o':
        board_list[spot]='o'
        playerLetter='x'
    print(board_list)
    print(playerLetter)
    printUglyBoard(board_list)
    return playerLetter  ############### <<------- CHANGED

def play():
    playerLetter = 'o'  ############### <<------- CHANGED
    printUglyBoard(board_list)
    while True:
        if playerLetter=='x':
            spot = int(input('Player x,enter the value:'))
        elif playerLetter=='o':
            spot = int(input('Player o,enter the value:'))
        isValidMove(board_list,spot)
        playerLetter = updateBoard(board_list,spot,playerLetter) ###### <<---- CHANGED

play()

<强>此外:

您可以在此处进行各种更改。以此为例,4行变为1:

if playerLetter=='x':
    spot = int(input('Player x,enter the value:'))
elif playerLetter=='o':
    spot = int(input('Player o,enter the value:'))

可以改为:

spot = int(input('Player {},enter the value:'.format(playerLetter))