from tkinter import * from random import randrange def jeu1(): ################### PARAMETRES ################### lc = 100 # largeur cellule eh = 50 # ecart haut eg = 20 # ecart gauche eb = 20 # ecart bas COLOR = ["#DEB887", # fond d'une case jouée 0 "#7E3300" , # trait actif 1 "#CD853F", # trait passif 2 "black", # quadrillage 3 "#FDF5E6"] # fond 4 x = 6 # dimension de l'echiquier y = 6 # et = 10 # epaisseur des traits ################################################################################## coups = 0 coup = "coup" +str(coups) fin = 0 dep = 0 Xp = -1 Yp = 0 X = 0 Y = 0 S = [1,0,0,0] jeu = [] valeur = 1 global Xp,Yp,fin,dep,coup,jeu,coups,S,valeur,X,Y for i in range(y+1): l = [] for j in range(x+1): l.append(0) jeu.append(l) jeu[0][0] = 1 je = randrange(2)+1 global je j = "Au tour du joueur : "+ str(je) fen1 = Toplevel() fen1.title("ligne noir") can=Canvas(fen1, width=eg+x*lc+eg, height=eh+y*lc+eb,bg=COLOR[4]) can.pack() for i in range(y+1): can.create_line(eg,eh+i*lc,eg+x*lc,eh+i*lc,fill=COLOR[3],width=et ,capstyle='projecting') for i in range(x+1): can.create_line(eg+i*lc,eh,eg+i*lc,eh+y*lc,fill=COLOR[3],width=et,capstyle='projecting') can.create_rectangle(eg+lc*(x-1)+((et+1)//2),eh+(y-1)*lc+((et+1)//2),eg+lc*x+et,eh+y*lc+et, width= 0, fill = COLOR[4]) joue = Label(can,text = j, font=('Courier New', '17', 'bold'),bg = COLOR[4]) joue.place(x=20,y=10) def croix(Xa,Ya): can.create_rectangle(eg+lc*Xa+((et+1)//2),eh+Ya*lc+((et+1)//2),eg+lc*(Xa+1)-((et+1)//2),eh+(Ya+1)*lc-((et+1)//2), width= 0, fill = COLOR[0]) can.create_line(eg+Xa*lc+((et+1)//2),eh+Ya*lc+(lc//2),eg+(Xa+1)*lc-((et+1)//2),eh+Ya*lc+(lc//2), fill=COLOR[2], width=et) can.create_line(eg+Xa*lc+(lc//2),eh+Ya*lc+((et+1)//2),eg+Xa*lc+(lc//2),eh+(Ya+1)*lc-((et+1)//2), fill=COLOR[2], width=et) def cercle1(Xa,Ya): can.create_rectangle(eg+lc*Xa+((et+1)//2),eh+Ya*lc+((et+1)//2),eg+lc*(Xa+1)-((et+1)//2),eh+(Ya+1)*lc-((et+1)//2), width= 0, fill = COLOR[0]) can.create_arc(eg+lc*(Xa-1)+(lc//2),eh+lc*Ya+(lc//2)-1,eg+lc*(Xa+1)-(lc//2),eh+lc*(Ya+2)-(lc//2), outline= COLOR[2], width=et) can.create_arc(eg+lc*(Xa+1)+(lc//2),eh+lc*(Ya-1)+(lc//2),eg+lc*(Xa+1)-(lc//2)-1,eh+lc*(Ya+1)-(lc//2), outline= COLOR[2], width=et, extent= 90, start =180 ) def cercle2(Xa,Ya): can.create_rectangle(eg+lc*Xa+((et+1)//2),eh+Ya*lc+((et+1)//2),eg+lc*(Xa+1)-((et+1)//2),eh+(Ya+1)*lc-((et+1)//2), width= 0, fill = COLOR[0]) can.create_arc(eg+lc*(Xa-1)+(lc//2),eh+lc*(Ya-1)+(lc//2)-1,eg+lc*(Xa+1)-(lc//2),eh+lc*(Ya+1)-(lc//2), outline= COLOR[2], width=et, extent= 90, start =270) can.create_arc(eg+lc*(Xa+1)+(lc//2),eh+lc*Ya+(lc//2)-1,eg+lc*(Xa+1)-(lc//2)-1,eh+lc*(Ya+2)-(lc//2), outline= COLOR[2], width=et, extent= 90, start =90 ) def trait(Xap,Yap,Xa,Ya): can.delete(coup) while jeu[Ya][Xa] != 0: if jeu[Ya][Xa] == 1: if (Xap == Xa-1 and Yap == Ya) or (Xap == Xa+1 and Yap == Ya): can.create_line(eg+Xa*lc+((et+1)//2),eh+Ya*lc+(lc//2),eg+(Xa+1)*lc-((et+1)//2),eh+Ya*lc+(lc//2), fill=COLOR[1], width=et,tag = coup) if Xap == Xa-1: Xap = Xa Yap = Ya Xa = Xa+1 Ya = Ya else: Xap = Xa Yap = Ya Xa = Xa-1 Ya = Ya else: can.create_line(eg+Xa*lc+(lc//2),eh+Ya*lc+((et+1)//2),eg+Xa*lc+(lc//2),eh+(Ya+1)*lc-((et+1)//2), fill=COLOR[1], width=et,tag = coup) if Yap == Ya-1: Xap = Xa Yap = Ya Xa = Xa Ya = Ya+1 else: Xap = Xa Yap = Ya Xa = Xa Ya = Ya-1 elif jeu[Ya][Xa] == 2: if (Xap == Xa+1 and Yap == Ya) or (Xap == Xa and Yap == Ya-1): can.create_arc(eg+lc*(Xa+1)+(lc//2),eh+lc*(Ya-1)+(lc//2),eg+lc*(Xa+1)-(lc//2)-1,eh+lc*(Ya+1)-(lc//2), outline=COLOR[1], width=et, extent= 90, start =180 ,tag = coup) if Xap == Xa+1: Xap = Xa Yap = Ya Xa = Xa Ya = Ya-1 else: Xap = Xa Yap = Ya Xa = Xa+1 Ya = Ya else: can.create_arc(eg+lc*(Xa-1)+(lc//2),eh+lc*Ya+(lc//2)-1,eg+lc*(Xa+1)-(lc//2),eh+lc*(Ya+2)-(lc//2), outline=COLOR[1], width=et,tag = coup) if Yap == Ya+1: Xap = Xa Yap = Ya Xa = Xa-1 Ya = Ya else: Xap = Xa Yap = Ya Xa = Xa Ya = Ya+1 if jeu[Ya][Xa] == 3: if (Xap == Xa+1 and Yap == Ya) or (Xap == Xa and Yap == Ya+1): can.create_arc(eg+lc*(Xa+1)+(lc//2),eh+lc*Ya+(lc//2)-1,eg+lc*(Xa+1)-(lc//2)-1,eh+lc*(Ya+2)-(lc//2), outline=COLOR[1], width=et, extent= 90, start =90 ,tag = coup) if Xap == Xa+1: Xap = Xa Yap = Ya Xa = Xa Ya = Ya+1 else: Xap = Xa Yap = Ya Xa = Xa+1 Ya = Ya else: can.create_arc(eg+lc*(Xa-1)+(lc//2),eh+lc*(Ya-1)+(lc//2)-1,eg+lc*(Xa+1)-(lc//2),eh+lc*(Ya+1)-(lc//2), outline=COLOR[1], width=et, extent= 90, start =270,tag = coup) if Yap == Ya-1: Xap = Xa Yap = Ya Xa = Xa-1 Ya = Ya else: Xap = Xa Yap = Ya Xa = Xa Ya = Ya-1 for i in range(y+1): can.create_line(eg,eh+i*lc,eg+x*lc,eh+i*lc,fill=COLOR[3],width=et,capstyle='projecting') for i in range(x+1): can.create_line(eg+i*lc,eh,eg+i*lc,eh+y*lc,fill=COLOR[3],width=et,capstyle='projecting') can.create_rectangle(eg+lc*(x-1)+((et+1)//2),eh+(y-1)*lc+((et+1)//2),eg+lc*x+et,eh+y*lc+et, width= 0, fill = COLOR[4]) Suiv = [Xa,Ya,Xap,Yap] return Suiv def depart(ev=None): global Xp,Yp if Xp - Yp == -1: Xp = 0 Yp = -1 else: Xp = -1 Yp = 0 def change(ev=None): global jeu,S,valeur if fin == 0: valeur = 0 if dep == 0: depart() else: if jeu[Y][X] == 1: cercle1(X,Y) jeu[Y][X] = 2 elif jeu[Y][X] == 2: cercle2(X,Y) jeu[Y][X] = 3 elif jeu[Y][X] == 3: croix(X,Y) jeu[Y][X] = 1 S = trait(Xp,Yp,X,Y) def valide(ev=None): global Xp,Yp,X,Y,fin,coups,coup,j,je,dep,jeu,valeur if fin == 0: if valeur == 0: valeur = 1 if dep == 0: dep = 1 else: if je == 1: je = 2 else: je = 1 coups +=1 coup = "coup" +str(coups) Xp = S[2] Yp = S[3] X = S[0] Y = S[1] j = "Au tour du joueur : "+ str(je) joue.configure(text = j) if X < 0 or Y < 0 or X > x-1 or Y > y-1 or (X == x-1 and Y == y-1): j = "Le joueur "+ str(je)+" a gagné !" joue.configure(text = j) fin = 1 else: can.create_rectangle(eg+lc*X+((et+1)//2),eh+Y*lc+((et+1)//2),eg+lc*(X+1)-((et+1)//2),eh+(Y+1)*lc-((et+1)//2), width= 0, fill = COLOR[0]) jeu[Y][X] = 1 else: fen1.destroy() return je croix(X,Y) change() change() can.bind_all('',change) can.bind_all('',valide) mainloop()