21 Juego de números en Python

21, Bagram, o Veinte más uno es un juego que avanza contando del 1 al 21, y el jugador que dice «21» es eliminado. Se puede jugar entre cualquier número de jugadores.

Implementación
Este es un juego simple de 21 números usando el lenguaje de programación Python. El juego ilustrado aquí es entre el jugador y la computadora. Puede haber muchas variaciones en el juego.

  • El jugador puede elegir comenzar primero o segundo.
  • La lista de números se muestra antes de que el Jugador tome su turno para que sea conveniente.
  • Si no se dan números consecutivos en la entrada, el jugador es automáticamente descalificado.
  • El jugador pierde si tiene la oportunidad de igualar 21 y gana en caso contrario.

Ganar contra la computadora puede ser posible eligiendo jugar segundo. La estrategia es llamar a números hasta el múltiplo de 4, lo que eventualmente conduciría a 21 en la computadora, por lo que el jugador sería el ganador.

# Python code to play 21 Number game
  
# returns the nearest multiple to 4
def nearestMultiple(num):
    if num >= 4:
        near = num + (4 - (num % 4))
    else:
        near = 4
    return near
  
def lose1():
    print ("\n\nYOU LOSE !")
    print("Better luck next time !")
    exit(0)
      
# checks whether the numbers are consecutive
def check(xyz):
    i = 1
    while i<len(xyz):
        if (xyz[i]-xyz[i-1])!= 1:
            return False
        i = i + 1
    return True
  
# starts the game
def start1():
    xyz = []
    last = 0
    while True:
        print ("Enter 'F' to take the first chance.")
        print("Enter 'S' to take the second chance.")
        chance = input('> ')
          
        # player takes the first chance
        if chance == "F":
            while True:
                if last == 20:
                    lose1()
                else:
                    print ("\nYour Turn.")
                    print ("\nHow many numbers do you wish to enter?")
                    inp = int(input('> '))
                      
                    if inp > 0 and inp <= 3:
                        comp = 4 - inp
                    else:
                        print ("Wrong input. You are disqualified from the game.")
                        lose1()
              
                    i, j = 1, 1
  
                    print ("Now enter the values")
                    while i <= inp:
                        a = input('> ')
                        a = int(a)
                        xyz.append(a)
                        i = i + 1
                      
                    # store the last element of xyz.
                    last = xyz[-1] 
                      
                    # checks whether the input 
                    # numbers are consecutive
                    if check(xyz) == True: 
                        if last == 21:
                            lose1()
                              
                        else:
                            #"Computer's turn."
                            while j <= comp:
                                xyz.append(last + j)
                                j = j + 1
                            print ("Order of inputs after computer's turn is: ")
                            print (xyz)
                            last = xyz[-1]
                    else:
                        print ("\nYou did not input consecutive integers.")
                        lose1()
                          
        # player takes the second chance
        elif chance == "S":
            comp = 1
            last = 0
            while last < 20:
                #"Computer's turn"
                j = 1
                while j <= comp:
                    xyz.append(last + j)
                    j = j + 1
                print ("Order of inputs after computer's turn is:")
                print (xyz)
                if xyz[-1] == 20:
                    lose1()
                      
                else:
                    print ("\nYour turn.")
                    print ("\nHow many numbers do you wish to enter?")
                    inp = input('> ')
                    inp = int(inp)
                    i = 1
                    print ("Enter your values")
                    while i <= inp:
                        xyz.append(int(input('> ')))
                        i = i + 1
                    last = xyz[-1]
                    if check(xyz) == True:
                        # print (xyz)
                        near = nearestMultiple(last)
                        comp = near - last
                        if comp == 4:
                            comp = 3
                        else:
                            comp = comp
                    else:
                        # if inputs are not consecutive
                        # automatically disqualified
                        print ("\nYou did not input consecutive integers.")
                        # print ("You are disqualified from the game.")
                        lose1()
            print ("\n\nCONGRATULATIONS !!!")
            print ("YOU WON !")
            exit(0)
              
        else:
            print ("wrong choice")
                          
          
game = True    
while game == True:
        print ("Player 2 is Computer.")
        print("Do you want to play the 21 number game? (Yes / No)")
        ans = input('> ')
        if ans =='Yes':
            start1()
        else:
            print ("Do you want quit the game?(yes / no)")
            nex = input('> ')
            if nex == "yes":
                print ("You are quitting the game...")
                exit(0)
            elif nex == "no":
                print ("Continuing...")
            else:
                print ("Wrong choice")
                 

Producción:

Player 2 is Computer.
Do you want to start the game? (Yes/No)
> Yes
Enter 'F' to take the first chance.
Enter 'S' to take the second chance.
> S
Order of inputs after computer's turn is:
[1]

Your turn.

How many numbers do you wish to enter?
> 3
Enter your values
> 2
> 3
> 4
Order of inputs after computer's turn is:
[1, 2, 3, 4, 5, 6, 7]

Your turn.

How many numbers do you wish to enter?
> 1
Enter your values
> 8
Order of inputs after computer's turn is:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

Your turn.

How many numbers do you wish to enter?
> 1
Enter your values
> 12
Order of inputs after computer's turn is:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

Your turn.

How many numbers do you wish to enter?
> 1
Enter your values
> 16
Order of inputs after computer's turn is:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

Your turn.

How many numbers do you wish to enter?
> 1
Enter your values
> 20


CONGRATULATIONS!!!
YOU WON! 

Pruébelo usted mismo como ejercicio:

  • Puede mejorar aún más el programa aumentando el número de jugadores.
  • También puede usar solo números pares/impares.
  • Puede reemplazar los números con el sistema numérico binario.
  • Puedes agregar niveles con variaciones en el juego.

Publicación traducida automáticamente

Artículo escrito por donika_3010 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 *