0

我需要帮助为我的井字游戏的每一轮制作计分板,我希望它在每轮结束时将 cscore(计算机得分)或 pscore(玩家得分)更新 1。

任何对我的代码的反馈将不胜感激,因为我对 python 还很陌生。

我已经把我已经拥有的分数相关代码放在下面

import random
def main():
    global cscore
    cscore = 0
    global pscore
    pscore = 0
if turn == 'player':
    drawBoard(theBoard)
    move = getPlayerMove(theBoard)
    makeMove(theBoard, playerLetter, move)
    if isWinner(theBoard, playerLetter):
        drawBoard(theBoard)
        print('Hooray! You have won the game!')
        pscore += 1
        print(main)
                
        gameIsPlaying = False
    else:
        if isBoardFull(theBoard):
            drawBoard(theBoard)
            print('The game is a tie!')
            print(main)
                

            break
        else:
            turn = 'computer'
else:
    move = getComputerMove(theBoard, computerLetter)
    makeMove(theBoard, computerLetter, move)
    if isWinner(theBoard, computerLetter):
        drawBoard(theBoard)
        print('The computer has beaten you! You lose.')
        cscore += 1
        print(main)
              
        gameIsPlaying = False
    else:
        if isBoardFull(theBoard):
            drawBoard(theBoard)
            print('The game is a tie!')
            print(main)
                   

            break
        else:
            turn = 'player'
if not playAgain():
break

main()
4

1 回答 1

0

在游戏循环的函数开始处,那个不会停止的循环,你需要添加

global cscore, pscore

就像在main()函数中一样。如果你没有 - 这是它不会更新给你的方式。

顺便说一句,更好的方法是class为播放器创建一个(我知道你是 python 新手,所以这 - https://www.youtube.com/watch?v=ZDa-Z5JzLYM可能会帮助你),类看起来有点像:

class Player():

    def __init__(self, is_human):
        self.score = 0
        self.is_human = is_human
于 2021-08-13T15:17:57.377 回答