2009-01-17 31 views
7

我需要一個Python遊戲IM工作的幫助(我剛開始學習Python的大約3天前,所以我還是一個小白=)編寫一個簡單的「剪刀石頭布」遊戲殭屍

這是什麼我想出了:

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!" 

和我想要什麼程序做的是隨機選擇1出三個選項(剪刀石頭布)的無論什麼用戶輸入!

+0

現在完全沒問題。 – PyRulez 2013-07-11 16:17:23

回答

29

那麼,你已經導入了隨機模塊,這是一個開始。

嘗試random.choice函數。

>>> from random import choice 
>>> cpu_choice = choice(('rock', 'paper', 'scissors')) 
7
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] 
+0

整潔!我也必須做一個。 =) – PEZ 2009-01-17 18:49:58

+3

你的節拍功能有點笨拙......爲什麼不在「((PAPER,ROCK),(SCISSORS,PAPER),(SCISSORS,ROCK))」中返回(a,b))? – Kiv 2009-04-29 19:03:59

4

由香古爾啓發:

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!" 
+0

你能解釋一下嗎?(player - cpu)%3 <(cpu - player)%3:`line? – koogee 2013-07-26 11:12:10

4

用詞典

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

不是專家,但這裏是我迄今爲止,使用__name__ == '__main__'聲明可能有幫助如果你需要電腦來生成答案並保持簡潔。

沒有給出解決方案。

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