我正在建造一个简单的石头剪刀游戏。它工作正常,除了当comp_count达到3时游戏不会停止的事实。我似乎无法理解为什么,因为它适用于player_count。请帮帮我!
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 :(得分:2)
你的while循环是什么导致你的问题。只需在您的player_won和computer_won函数中更改为if,它就可以解决问题。
TIdTCPClient(OutboundClient).Host
现在去摇滚纸剪你的心!
答案 1 :(得分: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()
这对于使您的代码无效是非常必要的,但我认为如果您感兴趣,这将非常有用。