# Le gasp

from tkinter import *
from random import randrange

haut = 4  # hauteur du tableau
larg = haut  # largeur du tableau
cote = 100  # côté d'une cellule
vivant = 1
mort = 0


# Créer les matrices
cell = [[0 for row in range(haut)] for col in range(larg)]
etat = [[mort for row in range(haut)] for col in range(larg)]
pion = [[mort for row in range(haut)] for col in range(larg)]


# Données initiales
def init():
    for y in range(haut):
        for x in range(larg):
            etat[x][y] = vivant
            cell[x][y] = canvas.create_rectangle((x*cote, y*cote, (x+1)*cote, (y+1)*cote), outline="black", fill="tan1")
            pion[x][y] = canvas.create_oval((x*cote+cote/20, y*cote+cote/20, (x+1)*cote-cote/20, (y+1)*cote-cote/20), outline="black", fill="black", width=4)


def retourner(x,y):
    global coul
    etat[x][y] = (etat[x][y]+1)%2  # on inverse l'état : mort <-> vivant
    if etat[x][y]==mort:
        coul = "white"
    else:
        coul = "black"
    canvas.itemconfig(pion[x][y], fill=coul)


# Modifier la tableau avec la souris
def pointeur(event):
    global coul
    x, y = event.x//cote, event.y//cote
    if x>0: # retourne les cases à gauche
        retourner(x-1,y)
        if y>0:
            retourner(x-1,y-1)
        if y<haut-1:
            retourner(x-1,y+1)
    if x<larg-1: # retourne les cases à droite
        retourner(x+1,y)
        if y>0:
            retourner(x+1,y-1)
        if y<haut-1:
            retourner(x+1,y+1)
    if y>0:  # retourne la case au-dessus
        retourner(x,y-1)
    if y<haut-1:  # retourne la case au-dessus
        retourner(x,y+1)
    

# Lancement du programme
fenetre = Tk()
fenetre.title("Le gasp")
canvas = Canvas(fenetre, width=cote*larg, height=cote*haut, highlightthickness=0)
canvas.bind("<Button-1>", pointeur)
canvas.pack()
bou1 = Button(fenetre,text='Quitter', width=8, command=fenetre.destroy)
bou1.pack(side=RIGHT)
bou1 = Button(fenetre,text='Recommencer', width=12, command=init)
bou1.pack(side=LEFT)
init()
fenetre.mainloop()
