jeu.py

Created by maeldepallens

Created on January 13, 2022

5.59 KB


# Type your text here
import random
import time



def affiche(liste) :
    for u in range(3) :
        tout = ""
        for i in range(3) :
            v = liste[i][2-u]
            if v == None :
                v = " "
            tout += "|"+v+"|"
        print(tout)
        print("---------")



def jeu() :
    print("Bonjour dans mon programme du morpion Le joueur 1 est O est le joueur 2 est X")
    print("")
    z=0
    liste = [[None,None,None],[None,None,None],[None,None,None]]
    liste2 = []
    listetout = []
    affiche(liste)
    tour = 0
    boucle = 0
    while tour != 9:
        listecl = []
        boucle = 0
        if tour%2 == 0 :
            while boucle == 0 :
                print("")
                ligne = int(input("Joueur 1 choisit ta ligne :  "))
                colone = int(input("Joueur 1 choisit ta colone  :  "))
                print("")

                listecl = [colone,ligne]
                if listecl in listetout :
                    print("Cette case est déja prise")
                else :
                    boucle = 1

                listetout.append(listecl)
            for i in range(100) :
                print("")
            liste[colone][ligne] = "O"







        else :
            while boucle == 0 :
                ligne = int(input("Joueur 2 choisit ta ligne :  "))
                colone = int(input("Joueur 2 choisit ta colone  :  "))
                print("")
                listecl = [colone,ligne]
                if listecl in listetout :
                    print("Cette case est déja prise")
                else :
                    boucle = 1

                listetout.append(listecl)
            for i in range(100) :
                print("")
            liste[colone][ligne] = "X"



        affiche(liste)
        print("")
        print("")
        g = vaincqueur(liste)
        tour += 1
        if g == "j1" :
            return "Le joueur 1 est le vaincqueur "
        elif g == "j2" :
            return "Le joueur 2 est le vaincqueur "
        if tour == 9 :
            return "Il y a égalité "
        else :
            pass

    print("")
    print("Il y a égalité ")




def vaincqueur(liste) :
    j1 = 0
    j2 = 0
    if liste[0][0] == "O" and liste[0][1] == "O" and liste[0][2] == "O" :
        j1 = True


    elif liste[1][0] == "O" and liste[1][1] == "O" and liste[1][2] == "O" :
        j1 = True


    elif liste[2][0] == "O" and liste[2][1] == "O" and liste[2][2] == "O" :
        j1 = True



    elif liste[0][0] == "O" and liste[1][0] == "O" and liste[2][0] == "O" :
        j1 = True


    elif liste[0][1] == "O" and liste[1][1] == "O" and liste[2][1] == "O" :
        j1 = True



    elif liste[0][2] == "O" and liste[1][2] == "O" and liste[2][2] == "O" :
        j1 = True



    elif liste[0][0] == "O" and liste[1][1] == "O" and liste[2][2] == "O" :
        j1 = True



    elif liste[2][0] == "O" and liste[1][1] == "O" and liste[0][2] == "O" :
        j1 = True

    elif liste[0][0] == "X" and liste[0][1] == "X" and liste[0][2] == "X" :
        j2 = True


    elif liste[1][0] == "X" and liste[1][1] == "X" and liste[1][2] == "X" :
        j2 = True


    elif liste[2][0] == "X" and liste[2][1] == "X" and liste[2][2] == "X" :
        j2 = True



    elif liste[0][0] == "X" and liste[1][0] == "X" and liste[2][0] == "X" :
        j2 = True


    elif liste[0][1] == "X" and liste[1][1] == "X" and liste[2][1] == "X" :
        j2 = True



    elif liste[0][2] == "X" and liste[1][2] == "X" and liste[2][2] == "X" :
        j2 = True



    elif liste[0][0] == "X" and liste[1][1] == "O" and liste[2][2] == "X" :
        j2 = True



    elif liste[2][0] == "X" and liste[1][1] == "X" and liste[0][2] == "X" :
        j2 = True


    if j1 == True :
        return "j1"
    elif j2 == True :
        return "j2"
    else :
        return None





def jeu_IA() :
    print("Bonjour dans mon programme du morpion ")
    print("")
    z=0
    liste = [[None,None,None],[None,None,None],[None,None,None]]
    liste2 = []
    listetout = []
    affiche(liste)
    tour = 0
    boucle = 0
    while tour != 9:
        listecl = []
        boucle = 0
        if tour%2 == 0 :
            while boucle == 0 :
                print("")
                ligne = int(input("Choisit ta ligne :  "))
                colone = int(input("Choisit ta colone  :  "))
                print("")
        
                listecl = [colone,ligne]
                if listecl in listetout :
                    print("Cette case est déja prise")
                else :
                    boucle = 1
        
                listetout.append(listecl)
            for i in range(100) :
                print("")
            liste[colone][ligne] = "O"
        
        
        
        
        
        
        
        else :
            while boucle == 0 :
                print("")
                ligne = random.randint(0,2)
                colone = random.randint(0,2)
                listecl = [colone,ligne]
                if listecl in listetout :
                    pass
                else :
                    boucle = 1
        
                listetout.append(listecl)
            for i in range(100) :
                print("")
            liste[colone][ligne] = "X"
    
    
    
        affiche(liste)
        time.sleep(2)
        print("")
        print("")
        g = vaincqueur(liste)
        tour += 1
        if g == "j1" :
            return "Tu as Gagné"
        elif g == "j2" :
            return "Tu as Perdu  "
        if tour == 9 :
            return "Il y a égalité "
        else :
            pass
        
    print("")
    print("Il y a égalité ")






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.