我刚刚开始使用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()
不幸的是,这段代码导致玩家总是赢了......我做错了什么?
非常感谢你的帮助:)
答案 0 :(得分:1)
让我们说player
和otherPlayer
保留他们制作的手形的代码。然后你可以通过检查一个玩家的代码是否包含在其他玩家代码的丢失条件中来检查输掉。
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
答案 1 :(得分: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."
...