2015-11-21 89 views
0

我正在建立一個簡單的岩石紙剪刀遊戲。它工作正常,除了comp_count達到3時遊戲不會停止的事實。我似乎無法理解爲什麼,因爲它對player_count正常工作。請幫助我!岩石紙剪刀AI問題

from random import randint 

player_count = 0 
comp_count = 0 

def game(): 
    player_choice = raw_input('Do you choose rock [r], paper [p], or scissors [s]? ') 

    computer_choice = randint(0,2) 
    #Rock = 0 Paper = 1 Scissors = 2 

    #Player chooses paper, computer chooses rock 
    if player_choice == "p" and computer_choice == 0: 
     print 'Computer chose rock' 
     player_won() 

    #Player chooses rock, computer chooses scissors 
    elif player_choice == 'r' and computer_choice == 2: 
     print 'Computer chose scissors' 
     player_won() 

    #Player chooses scissors, computer chooses paper 
    elif player_choice == 's' and computer_choice == 1: 
     print 'Computer chose paper' 
     player_won() 

    #Computer chooses paper, player chooses rock 
    elif player_choice == 'r' and computer_choice == 1: 
     print 'Computer chose paper' 
     computer_won() 

    #Computer chooses rock, player chooses scissors 
    elif player_choice == 's' and computer_choice == 0: 
     print 'Computer chose rock' 
     computer_won() 

    #Computer chooses scissors, player chooses paper 
    elif player_choice == 'p' and computer_choice == 2: 
     print 'Computer chose scissors' 
     computer_won() 

    #Ties 
    elif player_choice == 'r' and computer_choice == 0: 
     print "It's a tie!" 
     game() 

    elif player_choice == 's' and computer_choice == 2: 
     print "It's a tie!" 
     game() 

    elif player_choice == 'p' and computer_choice == 1: 
     print "It's a tie!" 
     game() 

    #Wrong input 
    else: 
     print 'Please try again.' 
     game() 

def player_won(): 
    global player_count 
    print 'You win!' 
    player_count += 1 
    print 'You have ' + str(player_count) + ' point(s).' 
    while player_count < 3: 
     game() 

def computer_won(): 
    global comp_count 
    print 'Computer wins!' 
    comp_count += 1 
    print 'Computer has ' + str(comp_count) + ' point(s).' 
    while comp_count < 3: 
     game() 

print 'Welcome to Rock, Paper, Scissors! First to 3 points wins it all.' 
game() 
+0

它適用於'player_count'?真?我測試了你的代碼,在使用Ctrl-C之前,它讓我贏了5次。 – TigerhawkT3

回答

2

你的while循環是什麼導致你的問題。只需在player_won和computer_won函數中更改if,就可以解決問題。

def player_won(): 
    global player_count 
    print 'You win!' 
    player_count += 1 
    print 'You have ' + str(player_count) + ' point(s).' 
    if player_count < 3: 
     game() 

def computer_won(): 
    global comp_count 
    print 'Computer wins!' 
    comp_count += 1 
    print 'Computer has ' + str(comp_count) + ' point(s).' 
    if comp_count < 3: 
     game() 

現在去剪刀石頭布你的心臟了!

0

我承認這不是對您的問題的直接回答,但我覺得可能有更簡單的方法來編寫這個問題。

您可以讓用戶在輸入中選擇三個不同的數字而不是字母,或者只是將字母轉換爲數字。這樣做的一個好處是,以測試領帶,你可以簡單地寫:

if player_choice == computer_choice: 

即使誰在一場比賽中贏了檢查,如果它不是一個領帶也不會是非常困難的,因爲如果它是所有的數字,一個擊敗另一個的選項將在某個方向離開它。因此,舉例來說,你可以測試,如果玩家贏了,像這樣:

winning = computer_choice - 1 
if winning == -1: winning = 2 
if player_choice == wining: 
    player_won() 
else: #assuming we have already checked if it is a tie, we can say that otherwise the computer won. 
    computer_won() 

如果每個數字代表不同的選項(例如,如果你有一本字典鏈接0岩石,1至剪刀,和2至紙),那麼這將檢查用戶在計算機之前是否選擇了該選項,這將是獲勝選項。

這實際上可以讓您檢查誰贏了,以及哪些選項與if語句相對較少。你的檢查可能會是這個樣子:

options = {0: "rock", 1:"scissors", 2:"paper"} 

#collect player and computer choice here 

print "computer chose "+options[computer_choice] #We will probably tell them what the computer chose no matter what the outcome, so might as well just put it once up here now that a variable can determine what the computer chose. 

if player_choice == computer_choice: 
    print "It's a tie!" 
    game() 

winning = computer_choice - 1 
if winning == -1: winning = 2 

if player_choice == wining: 
    player_won() 
else: #assuming we have already checked if it is a tie, we can say that otherwise the computer won. 
    computer_won() 

這是不是讓你的代碼工作真的有必要,但我認爲,如果你有興趣將是有益的。