石头剪刀布AI问题

Rock paper scissors AI issue

我正在构建一个简单的剪刀石头布游戏。它工作正常,除了 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()

您的 while 循环是导致问题的原因。只需将 player_won 和 computer_won 函数中的 while 更改为 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()

现在开始来一场剪刀石头布吧!

我承认这并不是您问题的直接答案,但我觉得提出一种可能更简单的方法来编写这个问题可能会很有用。

您可以让用户在输入中选择三个不同的数字而不是字母,或者只是将字母转换为数字。这样做的一个好处是要测试平局,您可以简单地写:

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()

这对于使您的代码正常工作并不是必需的,但我认为如果您有兴趣,它会很有用。