nim.py

Created by squirreline

Created on September 15, 2018

1.57 KB

Permet de jouer au jeu de Nim : il y a n allumettes et deux joueurs. A chaque tout chacun enlève un nb d’allumettes dans un intervalle fixé au préalable (de 1 à max). Celui qui prend la dernière allumette a perdu. nim_aleatoire : l’ordinateur joue au hasard nim_gagnant : l’ordinateur applique la stratégie gagnante, il est impossible de gagner


from random import*

def nim_aleatoire(n,max=4):
  
  rep=input("_Voulez vous commencer ?")
  if rep == "oui":
    a=int(input("nb retire="))
    b = randrange (1,max)
    print("_python retire",b)
    n = n - a
    n= n - b
    print("il reste",n)
  else:
    b = randrange (1,max)
    print("_python retire",b)
    n = n - b
    print("il reste",n)
    
  
  while n >= 1:
    a=int(input("nb retire="))
    b = randrange (1,max)
    n1 = n - a
    if n1 == 1:
      b=1
    n2= n1 - b
    n=n2
    print("_python retire",b)
    print("il reste",n2)
    
    if n1 == 1:
      print("*vous avez gagne*")
    if n2 == 1:
      print("*vous avez perdu*")
    if n1 == 0:
      print("*vous avez perdu*")
      
      
      
      
def nim_gagnant(n,max=4):
  
  if (n-1)%(max+1) == 0:
    print("_Je vous laisse commencer")
    while n >= 1:
      a=int(input("nb retire="))
      n1= n - a
      b = (max+1) - (n - n1)
      if n1 == 0:
        b=0
      print("_je retire",b)
      n2= n1-b
      print("il reste",n2)
      n = n2
      if n1 == 0:
        print("Il ne reste plus d'allumettes")
        print("*vous avez perdu*")
        
  if (n-1)%(max+1) and n != 0:
    print("_je commence")
    while n >= 1:
      if n%(max+1)==0:
        b= max
        print("_je retire",b)
        print("il reste",n-b)
      else:
        b= n%(max+1) - 1
        print("_je retire",b)
        print("il reste",n-b)
      a=int(input("nb retire="))
      n1= n - a
      n2= n1 - b  
      n=n2
    print("Il ne reste plus d'allumettes")
    print("*vous avez perdu*")
  
  

        
        
      

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.