Partage
  • Partager sur Facebook
  • Partager sur Twitter

Puissance 4 - Python

Code - Soluce

    18 septembre 2019 à 23:28:39

    Bonjour/Bonsoir tout le monde ! Je souhaite juste vous partager un puissance 4 que je viens de créer en Python. Il n'est certe pas très optimisé en terme de ligne mais il fonctionne x)

    #CODING : utf_8
    import os
    os.system("color a")
    os.system("title Puissance 4")
    
    fin=False
    libre=True
    jeton = "  "
    
    j1 = str(input("Quel est le nom du 1er joueur ? "))
    if j1 == "":
        j1 = "Inconnue 1"
    
    j2 = str(input("Quel est le nom du 2eme joueur ? "))
    if j2 == "":
        j2 = "Inconnue 2"
    
    a1=a2=a3=a4=a5=a6=a7=b1=b2=b3=b4=b5=b6=b7=c1=c2=c3=c4=c5=c6=c7=d1=d2=d3=d4=d5=d6=d7=e1=e2=e3=e4=e5=e6=e7=f1=f2=f3=f4=f5=f6=f7=" "
    
    joueur = j2    
    
    
    while 1==1:
        print("\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  ")     
        print( " 1   2   3   4   5   6   7")
        print( "============================")
        print(" "+f1,"|",f2,"|",f3,"|",f4 ,  "|",f5 ,  "|",f6 ,  "|",f7   )
        print(" "+e1,"|",e2,"|",e3  , "|",e4,   "|",e5,   "|",e6 ,  "|",e7)
        print( " "+d1,"|",d2,"|",d3 ,  "|",d4,   "|",d5,   "|",d6,   "|",d7 )
        print( " "+c1,"|",c2,"|",c3  , "|",c4  , "|",c5 ,  "|",c6 ,  "|",c7  )
        print( " "+b1,"|",b2,"|",b3,   "|",b4,   "|",b5 ,  "|",b6,   "|",b7)
        print( " "+a1,"|",a2,"|",a3,   "|",a4  , "|",a5,   "|",a6   ,"|",a7 )
        print( "============================")
    
        
        if a1 == "O" and a2 == "O" and a3 == "O" and a4 == "O":       #vérifie les lignes horizontales
            fin = True
        elif a2 == "O" and a3 == "O" and a4 == "O" and a5 == "O":
            fin = True
        elif a3 == "O" and a4 == "O" and a5 == "O" and a6 == "O":
            fin = True
        elif a4 == "O" and a5 == "O" and a6 == "O" and a7 == "O":
            fin = True
            
        elif b1 == "O" and b2 == "O" and b3 == "O" and b4 == "O":         #vérifie les lignes horizontales
            fin = True
        elif b2 == "O" and b3 == "O" and b4 == "O" and b5 == "O":
            fin = True
        elif b3 == "O" and b4 == "O" and b5 == "O" and b6 == "O":
            fin = True
        elif b4 == "O" and b5 == "O" and b6 == "O" and b7 == "O":
            fin = True
    
        elif c1 == "O" and c2 == "O" and c3 == "O" and c4 == "O":         #vérifie les lignes horizontales
            fin = True
        elif c2 == "O" and c3 == "O" and c4 == "O" and b5 == "O":
            fin = True
        elif c3 == "O" and c4 == "O" and c5 == "O" and b6 == "O":
            fin = True
        elif c4 == "O" and c5 == "O" and c6 == "O" and b7 == "O":
            fin = True
    
        elif d1 == "O" and d2 == "O" and d3 == "O" and d4 == "O":         #vérifie les lignes horizontales
            fin = True
        elif d2 == "O" and d3 == "O" and d4 == "O" and d5 == "O":
            fin = True
        elif d3 == "O" and d4 == "O" and d5 == "O" and d6 == "O":
            fin = True
        elif d4 == "O" and d5 == "O" and d6 == "O" and d7 == "O":
            fin = True
    
        elif e1 == "O" and e2 == "O" and e3 == "O" and e4 == "O":         #vérifie les lignes horizontales
            fin = True
        elif e2 == "O" and e3 == "O" and e4 == "O" and e5 == "O":
            fin = True
        elif e3 == "O" and e4 == "O" and e5 == "O" and e6 == "O":
            fin = True
        elif e4 == "O" and e5 == "O" and e6 == "O" and e7 == "O":
            fin = True
    
        elif f1 == "O" and f2 == "O" and f3 == "O" and f4 == "O":         #vérifie les lignes horizontales
            fin = True
        elif f2 == "O" and f3 == "O" and f4 == "O" and f5 == "O":
            fin = True
        elif f3 == "O" and f4 == "O" and f5 == "O" and f6 == "O":
            fin = True
        elif f4 == "O" and f5 == "O" and f6 == "O" and f7 == "O":
            fin = True
    
    
    
        elif a1 == "O" and b1 == "O" and c1 == "O" and d1 == "O":         #vérifie les lignes verticales
            fin = True
        elif b1 == "O" and c1 == "O" and d1 == "O" and e1 == "O":
            fin = True
        elif c1 == "O" and d1 == "O" and e1 == "O" and f1 == "O":
            fin = True
    
        elif a2 == "O" and b2 == "O" and c2 == "O" and d2 == "O":         #vérifie les lignes verticales
            fin = True
        elif b2 == "O" and c2 == "O" and d2 == "O" and e2 == "O":
            fin = True
        elif c2 == "O" and d2 == "O" and e2 == "O" and f2 == "O":
            fin = True
    
        elif a3 == "O" and b3 == "O" and c3 == "O" and d3 == "O":         #vérifie les lignes verticales
            fin = True
        elif b3 == "O" and c3 == "O" and d3 == "O" and e3 == "O":
            fin = True
        elif c3 == "O" and d3 == "O" and e3 == "O" and f3 == "O":
            fin = True
    
        elif a4 == "O" and b4 == "O" and c4 == "O" and d4 == "O":         #vérifie les lignes verticales
            fin = True
        elif b4 == "O" and c4 == "O" and d4 == "O" and e4 == "O":
            fin = True
        elif c4 == "O" and d4 == "O" and e4 == "O" and f4 == "O":
            fin = True
    
        elif a5 == "O" and b5 == "O" and c5 == "O" and d5 == "O":         #vérifie les lignes verticales
            fin = True
        elif b5 == "O" and c5 == "O" and d5 == "O" and e5 == "O":
            fin = True
        elif c5 == "O" and d5 == "O" and e5 == "O" and f5 == "O":
            fin = True
    
        elif a6 == "O" and b6 == "O" and c6 == "O" and d6 == "O":         #vérifie les lignes verticales
            fin = True
        elif b6 == "O" and c6 == "O" and d6 == "O" and e6 == "O":
            fin = True
        elif c6 == "O" and d6 == "O" and e6 == "O" and f6 == "O":
            fin = True
    
        elif a7 == "O" and b7 == "O" and c7 == "O" and d7 == "O":         #vérifie les lignes verticales
            fin = True
        elif b7 == "O" and c7 == "O" and d7 == "O" and e7 == "O":
            fin = True
        elif c7 == "O" and d7 == "O" and e7 == "O" and f7 == "O":
            fin = True
    
    
        elif a1 == "O" and b2 == "O" and c3 == "O" and d4 == "O":         #vérifie les diagonales droites
            fin = True
        elif b2 == "O" and c3 == "O" and d4 == "O" and e5 == "O":
            fin = True
        elif c3 == "O" and d4 == "O" and e5 == "O" and f6 == "O":
            fin = True
    
        elif a2 == "O" and b3 == "O" and c4 == "O" and d5 == "O":         #vérifie les diagonales droites
            fin = True
        elif b3 == "O" and c4 == "O" and d5 == "O" and e6 == "O":
            fin = True
        elif c4 == "O" and d5 == "O" and e6 == "O" and f7 == "O":
            fin = True
    
        elif a3 == "O" and b4 == "O" and c5 == "O" and d6 == "O":         #vérifie les diagonales droites
            fin = True
        elif b4 == "O" and c5 == "O" and d6 == "O" and e7 == "O":
            fin = True
    
        elif a4 == "O" and b5 == "O" and c6 == "O" and d7 == "O":         #vérifie les diagonales droites
            fin = True
            
    
    
           
        elif a7 == "O" and b6 == "O" and c5 == "O" and d4 == "O":         #vérifie les diagonales gauche
            fin = True
        elif b6 == "O" and c5 == "O" and d4 == "O" and e3 == "O":
            fin = True
        elif c4 == "O" and d3 == "O" and e1 == "O" and f1 == "O":
            fin = True
    
        elif a6 == "O" and b5 == "O" and c4 == "O" and d3 == "O":         #vérifie les diagonales gauche
            fin = True
        elif b5 == "O" and c4 == "O" and d2 == "O" and e2 == "O":
            fin = True
        elif c4 == "O" and d3 == "O" and e2 == "O" and f1 == "O":
            fin = True
    
        elif a5 == "O" and b4 == "O" and c3 == "O" and d2 == "O":         #vérifie les diagonales gauche
            fin = True
        elif b4 == "O" and c3 == "O" and d2 == "O" and e1 == "O":
            fin = True
    
        elif a4 == "O" and b3 == "O" and c2 == "O" and d1 == "O":         #vérifie les diagonales gauche
            fin = True
    
    
    
        if a1 == "X" and a2 == "X" and a3 == "X" and a4 == "X":     #vérifie les lignes horizontales
            fin = True
        elif a2 == "X" and a3 == "X" and a4 == "X" and a5 == "X":
            fin = True
        elif a3 == "X" and a4 == "X" and a5 == "X" and a6 == "X":
            fin = True
        elif a4 == "X" and a5 == "X" and a6 == "X" and a7 == "X":
            fin = True
            
        elif b1 == "X" and b2 == "X" and b3 == "X" and b4 == "X":         #vérifie les lignes horizontales
            fin = True
        elif b2 == "X" and b3 == "X" and b4 == "X" and b5 == "X":
            fin = True
        elif b3 == "X" and b4 == "X" and b5 == "X" and b6 == "X":
            fin = True
        elif b4 == "X" and b5 == "X"  and b6 == "X" and b7 == "X":
            fin = True
    
        elif c1 == "X" and c2 == "X" and c3 == "X" and c4 == "X":         #vérifie les lignes horizontales
            fin = True
        elif c2 == "X" and c3 == "X" and c4 == "X" and b5 == "X":
            fin = True
        elif c3 == "X" and c4 == "X" and c5 == "X" and b6 == "X":
            fin = True
        elif c4 == "X" and c5 == "X" and c6 == "X" and b7 == "X":
            fin = True
    
        elif d1 == "X" and d2 == "X" and d3 == "X" and d4 == "X":         #vérifie les lignes horizontales
            fin = True
        elif d2 == "X" and d3 == "X" and d4 == "X" and d5 == "X":
            fin = True
        elif d3 == "X" and d4 == "X" and d5 == "X" and d6 == "X":
            fin = True
        elif d4 == "X" and d5 == "X" and d6 == "X" and d7 == "X":
            fin = True
    
        elif e1 == "X" and e2 == "X" and e3 == "X" and e4 == "X":         #vérifie les lignes horizontales
            fin = True
        elif e2 == "X" and e3 == "X" and e4 == "X" and e5 == "X":
            fin = True
        elif e3 == "X" and e4 == "X" and e5 == "X" and e6 == "X":
            fin = True
        elif e4 == "X" and e5 == "X" and e6 == "X" and e7 == "X":
            fin = True
    
        elif f1 == "X" and f2 == "X" and f3 == "X" and f4 == "X":         #vérifie les lignes horizontales
            fin = True
        elif f2 == "X" and f3 == "X" and f4 == "X" and f5 == "X":
            fin = True
        elif f3 == "X" and f4 == "X" and f5 == "X" and f6 == "X":
            fin = True
        elif f4 == "X" and f5 == "X" and f6 == "X" and f7 == "X":
            fin = True
    
    
    
        elif a1 == "X" and b1 == "X" and c1 == "X" and d1 == "X":         #vérifie les lignes verticales
            fin = True
        elif b1 == "X" and c1 == "X" and d1 == "X" and e1 == "X":
            fin = True
        elif c1 == "X" and d1 == "X" and e1 == "X" and f1 == "X":
            fin = True
    
        elif a2 == "X" and b2 == "X" and c2 == "X" and d2 == "X":         #vérifie les lignes verticales
            fin = True
        elif b2 == "X" and c2 == "X" and d2 == "X" and e2 == "X":
            fin = True
        elif c2 == "X" and d2 == "X" and e2 == "X" and f2 == "X":
            fin = True
    
        elif a3 == "X" and b3 == "X" and c3 == "X" and d3 == "X":         #vérifie les lignes verticales
            fin = True
        elif b3 == "X" and c3 == "X" and d3 == "X" and e3 == "X":
            fin = True
        elif c3 == "X" and d3 == "X" and e3 == "X" and f3 == "X":
            fin = True
    
        elif a4 == "X" and b4 == "X" and c4 == "X" and d4 == "X":         #vérifie les lignes verticales
            fin = True
        elif b4 == "X" and c4 == "X" and d4 == "X" and e4 == "X":
            fin = True
        elif c4 == "X" and d4 == "X" and e4 == "X" and f4 == "X":
            fin = True
    
        elif a5 == "X" and b5 == "X" and c5 == "X" and d5 == "X":         #vérifie les lignes verticales
            fin = True
        elif b5 == "X" and c5 == "X" and d5 == "X" and e5 == "X":
            fin = True
        elif c5 == "X" and d5 == "X" and e5 == "X" and f5 == "X":
            fin = True
    
        elif a6 == "X" and b6 == "X" and c6 == "X" and d6 == "X":         #vérifie les lignes verticales
            fin = True
        elif b6 == "X" and c6 == "X" and d6 == "X" and e6 == "X":
            fin = True
        elif c6 == "X" and d6 == "X" and e6 == "X" and f6 == "X":
            fin = True
    
        elif a7 == "X" and b7 == "X" and c7 == "X" and d7 == "X":         #vérifie les lignes verticales
            fin = True
        elif b7 == "X" and c7 == "X" and d7 == "X" and e7 == "X":
            fin = True
        elif c7 == "X" and d7 == "X" and e7 == "X" and f7 == "X":
            fin = True
    
    
        elif a1 == "X" and b2 == "X" and c3 == "X" and d4 == "X":         #vérifie les diagonales droites
            fin = True
        elif b2 == "X" and c3 == "X" and d4 == "X" and e5 == "X":
            fin = True
        elif c3 == "X" and d4 == "X" and e5 == "X" and f6 == "X":
            fin = True
    
        elif a2 == "X" and b3 == "X" and c4 == "X" and d5 == "X":         #vérifie les diagonales droites
            fin = True
        elif b3 == "X" and c4 == "X" and d5 == "X" and e6 == "X":
            fin = True
        elif c4 == "X" and d5 == "X" and e6 == "X" and f7 == "X":
            fin = True
    
        elif a3 == "X" and b4 == "X" and c5 == "X" and d6 == "X":         #vérifie les diagonales droites
            fin = True
        elif b4 == "X" and c5 == "X" and d6 == "X" and e7 == "X":
            fin = True
    
        elif a4 == "X" and b5 == "X" and c6 == "X" and d7 == "X":         #vérifie les diagonales droites
            fin = True
            
    
    
           
        elif a7 == "X" and b6 == "X" and c5 == "X" and d4 == "X":         #vérifie les diagonales gauche
            fin = True
        elif b6 == "X" and c5 == "X" and d4 == "X" and e3 == "X":
            fin = True
        elif c4 == "X" and d3 == "X" and e1 == "X" and f1 == "X":
            fin = True
    
        elif a6 == "X" and b5 == "X" and c4 == "X" and d3 == "X":         #vérifie les diagonales gauche
            fin = True
        elif b5 == "X" and c4 == "X" and d2 == "X" and e2 == "X":
            fin = True
        elif c4 == "X" and d3 == "X" and e2 == "X" and f1 == "X":
            fin = True
    
        elif a5 == "X" and b4 == "X" and c3 == "X" and d2 == "X":         #vérifie les diagonales gauche
            fin = True
        elif b4 == "X" and c3 == "X" and d2 == "X" and e1 == "X":
            fin = True
    
        elif a4 == "X" and b3 == "X" and c2 == "X" and d1 == "X":         #vérifie les diagonales gauche
            fin = True
    
            
        if fin == True:
            print("Le vainqueur est",joueur,"!")
            break
    
    
        
        if joueur == j2:
            joueur = j1
        elif joueur == j1:
            joueur = j2
        
        
    
        print("C'est à",joueur, "de jouer.")
    
        if joueur == j1:
            jeton = "O"
        elif joueur == j2:
            jeton = "X"
    
    
        while 1==1:
           
            try:
                choix = str(input("Quel collone choisissez vous ? "))
                if choix=="":
                    raise NameError
            except:
                print("Veuillez choisir une colonne valide.")
                continue
    
            
            if choix == "1":
                if a1 == " ":
                    a1 = jeton
                elif b1 == " ":
                    b1 = jeton
                elif c1 == " ":
                    c1 = jeton
                elif d1 == " ":
                    d1 = jeton
                elif e1 == " ":
                    e1 = jeton
                elif f1 == " ":
                    f1 = jeton
                else:
                    libre = False
                if libre == False:
                    print("Il n'y a pas de place libre.")
                    libre = True
                    continue
    
    
            elif choix == "2":
                if a2 == " ":
                    a2 = jeton
                elif b2 == " ":
                    b2 = jeton
                elif c2 == " ":
                    c2 = jeton
                elif d2 == " ":
                    d2 = jeton
                elif e2 == " ":
                    e2 = jeton
                elif f2 == " ":
                    f2 = jeton
                else:
                    libre = False
                        
                if libre == False:
                    print("Il n'y a pas de place libre.")
                    libre = True
                    continue
    
    
            elif choix == "3":
                if a3 == " ":
                    a3 = jeton
                elif b3 == " ":
                    b3 = jeton
                elif c3 == " ":
                    c3 = jeton
                elif d3 == " ":
                    d3 = jeton
                elif e3 == " ":
                    e3 = jeton
                elif f3 == " ":
                    f3 = jeton
                else:
                    libre = False
    
                if libre == False:
                    print("Il n'y a pas de place libre.")
                    libre = True
                    continue
    
    
            elif choix == "4":
                if a4 == " ":
                    a4 = jeton
                elif b4 == " ":
                    b4 = jeton
                elif c4 == " ":
                    c4 = jeton
                elif d4 == " ":
                    d4 = jeton
                elif e4 == " ":
                    e4 = jeton
                elif f4 == " ":
                    f4 = jeton
                else:
                    libre = False
    
                if libre == False:
                    print("Il n'y a pas de place libre.")
                    libre = True
                    continue
    
    
            elif choix == "5":
                if a5 == " ":
                    a5 = jeton
                elif b5 == " ":
                    b5 = jeton
                elif c5 == " ":
                    c5 = jeton
                elif d5 == " ":
                    d5 = jeton
                elif e5 == " ":
                    e5 = jeton
                elif f5 == " ":
                    f5 = jeton
                else:
                    libre = False
    
                if libre == False:  
                    print("Il n'y a pas de place libre.")
                    libre = True
                    continue
    
    
            elif choix == "6":         
                if a6 == " ":
                    a6 = jeton
                elif b6 == " ":
                    b6 = jeton
                elif c6 == " ":
                    c6 = jeton
                elif d6 == " ":
                    d6 = jeton
                elif e6 == " ":
                    e6 = jeton
                elif f6 == " ":
                    f6 = jeton
                else:
                    libre = False
    
                if libre == False:
                    print("Il n'y a pas de place libre.")
                    libre = True
                    continue
    
    
            elif choix == "7":
                if a7 == " ":
                    a7 = jeton
                elif b7 == " ":
                    b7 = jeton
                elif c7 == " ":
                    c7 = jeton
                elif d7 == " ":
                    d7 = jeton
                elif e7 == " ":
                    e7 = jeton
                elif f7 == " ":
                    f7 = jeton
                else:
                    libre = False
    
                if libre == False:      
                    print("Il n'y a pas de place libre.")
                    libre = True
                    continue
    
    
            elif choix !="1" and choix !="2" and choix !="3" and choix !="4" and choix !="5" and choix !="6" and choix !="7":
                print("Choisissez une colonne entre 1 et 7")
            break
     
    
    os.system("pause")
    

    -
    Edité par Hikudo 19 septembre 2019 à 19:12:32

    • Partager sur Facebook
    • Partager sur Twitter
      18 septembre 2019 à 23:55:26

      Combien de temps pour écrire le code x)
      • Partager sur Facebook
      • Partager sur Twitter

      "I believe in two things. Discipline and the Bible." The Shawshank Redemption

        19 septembre 2019 à 7:57:20

        Utilise des listes pour le simplifier ...
        • Partager sur Facebook
        • Partager sur Twitter
          19 septembre 2019 à 12:48:11

          Clairement il va falloir reprendre la base car un code comme ça c'est pas possible ... Si je te dis "change ton code pour faire un puissance 5, 6, 8" tu vas t'amuser
          • Partager sur Facebook
          • Partager sur Twitter
            19 septembre 2019 à 13:32:01

            Ca a pas du être une partie de plaisir à coder :lol:
            • Partager sur Facebook
            • Partager sur Twitter
              19 septembre 2019 à 14:12:32

              Petit exo : essaye de faire un puissance 4 où le joueur peut choisir le nombre de lignes et de colonnes entre 5 et 1000 :p

              Normalement ça prend beaucoup moins de lignes de code.

              -
              Edité par thelinekioubeur 19 septembre 2019 à 14:13:05

              • Partager sur Facebook
              • Partager sur Twitter
                19 septembre 2019 à 16:39:46

                -
                Edité par bergiLeZÉR0 19 septembre 2019 à 16:41:13

                • Partager sur Facebook
                • Partager sur Twitter

                "I believe in two things. Discipline and the Bible." The Shawshank Redemption

                  19 septembre 2019 à 19:14:01

                  Nephthys a écrit:

                  Clairement il va falloir reprendre la base car un code comme ça c'est pas possible ... Si je te dis "change ton code pour faire un puissance 5, 6, 8" tu vas t'amuser

                  effectivement, il me semblait bien que c'était pas normal d'avoir autant de lignes, je vais essayer de faire un truc plus opti ça serait mieux, faut que je m'y prenne autrement

                  • Partager sur Facebook
                  • Partager sur Twitter
                    25 septembre 2019 à 11:38:45

                    Hikudo a écrit:

                    Nephthys a écrit:

                    Clairement il va falloir reprendre la base car un code comme ça c'est pas possible ... Si je te dis "change ton code pour faire un puissance 5, 6, 8" tu vas t'amuser

                    effectivement, il me semblait bien que c'était pas normal d'avoir autant de lignes, je vais essayer de faire un truc plus opti ça serait mieux, faut que je m'y prenne autrement

                    Une règle de base : quand on commence à utiliser des variables numérotés, c'est qu'on va dans le mur. Dans ce cas, il faut utiliser des listes.

                    Donc, plutôt que d'avoir des variables a1, a2, ... an, tu as une liste a contenant n éléments et tu y accèdes via leur index respectif a[1], a[2], ... a[n]
                    De la même manière pour tes lignes, au lieu d'avoir un liste pour la ligne a, une liste pour la ligne b, etc. codées "en dur", tu as une liste de listes.

                    Donc au final tu aurais un structure de données comme celle-la :

                    l=[

                    [["a1"],["a2"],...,["an"]]

                    ..

                    [["f1"],["f2"],...,["fn"]]

                    ]

                    Donc pour accéder à l'élément "a2" tu n'as plus qu'à faire l=[0][1]

                    • Partager sur Facebook
                    • Partager sur Twitter

                    Puissance 4 - Python

                    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                    • Editeur
                    • Markdown