python 3.5 Tic Tac Toe得分计数

时间:2016-01-22 14:04:48

标签: python tic-tac-toe python-3.5

需要帮助阻止比分重置每场比赛,

到目前为止,这是我的代码而且无效。

这是检查谁是赢家等的功能的一部分。

def winner ():
        global Win
        if alist [0] == player1 and alist[1] == player1 and alist[2] ==player1:#top line horizontal
            Win = 'player1'
            return True
        elif alist[3] == player1 and alist[4] == player1 and alist[5] ==player1:#middleline horizontal
            Win = 'player1'
            return True

此声明确定分数以及是否开始另一场比赛。

if winner():
                if Win == 'player1':
                    print("player1 is winner")
                    p1score = p1score+1
                elif Win == 'player2':
                    print("player2 is winner")
                    p2score = p2score+1
                print('Player1s score =', p1score,'Player2s score =', p2score)
print("Would you like to play again(yes or no)")
                restart = input("")
                if restart == 'yes':
                    return gamemode()

好的,所以在游戏结束时分数显示正确,但是当播放另一个游戏时,它会重置吗?

def playervscomputer():
        global Player1Score
        Player1Score = 0
        global ComputerScore
        ComputerScore = 0
        players = [name, 'computer']
        global turn
        turn = random.randint(0,1)
        while True:
            print('its\s %s\'s turn' % players[turn])
        if winner1():
            #Check if people have won
            if Win == 'player1':
                print("player1 is winner")
                Player1Score = Player1Score+1
                print("player1s score is", Player1Score, 'Computer Score=', ComputerScore)
                print("would you like to play again?(yes or no)")
                restart = input("")
                if restart =='yes':
                    return main()
                else:
                    print("Thanks for playing")
     elif Win == 'Computer':
                    print("Computer is winner")
                    ComputerScore = ComputerScore+1
                print('Player1s score =', Player1Score,'Computers score =', ComputerScore)

任何想法或任何帮助都会在几场比赛后保持比分。

由于

2 个答案:

答案 0 :(得分:0)

您似乎并未完全了解范围。尝试将p1score和p2score更改为全局变量(就像你在Win中所做的那样)。

此YouTube视频帮助我了解了全局和局部变量之间的区别,也许它会对您有所帮助。 https://www.youtube.com/watch?v=A054Ged9suI

答案 1 :(得分:0)

一个简单的程序,它使用一个函数递增两个独立变量(在列表中):

score = [0,0]
def increment(Player):
    global score
    if Player == 1:
        score[0]+=1
    elif Player ==2:
        score[1]+=1
    else:
        print('Input not defined')
def main():
    global score
    print(score)
    increment(1)
    increment(2)
    increment(2)
    print(score)