python 编写一个简单的“石头剪刀布”游戏机器人

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/453396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:09:38  来源:igfitidea点击:

Writing a simple "Rock Paper Scissors" game bot

python

提问by Test

This is what I came up with for my rock-paper-scissors game:

这就是我为我的石头剪刀布游戏想到的:

import random 

from time import sleep 

print "Please select: " 
print "1  Rock" 
print "2  Paper" 
print "3  Scissors" 

player = input ("Choose from 1-3: ") 

if player == 1: 
    print "You choose Rock" 
    sleep (2) 
    print "CPU chooses Paper" 
    sleep (.5) 
    print "You lose, and you will never win!" 

elif player == 2: 
    print "You choose Paper" 
    sleep (2) 
    print "CPU chooses Scissors" 
    sleep (.5) 
    print "You lose, and you will never win!" 

else: 
    print "You choose Scissors" 
    sleep (2) 
    print "CPU chooses Rock" 
    sleep (.5) 
    print "You lose, and you will never win!"

and what I want the program to do is to RANDOMLY choose 1 out of the three options (rock paper scissors) no matter what the user inputs. How can I accomplish this?

我希望程序做的是无论用户输入什么,都从三个选项(石头剪刀布)中随机选择一个。我怎样才能做到这一点?

回答by sykora

Well, you've already imported the random module, that's a start.

好吧,您已经导入了 random 模块,这是一个开始。

Try the random.choice function.

试试 random.choice 函数。

>>> from random import choice
>>> cpu_choice = choice(('rock', 'paper', 'scissors'))

回答by gumuz

import random

ROCK, PAPER, SCISSORS = 1, 2, 3
names = 'ROCK', 'PAPER', 'SCISSORS'

def beats(a, b):
    if (a,b) in ((ROCK, PAPER), (PAPER, SCISSORS), (SCISSORS, ROCK)): 
        return False

    return True


print "Please select: " 
print "1  Rock" 
print "2  Paper" 
print "3  Scissors" 

player = int(input ("Choose from 1-3: "))
cpu = random.choice((ROCK, PAPER, SCISSORS))

if cpu != player:
    if beats(player, cpu):
        print "player won"
    else:
        print "cpu won"
else:
    print "tie!"

print names[player-1], "vs", names[cpu-1]

回答by PEZ

Inspired by gumuz:

灵感来自古姆兹:

import random

WEAPONS = 'Rock', 'Paper', 'Scissors'

for i in range(0, 3):
  print "%d %s" % (i + 1, WEAPONS[i])

player = int(input ("Choose from 1-3: ")) - 1
cpu = random.choice(range(0, 3))

print "%s vs %s" % (WEAPONS[player], WEAPONS[cpu])
if cpu != player:
  if (player - cpu) % 3 < (cpu - player) % 3:
    print "Player wins"
  else:
    print "CPU wins"
else:
  print "tie!"

回答by Carlitos

use dictionaries

使用字典

loseDic = { 'rock'     : 'paper',
            'paper'    : 'scissors',
            'scissors' : 'rock',
}

## Get the human move, chose a random move, bla bla bla...

if robotMove == humanMove:
    tie()
elif humanMove == loseDic[robotMove]:
    lose()
else:
    win()

回答by weallneedmoresunshine

Not an expert, but here is what I have thus far, using a __name__ == '__main__'statement may be helpful if you need the computer to generate an answer and keep it clean and concise.

不是专家,但这是我迄今为止所拥有的,__name__ == '__main__'如果您需要计算机来生成答案并保持其简洁明了,那么使用语句可能会有所帮助。

No solution given.

没有给出解决方案。

import random

def is_tie(move1, move2):

'''FIX! (parameter types) -> return type

    Return True if move1 and move2 are the same.'''

    if move1 == move2:
        return True

def is_win(move1, move2):
    '''FIX! (parameter types) -> return type

    Return True iff move1 beats move2 in rock-paper-scissors.'''

    choice1 = scissor > paper,
    choice2 = paper > rock,
    choice3 = rock > scissor   

    return choice1 or choice2 or choice3

    if move1 > move2:

    return True

if __name__ == '__main__':

    # Get a move from the user.
    print "Rock, Paper, Scissor",    
    move1 = raw_input("What is your move? ")

    # Now, to generate a random move for the computer. Tricky... Here are some examples and suggestions, no solution given.

    if move2(random.randint(1,3)) == 1:
        print "paper"
    elif move2(random.randint(1,3)) == 2:
        print "rock"
    else:
        print "scissor"

    # Evaluate who wins and then print out an appropriate message.    
    #if solution(move1, move2):
    #   print 
    #if move2 > move1:
    #    usr_fail = (raw_input('I win!!'))
    #    print usr_fail
    #if move2 < move1:
    #    usr_win  = (raw_input('Damn it!'))
    #    print usr_win
    #if move2 == move1
    #usr_draw = (raw_input('Draw!!!')
    #    print usr_draw

回答by Mehdi Chajadine

If anyone feels like playing with the "original school playground rule", first to 3 wins.

如果有人喜欢玩“原始学校操场规则”,则先到 3 人获胜。

import random

x = ["Rock", "Paper", "Scissors"]

computer_score = 0
my_score = 0

z = ""
y = ""

while computer_score <= 3 and my_score <= 3:
    z = input("Rock, Paper or Scissors: ")
    y = random.choice(x)
    print(y)
    if z == "Rock" and y == "Scissors":
        my_score += 1
    elif z == "Paper" and y == "Rock":
        my_score += 1
    elif z == "Scissors" and y == "Paper":
        my_score += 1
    elif z == y:
        my_score = my_score
        computer_score = computer_score
    else:
        computer_score += 1
    print("Computer: {}".format(computer_score))
    print("Me: {}".format(my_score))

    if computer_score == 3:
        print("The computer wins!")
        break
    elif my_score == 3:
        print("You win!")
        break

回答by alfie redgrave

Try this instead:

试试这个:

    print()
    humanGuess = input("rock, paper or scissors?: ")
    import random
    misc =(random.randint(1,3))
    if misc == 1 and humanGuess == "paper":
        print ("paper, [draw] ")
    elif misc == 2 and humanGuess == "paper":
        print ("rock, [you win] ")
    elif misc == 3 and humanGuess == "paper":
        print ("scissors, [I win] ")
    elif misc == 1 and humanGuess == "rock":
        print ("paper, [I win] ")
    elif misc == 2 and humanGuess == "rock":
        print ("rock, [draw] ")
    elif misc == 3 and humanGuess == "rock":
        print ("scissors, [you win] ")
    elif misc == 1 and humanGuess == "scissors":
        print ("paper, [you win] ")
    elif misc == 2 and humanGuess == "scissors":
        print ("rock, [I won] ")
    elif misc == 3 and humanGuess == "scissors":
        print ("scissors, [draw] ")
    else:
        print ("not recognised")

I think this works

我认为这有效