2009-01-17 32 views
7

Tôi cần trợ giúp với một trò chơi python im (Tôi mới bắt đầu học Python cách đây khoảng 3 ngày, vì vậy tôi vẫn là nOob =)Viết một trò chơi đơn giản "Rock Paper Scissors" bot

Đây là những gì tôi đã đưa ra:

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

và những gì tôi muốn chương trình làm là ngẫu nhiên chọn 1 trong ba tùy chọn (kéo giấy đá) không có vấn đề gì các đầu vào người sử dụng!

+0

Điều này hoàn toàn ổn. – PyRulez

Trả lời

29

Vâng, bạn đã nhập mô-đun ngẫu nhiên, đó là bắt đầu.

Thử chức năng 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

Gọn gàng! Tôi cũng phải làm một cái. =) – PEZ

+3

Chức năng nhịp đập của bạn hơi khó hiểu ... tại sao không chỉ "trả lại (a, b) trong ((GIẤY, ROCK), (SCISSORS, PAPER), (SCISSORS, ROCK))"? – Kiv

4

Lấy cảm hứng từ 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!" 
+0

Bạn có thể giải thích 'if (player - cpu)% 3 <(cpu - player)% 3:' line? – koogee

4

từ điển sử dụng

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() 
+5

Một người lạc quan sẽ gọi nó là 'winDic' với các trận đấu được sửa đổi tương ứng;) – sjngm

1

Không một chuyên gia, nhưng đây là những gì tôi có vậy, đến nay, sử dụng một tuyên bố __name__ == '__main__' có thể hữu ích nếu bạn cần máy tính để tạo ra một câu trả lời và giữ cho nó sạch sẽ và ngắn gọn.

Không có giải pháp nào được cung cấp.

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 
Các vấn đề liên quan