bonjour
j’ai un devoir à rendre il faut que j’explique chaque fonction du programme du jeu le morpion
je voudrai bien si quelqu’un peut m’expliquer brièvement chaque fonction du programme merci d’avance
voila le programme:
import tkinter as Tk
import random
#-------------------------------------------------------------------------------
initialisation des variables
taille_fenetre = 300
tableauPartie = [[0,0,0],[0,0,0],[0,0,0]] # initialisation plateau
longueur_case = taille_fenetre/len(tableauPartie)
coupsJoueur1=0
coupsJoueur2=0
tourJoueur=1
totalLigne=[0,0,0]
totalColonne=[0,0,0]
totalDiagonale=[0,0]
Mafenetre = Tk.Tk()
tex1 = Tk.Label(Mafenetre, text=‘Règles: Il y a deux joueurs; l’un joue avec la “X” l’autre avec le “O”.Les deux joueurs remplissent alternativement le tableau. Le but est de placer 3 signes identiques sur une colonne, une ligne ou une diagonale. Le jeu prend fin si le tableau est entièrement rempli.’, fg=‘Purple’)
tex1.pack()
#-------------------------------------------------------------------------------
definition des fonctions
#-------------------------------------------------------------------------------
def creerPlateauJeu():
global fen,plateau
fen=Tk.Tk()
plateau=Tk.Canvas(fen, width=taille_fenetre, height=taille_fenetre)
for i in range(len(tableauPartie)):
for j in range(len(tableauPartie[0])):
plateau.create_rectangle(ilongueur_case,jlongueur_case,(i+1)*longueur_case,(j+1)*longueur_case)
boutonExit=Tk.Button(fen,text=“Quitter”,command=fen.destroy).pack(side=Tk.BOTTOM,padx=5,pady=5)
plateau.pack(side=Tk.TOP)
def calculerTotalLigne(tableau):
global totalLigne
totalLigne=[0,0,0]
for i in range(3):
for j in range(3):
totalLigne[i]=totalLigne[i]+tableau[i][j]
return totalLigne
def calculerTotalColonne(tableau):
global totalColonne
totalColonne=[0,0,0]
for i in range(3):
for j in range(3):
totalColonne[i]=totalColonne[i]+tableau[j][i]
return totalColonne
def calculerTotalDiagonale(tableau):
global totalDiagonale
totalDiagonale=[0,0]
for i in range(3):
totalDiagonale[0]=totalDiagonale[0]+tableau[i][i]
totalDiagonale[1]=totalDiagonale[1]+tableau[i][2-i]
return totalDiagonale
def vainqueur(tableau):
global tableauPartie,totalColonne,totalDiagonale,totalLigne
gagnant=[-1,’’]
if (3 in calculerTotalLigne(tableau)) or (3 in calculerTotalColonne(tableau)) or (3 in calculerTotalDiagonale(tableau)):
gagnant=[1,’\n Le joueur 1 gagne’]
elif (15 in calculerTotalLigne(tableau)) or (15 in calculerTotalColonne(tableau)) or (15 in calculerTotalDiagonale(tableau)):
gagnant=[2,’\n Le joueur 2 gagne’]
else:
gagnant=[0,’\n La partie est nulle’]
return gagnant
def jouerValide(tableau,coup1,coup2):
jeu=True
if (coup1==5) or (coup2==5) or (vainqueur(tableau)[0]==1) or (vainqueur(tableau)[0]==2):
jeu=False
afficherVainqueur(vainqueur(tableau),plateau)
return jeu
def afficherVainqueur(joueur,plateauJeu):
texte0=Tk.Text(plateauJeu, width=25, height=5)
texte0.config(font=“sans 13”, width=25, height=5)
texte0.insert(Tk.END,joueur[1])
texte0.pack(side=Tk.TOP)
def jouer(fenetre,plateauJeu):
plateauJeu.bind("",joueur1) # un clic gauche pour le joueur1
plateauJeu.bind("",joueur2) # un clic droit pour le joueur2
fenetre.mainloop()
#-------------------------------------------------------------------------------
fonction qui g?re le joueur1
#-------------------------------------------------------------------------------
def joueur1(event):
global jeu,coupsJoueur1, coupsJoueur2, tableauPartie,totalColonne,totalDiagonale,totalLigne, tourJoueur,gagnant
r=10
couleur="blue"
x,y=event.x,event.y
if (tourJoueur==1) and (jouerValide(tableauPartie,coupsJoueur1,coupsJoueur2)):
coupsJoueur1+=1
if (0<x<100) and (0<y<100) and (tableauPartie[0][0]==0):
plateau.create_rectangle(50-r,50-r,50+r,50+r,width=5,fill=couleur)
tableauPartie[0][0]=1
if (100<x<200) and (100<y<200) and (tableauPartie[1][1]==0):
plateau.create_rectangle(150-r,150-r,150+r,150+r,width=5,fill=couleur)
tableauPartie[1][1]=1
if (200<x<300) and (200<y<300) and (tableauPartie[2][2]==0):
plateau.create_rectangle(250-r,250-r,250+r,250+r,width=5,fill=couleur)
tableauPartie[2][2]=1
if (100<x<200) and (0<y<100) and (tableauPartie[0][1]==0):
plateau.create_rectangle(150-r,50-r,150+r,50+r,width=5,fill=couleur)
tableauPartie[0][1]=1
if (200<x<300) and (0<y<100) and (tableauPartie[0][2]==0):
plateau.create_rectangle(250-r,50-r,250+r,50+r,width=5,fill=couleur)
tableauPartie[0][2]=1
if (0<x<100) and (100<y<200) and (tableauPartie[1][0]==0):
plateau.create_rectangle(50-r,150-r,50+r,150+r,width=5,fill=couleur)
tableauPartie[1][0]=1
if (200<x<300) and (100<y<200) and (tableauPartie[1][2]==0):
plateau.create_rectangle(250-r,150-r,250+r,150+r,width=5,fill=couleur)
tableauPartie[1][2]=1
if (0<x<100) and (200<y<300) and (tableauPartie[2][0]==0):
plateau.create_rectangle(50-r,250-r,50+r,250+r,width=5,fill=couleur)
tableauPartie[2][0]=1
if (100<x<200) and (200<y<300) and (tableauPartie[2][1]==0):
plateau.create_rectangle(150-r,250-r,150+r,250+r,width=5,fill=couleur)
tableauPartie[2][1]=1
else:
fen.destroy
tourJoueur=2
#------------------------------------------------------------------------------
fonction qui g?re le joueur2
def joueur2(event):
global jeu,coupsJoueur1, coupsJoueur2, tableauPartie,totalColonne,totalDiagonale,totalLigne, tourJoueur,gagnant
r=10
couleur="purple"
x,y=event.x,event.y
if (tourJoueur==2) and (jouerValide(tableauPartie,coupsJoueur1,coupsJoueur2)) :
coupsJoueur2+=1
if (0<x<100) and (0<y<100) and (tableauPartie[0][0]==0):
tableauPartie[0][0]=5
plateau.create_oval(50-r,50-r,50+r,50+r,fill=couleur)
if (100<x<200) and (100<y<200) and (tableauPartie[1][1]==0):
plateau.create_oval(150-r,150-r,150+r,150+r,fill=couleur)
tableauPartie[1][1]=5
if(200<x<300) and (200<y<300) and (tableauPartie[2][2]==0):
plateau.create_oval(250-r,250-r,250+r,250+r,fill=couleur)
tableauPartie[2][2]=5
if (100<x<200) and (0<y<100) and (tableauPartie[0][1]==0):
plateau.create_oval(150-r,50-r,150+r,50+r,fill=couleur)
tableauPartie[0][1]=5
if (200<x<300) and (0<y<100) and (tableauPartie[0][2]==0):
plateau.create_oval(250-r,50-r,250+r,50+r,fill=couleur)
tableauPartie[0][2]=5
if (0<x<100) and (100<y<200) and (tableauPartie[1][0]==0):
plateau.create_oval(50-r,150-r,50+r,150+r,fill=couleur)
tableauPartie[1][0]=5
if (200<x<300) and (100<y<200) and (tableauPartie[1][2]==0):
plateau.create_oval(250-r,150-r,250+r,150+r,fill=couleur)
tableauPartie[1][2]=5
if (0<x<100) and (200<y<300) and (tableauPartie[2][0]==0):
plateau.create_oval(50-r,250-r,50+r,250+r,fill=couleur)
tableauPartie[2][0]=5
if (100<x<200) and (200<y<300) and (tableauPartie[2][1]==0):
plateau.create_oval(150-r,250-r,150+r,250+r,fill=couleur)
tableauPartie[2][1]=5
else:
fen.destroy
tourJoueur=1
#------------------------------------------------------------------------------
programme principal
#------------------------------------------------------------------------------
creerPlateauJeu()
jouerValide(tableauPartie,coupsJoueur1,coupsJoueur2)
jouer(fen,plateau)
#------------------------------------------------------------------------------