Untitled

Views: 1171Forks: 2

Anonymous   3 years ago   Never
Text   2.87 KB   Public
Crumb Tree
Highlight mode ON

Click on the lines you want to highlight and share this url.

    Download   RAW   Copy   Fork
  1. from random import *
  2.  
  3. def main() :
  4.     finish = True
  5.     while finish :
  6.         print("Benvenuto nel gioco di Nim.")
  7.         count = 0
  8.         player = randomBoolean()
  9.         numBalls = randint(10,100)
  10.         master = randint(1, 2)
  11.         computerPlayerIsExpert = False
  12.         if master == 1:
  13.             computerPlayerIsExpert = True
  14.             print("Computer intelligente")
  15.         else :
  16.             print("Computer stupido")
  17.         print("Le biglie totali sono: ", numBalls)
  18.         while numBalls >1 :
  19.             rimaste = 0
  20.             if player :
  21.                 print("Tocca al giocatore")
  22.                 newnumBalls = askUserBalls(numBalls)
  23.             else:
  24.                 print("Tocca al computer")
  25.                 newnumBalls = takeComputerBalls(computerPlayerIsExpert,numBalls)
  26.             rimaste = numBalls - newnumBalls
  27.             numBalls = newnumBalls
  28.             print("Sono state prese:" , rimaste ,"biglie. Ora le biglie sono:" , numBalls)
  29.             player = not player
  30.         if player :
  31.             print("Hai perso")
  32.         else :
  33.             print("Hai vinto")
  34.         finish = playAgain()
  35.  
  36.  
  37. def askUserBalls(numBalls) :
  38.     repeat = True
  39.     while repeat :
  40.         balls = int(input("Quante biglie vuoi prendere? "))
  41.         if balls < 1 or balls > numBalls//2 :
  42.             print("Errore! Il valore delle biglie deve essere compreso tra uno e la metà del totale delle biglie esclusa. Riprova!")
  43.         else :
  44.             numBalls -= balls
  45.             repeat = False
  46.     return numBalls
  47.  
  48.  
  49. def takeComputerBalls(computerPlayerIsExpert,numBalls) :
  50.     if computerPlayerIsExpert :
  51.         if numBalls == 3 or numBalls == 7 or numBalls == 15 or numBalls == 31 or numBalls == 63:
  52.             balls = randint(1, numBalls // 2)
  53.             numBalls -= balls
  54.         elif numBalls > 63:
  55.             balls = numBalls - 63
  56.         elif numBalls > 31:
  57.             balls = numBalls - 31
  58.         elif numBalls > 15:
  59.             balls = numBalls - 15
  60.         elif numBalls > 7:
  61.             balls = numBalls - 7
  62.         elif numBalls > 3:
  63.             balls = numBalls - 3
  64.         else:
  65.             balls = 1
  66.     else :
  67.         balls = randint(1, numBalls // 2)
  68.     numBalls -= balls
  69.     return numBalls
  70.  
  71.  
  72. def playAgain() :
  73.         playagain = input("Vuoi giocare ancora? ""s"" per il sì oppure ""n"" per il no: ")
  74.         if playagain == "n" :
  75.            return False
  76.         elif playagain == "s" :
  77.             return True
  78.  
  79.  
  80. def randomBoolean() :
  81.     x = randint(1,2)
  82.     if x == 1 :
  83.         return True
  84.     else :
  85.         return False
  86.  
  87.  
  88. main()