Advertisement
Kyrexar

Muertos

May 1st, 2013
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 KB | None | 0 0
  1. import random, time
  2.  
  3. class GranNError(Exception):
  4.     def __init__(self,arg):
  5.         self.args=arg
  6.  
  7. cad ="\n Bienvenido al juego de Muertos y heridos\n"
  8. cad+=" Tienes que adivinar un numero entre 0000 y 9999.\n"
  9. cad+=" Prueba numeros y como pista recibiras 1 herido por cada cifra acertada.\n"
  10. cad+=" Si aciertas tambien su posicion, en vez de un herido, recibiras 1 muerto.\n"
  11. cad+=" Ganas cuando consigas 4 muertos, es  decir, cuando ordenes los 4 numeros.\n"
  12. print(cad)
  13.  
  14. n1=random.randrange(10)
  15. n2=random.randrange(10)
  16. n3=random.randrange(10)
  17. n4=random.randrange(10)
  18. n=n1*1000+n2*100+n3*10+n4
  19.  
  20. while True:
  21.     try:
  22.         g=int(input(" Adivina que numero estoy pensando (0000-9999): "))
  23.         if g>9999: raise GranNError("")
  24.         break
  25.     except ValueError: print(" Eso no es un numero, vuelve a intentarlo")
  26.     except GranNError as e: print(" Numero no valido, prueba solo con 4 cifras")
  27.  
  28. intentos = 0
  29. correcto=False
  30. dif = time.time()
  31.  
  32. while not correcto :
  33.     heridos=0
  34.     muertos=0
  35.     intentos+=1
  36.    
  37.     g1=int(g/1000)
  38.     if g1==n1 : muertos+=1
  39.     elif g1==n2 or g1==n3 or g1==n4 : heridos+=1
  40.    
  41.     g2=int(g/100-g1*10)
  42.     if g2==n2 : muertos+=1
  43.     elif g2==n1 or g2==n3 or g2==n4 : heridos+=1
  44.    
  45.     g3=int(g/10-g1*100-g2*10)
  46.     if g3==n3 : muertos+=1
  47.     elif g3==n1 or g3==n2 or g3==n4 : heridos+=1
  48.    
  49.     g4=int(g-g1*1000-g2*100-g3*10)
  50.     if g4==n4 : muertos+=1
  51.     elif g4==n1 or g4==n2 or g3==n3 : heridos+=1
  52.    
  53.     if muertos==4 : correcto=True
  54.     else :
  55.         cad = str(g1)+str(g2)+str(g3)+str(g4)
  56.         while True:
  57.             try:
  58.                 g=int(input(" En "+cad+" hay "+str(muertos)+" muertos y "+str(heridos)+" heridos. Sigue intentandolo: "))
  59.                 if g>9999: raise GranNError("")
  60.                 break
  61.             except ValueError: print(" Eso no es un numero, vuelve a intentarlo")
  62.             except GranNError as e: print(" Numero no valido, prueba solo con 4 cifras")
  63.  
  64. dif = time.time()-dif
  65. puntuacion = int((1.0/intentos + 1.0/(dif+10**10))*1000)
  66.  
  67. print(" Has acertado. Intentos: "+str(intentos)+". Tiempo: "+str(round(dif,3))+" segundos. Puntos: "+str(puntuacion))
  68. input(" Pulsa INTRO para salir")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement