n = 6
m = 7

def f(x):
    if x==0: return " "
    elif x==1 : return "o"
    elif x==2 : return "x"

def afficher_grille(grille):
    num = [" "]+[str(j) for j in range(m)]+[" "]
    print(" ".join(num))
    for i in range(n):
        l = [str(i)]
        l += [f(x) for x in grille[i]]
        l.append(str(i))
        print("|".join(l))
    print(" ".join(num))

def coup_possible(hauteur, k):
    return k>=0 and k<m and hauteur[k]<n

def jouer(grille, hauteur, k, p):
    grille[n - 1 - hauteur[k]][k] = p
    hauteur[k]+=1

ex_grille = [[0]*7 for _ in range(6)]

ex_hauteur = [0]*7

jouer(ex_grille, ex_hauteur, 3, 1)
jouer(ex_grille, ex_hauteur, 3, 2)
jouer(ex_grille, ex_hauteur, 2, 1)
jouer(ex_grille, ex_hauteur, 1, 2)
jouer(ex_grille, ex_hauteur, 3, 1)

def victoire(grille, p):
    p4 = [p]*4
    for i in range(n):
        for j in range(m):
            #ligne
            if j <= m-4:
                l = [grille[i][j+k] for k in range(4)]
                if l == p4:
                    return True
            #colonne
            #A compléter
            #diagonale croissante
            #A compléter
            #diagonale décroissante
            #A compléter
    return False

def PvP():
    grille = [[0 for _ in range(m)] for _ in range(n)]
    hauteur = [0 for _ in range(m)]
    joueur_courant = 1
    afficher_grille(grille)
    while True:
        k = int(input("Joueur "+f(joueur_courant)+", entrez votre coup :"))
        while not coup_possible(hauteur, k):
            print("coup non valide")
            k = int(input("Joueur "+f(joueur_courant)+", entrez votre coup :"))
        jouer(grille, hauteur, k, joueur_courant)
        afficher_grille(grille)
        if victoire(grille, joueur_courant):
            print("le joueur ",f(joueur_courant), " a gagné la partie")
            return
        if hauteur == [n]*m:
            print("match nul")
            return
        joueur_courant = 3 - joueur_courant

from time import *

#Fonctions enlever_coup, eval_serie, eval et IA à écrire ici

def PvIA(prof):
    grille = [[0 for _ in range(m)] for _ in range(n)]
    hauteur = [0 for _ in range(m)]
    joueur_courant = 1
    afficher_grille(grille)
    while True:
        if joueur_courant == 2:
            t = time()
            k = IA(grille, hauteur, 2, prof)
            print ("temps : ", time() - t)
        else:
            k = int(input("Joueur "+f(joueur_courant)+", entrez votre coup :"))
            while not coup_possible(hauteur, k):
                print("coup non valide")
                k = int(input("Joueur "+f(joueur_courant)+", entrez votre coup :"))
        jouer(grille, hauteur, k, joueur_courant)
        afficher_grille(grille)
        if victoire(grille, joueur_courant):
            print("le joueur",f(joueur_courant), "a gagné la partie")
            return
        if hauteur == [n]*m:
            print("match nul")
            return
        joueur_courant = 3 - joueur_courant
