Programa en Python para implementar el juego Piedra, Papel, Tijera

Python es un lenguaje multipropósito y uno puede hacer cualquier cosa con él. Python también se puede utilizar para el desarrollo de juegos. Vamos a crear un juego de piedra, papel o tijera de línea de comandos simple sin usar bibliotecas de juegos externas como PyGame. En este juego, el usuario tiene la primera oportunidad de elegir la opción entre piedra, papel y tijera. Después de que la computadora seleccione de las dos opciones restantes (al azar), el ganador se decide según las reglas.

Winning Rules as follows :

Rock vs paper-> paper wins
Rock vs scissor-> Rock wins
paper vs scissor-> scissor wins.

En este juego, la función incorporada randint() se usa para generar valores enteros aleatorios dentro del rango dado.

A continuación se muestra la implementación: 

Python3

# import random module
import random
 
# Print multiline instruction
# performstring concatenation of string
print("Winning Rules of the Rock paper scissor game as follows: \n"
                                +"Rock vs paper->paper wins \n"
                                + "Rock vs scissor->Rock wins \n"
                                +"paper vs scissor->scissor wins \n")
 
while True:
    print("Enter choice \n 1 for Rock, \n 2 for paper, and \n 3 for scissor \n")
     
    # take the input from user
    choice = int(input("User turn: "))
 
    # OR is the short-circuit operator
    # if any one of the condition is true
    # then it return True value
     
    # looping until user enter invalid input
    while choice > 3 or choice < 1:
        choice = int(input("enter valid input: "))
         
 
    # initialize value of choice_name variable
    # corresponding to the choice value
    if choice == 1:
        choice_name = 'Rock'
    elif choice == 2:
        choice_name = 'paper'
    else:
        choice_name = 'scissor'
         
    # print user choice
    print("user choice is: " + choice_name)
    print("\nNow its computer turn.......")
 
    # Computer chooses randomly any number
    # among 1 , 2 and 3. Using randint method
    # of random module
    comp_choice = random.randint(1, 3)
     
    # looping until comp_choice value
    # is equal to the choice value
    while comp_choice == choice:
        comp_choice = random.randint(1, 3)
 
    # initialize value of comp_choice_name
    # variable corresponding to the choice value
    if comp_choice == 1:
        comp_choice_name = 'Rock'
    elif comp_choice == 2:
        comp_choice_name = 'paper'
    else:
        comp_choice_name = 'scissor'
         
    print("Computer choice is: " + comp_choice_name)
 
    print(choice_name + " V/s " + comp_choice_name)
    #we need to check of a draw
    if choice == comp_choice:
        print("Draw=> ", end = "")
        result = Draw
       
    # condition for winning
        if((choice == 1 and comp_choice == 2) or
          (choice == 2 and comp_choice ==1 )):
            print("paper wins => ", end = "")
            result = "paper"
 
        elif((choice == 1 and comp_choice == 3) or
            (choice == 3 and comp_choice == 1)):
            print("Rock wins =>", end = "")
            result = "Rock"
        else:
            print("scissor wins =>", end = "")
            result = "scissor"
 
    # Printing either user or computer wins or draw
    if result == Draw:
        print("<== Its a tie ==>")
    if result == choice_name:
        print("<== User wins ==>")
    else:
        print("<== Computer wins ==>")
         
    print("Do you want to play again? (Y/N)")
    ans = input().lower
 
 
    # if user input n or N then condition is True
    if ans == 'n':
        break
     
# after coming out of the while loop
# we print thanks for playing
print("\nThanks for playing")

Producción: 

winning Rules of the Rock paper and scissor game as follows:
rock vs paper->paper wins 
rock vs scissors->rock wins 
paper vs scissors->scissors wins 

Enter choice 
 1. Rock 
 2. paper 
 3. scissor 

User turn: 1
User choice is: Rock

Now its computer turn.......

computer choice is: paper
Rock V/s paper
paper wins =>computer wins
do you want to play again?
N

Publicación traducida automáticamente

Artículo escrito por ankthon y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *