2009-01-17 9 views
7

Necesito ayuda con un juego de pitón im trabajando en (Acabo de empezar a aprender Python hace aproximadamente 3 días, así que estoy todavía un novato =)Escribir un sencillo "Rock Paper Scissors" bot juego

Esto es lo Se me ocurrió:

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

y lo que yo quiero el programa para hacer es elegir AZAR 1 de las tres opciones (tijeras de papel roca) sin importar las entradas del usuario!

+0

Está perfectamente bien ahora. – PyRulez

Respuesta

29

Bueno, ya ha importado el módulo aleatorio, eso es un comienzo.

Pruebe la función 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

¡Aseado! Tuve que hacer uno también. =) – PEZ

+3

La función de su beats es un poco torpe ... ¿por qué no simplemente "return (a, b) in ((PAPER, ROCK), (SCISSORS, PAPER), (SCISSORS, ROCK))"? – Kiv

4

Inspirado por 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

¿Puedes explicar 'if (player - cpu)% 3 <(cpu - player)% 3:' line? – koogee

4

diccionarios de uso

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

Un optimista lo habría llamado 'winDic' con las coincidencias modificadas en consecuencia;) – sjngm

1
No

un experto, pero aquí es lo que tengo hasta ahora, mediante una instrucción __name__ == '__main__' puede ser útil si necesita la computadora para generar una respuesta y mantenerla limpia y conciso.

No se ha proporcionado ninguna solución.

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 
Cuestiones relacionadas