from bibV3 import *


#Exercice 1
def demarquageSommets(G):
    for s in listeSommets(G):
        demarquerSommet(s)

#Exercice 2
def nombreSommetsMarques(G):
    c = 0
    for s in listeSommets(G):
        if estMarqueSommet(s):
            c = c + 1
    return c

#Exercice 3
def tousEnRouge(G):
    for s in listeSommets(G):
        if couleurSommet(s) != "red":
            return False
    return True


#Exercice 4
def coloriageAleatoire(G):
    for s in listeSommets(G):
        colorierSommet(s, elementAleatoireListe(["blue","red"]) )
        
#Exercice 5
def existeVoisinAmiMarque(s):
    for t in listeVoisins(s):
        if estMarqueSommet(t) and (couleurSommet(t) == couleurSommet(s)):
            return True
    return False

#Exercice 6
def changeCouleur(s):
    if couleurSommet(s) == "red":
        colorierSommet(s,"blue")
    elif couleurSommet(s) == "blue":
        colorierSommet(s,"red")

#Exercice 7
def negatif(l):
    for s in l:
        changeCouleur(s)

#Exercice 8
def voisinAVisiter(G):
    for s in listeSommets(G):
        if (not estMarqueSommet(s)) and existeVoisinAmiMarque(s):
            return s

#Exercice 9
def nombreSommetsComposanteColoree(G,s):
    demarquageSommets(G)
    marquerSommet(s)
    while voisinAVisiter(G) != None:
        marquerSommet(voisinAVisiter(G))   
    return nombreSommetsMarques(G)

#Exercice 10
def listeSommetsChangeants(G):
    l = []
    for s in listeSommets(G):
        m = 0
        for v in listeVoisins(s):
            if couleurSommet(v) != couleurSommet(s) and m < nombreSommetsComposanteColoree(G,v):
                m = nombreSommetsComposanteColoree(G,v)
        if m > nombreSommetsComposanteColoree(G,s):
            l.append(s)
    demarquageSommets(G)
    return l

def etapeDeChangement(G):
    negatif(listeSommetsChangeants(G))

#Exercice 11
def equilibre(G):
    while len(listeSommetsChangeants(G)) != 0 :
        etapeDeChangement(G)

#Exercice 12
def coloriageEquilibreAleatoire(G):
    [c1,c2]=elementAleatoireListe([["red","blue"],["blue","red"]])
    for s in listeSommets(G):
        colorierSommet(s,c1)
    for k in range(nbSommets(G)//2):
        s = elementAleatoireListe(listeSommets(G))
        while couleurSommet(s) != c1:
            s = elementAleatoireListe(listeSommets(G))
        colorierSommet(s,c2)


#Exercice 13

def existeBlanc(G):
    for s in listeSommets(G):
        if couleurSommet(s) == 'white':
            return True
    return False

def listeBlancs(G):
    l = []
    for s in listeSommets(G):
        if couleurSommet(s) == 'white':
            l.append(s)
    return l

        
def jeuComposanteColoree(G):
    for s in listeSommets(G):
        colorierSommet(s,"white")
    rougeactif = True
    dessiner(G)
    while existeBlanc(G):
        st = "Joueur "
        if rougeactif:
            st = st + "rouge, "
        else:
            st = st + "bleu, "
        st = st + "rentrez le nom d'un sommet ou une instruction : "
        som = None
        print(st)
        while som == None:
            com = input("")
            if com == "sommets":
                for s in listeBlancs(G):
                    print(nomSommet(s))
                print("Veuillez rentrez le nom d'un sommet ou une instruction : ")
#            elif com == "dessiner":
#                dessiner(G)
#                print("Veuillez rentrez le nom d'un sommet ou une instruction : ")
            else:
                som = sommetNom(G,com)
                if som == None:
                    print ("Ce sommet n'existe pas, veuillez recommencer.")
                if couleurSommet(som) != "white":
                    print ("Ce sommet est déjà coloré, veuillez recommencer.")
                    som = None
        if rougeactif:
            colorierSommet(som,"red")
        else:
            colorierSommet(som,"blue")
        rougeactif = not(rougeactif)
        dessiner(G)
    equilibre(G)
    dessiner(G)
    if tousEnRouge(G):
        print("Rouge a gagné !")
    else :
        print("Bleu a gagné !")

        
    
