0

我刚刚开始使用 Python,我正在尝试将一个简单的石头剪刀布游戏修改为石头剪刀布蜥蜴 spock。结果,我现在需要将随机生成的计算机选择与表示丢失值的不是 1 而是 2 个字典项进行比较:


#!/usr/bin/python

import random
import time

rock = 1
paper = 2
scissors = 3
lizard = 4
spock = 5

names = { rock: "Rock", paper: "Paper", scissors: "Scissors", lizard: "Lizard", spock: "Spock"}
rules = { rock: [scissors, lizard], paper: [rock, spock], scissors: [paper, lizard], lizard: [paper, spock], spock: [rock, scissors]}

player_score = 0
computer_score = 0

def start():
    print "Let's play a game of Rock, Paper, Scissors, Lizard, Spock"
    while game():
        pass
    scores()

def game():
    player = move()
    computer = random.randint(1, 5)
    result (player, computer)
    return play_again()

def move():
    while True:
        print
        player = raw_input("Rock     = 1\nPaper    = 2\nScissors = 3\nLizard   = 4\nSpock    = 5\nMake a move: ")
        try:
            player = int(player)
            if player in (1,2,3,4,5):
                return player
        except ValueError:
            pass
        print "Oops! I didn't understand that. Please enter 1, 2, 3, 4, or 5."

def result(player, computer):
#   print "1..."
#   time.sleep(1)
#   print "2..."
#   time.sleep(1)
#   print "3!"
#   time.sleep(0.5)
    print "Computer threw {0}!".format(names[computer])
    global player_score, computer_score
    for i in rules[player]:
        if i == computer:
            global outcome
            outcome = "win"
    if outcome == "win":
        print "Your victory has been assured."
        player_score += 1
    elif player == computer:
        print "Tie game."
    else:
        print "The computer laughs as you realise you have been defeated."
        computer_score += 1

def play_again():
    answer = raw_input("Would you like to play again? y/n: ")
    if answer in ("y", "Y", "yes", "Yes", "Of course!"):
        return answer
    else:
        print "Thank you very much for playing. See you next time!"

def scores():
    global player_score, computer_score
    print "HIGH SCORES"
    print "Player: ", player_score
    print "Computer: ", computer_score

if __name__ == '__main__':
    start()

不幸的是,这段代码导致玩家总是赢……我做错了什么?

非常感谢您的帮助 :)

4

2 回答 2

1

让我们说playerotherPlayer持有他们制作的手形的代码。然后您可以通过检查一个玩家的代码是否包含在另一个玩家的代码的失败条件中来​​检查失败。

if player in rules [otherPlayer]: doSomething()

在不批评您的代码的情况下,我个人会按照这些方式以某种方式实现它。也许您可以从中获得两个想法,甚至是一些您肯定不想使用的模式:

import random

rules = '''Scissors cut paper
Paper covers rock
Rock crushes lizard
Lizard poisons Spock
Spock smashes scissors
Scissors decapitate lizard
Lizard eats paper
Paper disproves Spock
Spock vaporizes rock
Rock crushes scissors'''

rules = rules.lower ().split ()
rules = [_ for _ in zip (rules [::3], rules [2::3] ) ]
names = list (set (name for name, _ in rules) )

def turn ():
    ai = random.choice (names)
    player = input ('Enter your choice: ').lower ()
    if player not in names: raise Exception ('Sheldon out of bounds.')
    print ('AI chose {}.'.format (ai) )
    if (ai, player) in rules:
        print ('AI won.')
        return (0, 1)
    if (player, ai) in rules:
        print ('You won.')
        return (1, 0)
    print ('Draw.')
    return (0, 0)

score = (0, 0)
while True:
    you, ai = turn ()
    score = (score [0] + you, score [1] + ai)
    print ('The score is Human:Machine {}:{}'.format (*score) )
    if input ('Play again? [n/*] ').lower () == 'n': break
于 2013-10-14T23:03:14.530 回答
1

您已经获得outcome了全球性,但您从未将其设置为“赢”以外的任何内容。所以一旦你赢了一次,价值outcome永远是“赢”。

def result(player, computer):
    outcome = ""

您不会outcome在其他任何地方使用,因此无论如何都没有理由使其成为全球性的。

你甚至根本不需要那个变量。将此与 Hyperboreus 在他/她的回答中提到的内容相结合,您的result方法可以像这样开始,其他一切都相同:

def result(player, computer):
    print "Computer threw {0}!".format(names[computer])
    global player_score, computer_score
    if computer in rules[player]:
        print "Your victory has been assured."
        ...
于 2013-10-14T23:10:00.593 回答