首页 文章

如何将字典键的随机结果与2个值进行比较?

提问于
浏览
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()

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

非常感谢您的帮助 :)

2 回答

  • 1

    你有 outcome 作为全局,但你永远不会将它设置为"win"以外的任何东西 . 所以一旦你赢了一次, outcome 的值将永远是"win" .

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

    你不在其他任何地方使用 outcome ,所以没有理由把它变成全局的 .

    你真的不在他/她的回答中提到,你的 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."
            ...
    
  • 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
    

相关问题