Bonjour, je suis en Terminale S et en ISN et vu que je m'ennuyais chez moi je me suis dis que j'allais faire un exercice qu'on nous a donné pour aller "plus loin" qui consiste à créer un morpion dans une interface graphique qui détecte les clics de la souris et qui place une croix ou un rond selon le joueur, et j'ai produis ça :
[/
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
from tkinter import *
 
fen= Tk()
fen.title("Morpion")
 
i=0
a1,a2,a3,a4,a5,a6,a7,a8,a9 = 0,0,0,0,0,0,0,0,0  #Creation des variables pour les ronds
b1,b2,b3,b4,b5,b6,b7,b8,b9 = 0,0,0,0,0,0,0,0,0  #Creation des variables pour les carrés
victoire=0
 
           #fonction pour dessiner une croix
def croix(x,y):
    can.create_line(x-42,y-42,x+42,y+42, width=8, fill="blue")
    can.create_line(x+42,y-42,x-42,y+42, width=8, fill="blue")
 
            #fonction pour dessiner un rond
def rond(x,y):
    r=43 #rayon du rond
    can.create_oval(x-r,y-r,x+r,y+r, width=8, outline="red")
 
            #fonction pour recommencer
def recommence():
    global a1,a2,a3,a4,a5,a6,a7,a8,a9
    global b1,b2,b3,b4,b5,b6,b7,b8,b9
    global victoire
    global i
 
    chaine.configure(text="Afficher vainqueur")
    can.delete(ALL)
    can.create_line(100,0,100,301, width=2, fill='black')
    can.create_line(200,0,200,301, width=2, fill='black')
    can.create_line(0,100,301,100, width=2, fill='black')
    can.create_line(0,200,301,200, width=2, fill='black')
    i=0
    a1,a2,a3,a4,a5,a6,a7,a8,a9 = 0,0,0,0,0,0,0,0,0
    b1,b2,b3,b4,b5,b6,b7,b8,b9 = 0,0,0,0,0,0,0,0,0
    victoire=0
 
            #fonction qui traite tout (dessin des ronds et croix + victoire du joueur)
def morpion(event):
    global i
    global a1,a2,a3,a4,a5,a6,a7,a8,a9
    global b1,b2,b3,b4,b5,b6,b7,b8,b9
    global victoire
 
            #placement des ronds
 
    if event.x<=100 and event.y<=100 and i%2==1:
        x=50
        y=50
        if b1!=0:
            i=i
            if b1==croix(x,y):
                i=i
        else:
            a1=rond(x,y)
            a1
            i+=1
    if event.x<=200 and event.x>100 and event.y<=100 and i%2==1:
        x=150
        y=50
        if b2!=0:
            i=i
            if b2==croix(x,y):
                i=i
        else:
            a2=rond(x,y)
            a2
            i+=1
    if event.x<=300 and event.x>200 and event.y<=100 and i%2==1:
        x=250
        y=50
        if b3!=0:
            i=i
            if b3==croix(x,y):
                i=i
        else:
            a3=rond(x,y)
            a3
            i+=1
    if event.x<=100 and event.y>100 and event.y<=200 and i%2==1:
        x=50
        y=150
        if b4!=0:
            i=i
            if b4==croix(x,y):
                i=i
        else:
            a4=rond(x,y)
            a4
            i+=1
    if event.x<=200 and event.x>100 and event.y>100 and event.y<=200 and i%2==1:
        x=150
        y=150
        if b5!=0:
            i=i
            if b5==croix(x,y):
                i=i
        else:
            a5=rond(x,y)
            a5
            i+=1
    if event.x<=300 and event.x>200 and event.y>100 and event.y<=200 and i%2==1:
        x=250
        y=150
        if b6!=0:
            i=i
            if b6==croix(x,y):
                i=i
        else:
            a6=rond(x,y)
            a6
            i+=1
    if event.x<=100 and event.y>200 and event.y<=300 and i%2==1:
        x=50
        y=250
        if b7!=0:
            i=i
            if b7==croix(x,y):
                i=i
        else:
            a7=rond(x,y)
            a7
            i+=1
    if event.x<=200 and event.x>100 and event.y>200 and event.y<=300 and i%2==1:
        x=150
        y=250
        if b8!=0:
            i=i
            if b8==croix(x,y):
                i=i
        else:
            a8=rond(x,y)
            a8
            i+=1
    if event.x<=300 and event.x>200 and event.y>200 and event.y<=300 and i%2==1:
        x=250
        y=250
        if b9!=0:
            i=i
            if b9==croix(x,y):
                i=i
        else:
            a9=rond(x,y)
            a9
            i+=1
 
                    #placement des croix
 
    if event.x<=100 and event.y<=100 and i%2==0 :
        x=50
        y=50
        if a1!=0:
            i=i
            if a1==rond(x,y):
                i=i
        else:
            b1=croix(x,y)
            b1
            i+=1
    if event.x<=200 and event.x>100 and event.y<=100 and i%2==0:
        x=150
        y=50
        if a2!=0:
            i=i
            if a2==rond(x,y):
                i=i
        else:
            b2=croix(x,y)
            b2
            i+=1
    if event.x<=300 and event.x>200 and event.y<=100 and i%2==0:
        x=250
        y=50
        if a3!=0:
            i=i
            if a3==rond(x,y):
                i=i
        else:
            b3=croix(x,y)
            b3
            i+=1
    if event.x<=100 and event.y>100 and event.y<=200 and i%2==0:
        x=50
        y=150
        if a4!=0:
            i=i
            if a4==rond(x,y):
                i=i
        else:
            b4=croix(x,y)
            b4
            i+=1
    if event.x<=200 and event.x>100 and event.y>100 and event.y<=200 and i%2==0:
        x=150
        y=150
        if a5!=0:
            i=i
            if a5==rond(x,y):
                i=i
        else:
            b5=croix(x,y)
            b5
            i+=1
    if event.x<=300 and event.x>200 and event.y>100 and event.y<=200 and i%2==0:
        x=250
        y=150
        if a6!=0:
            i=i
            if a6==rond(x,y):
                i=i
        else:
            b6=croix(x,y)
            b6
            i+=1
    if event.x<=100 and event.y>200 and event.y<=300 and i%2==0:
        x=50
        y=250
        if a7!=0:
            i=i
            if a7==rond(x,y):
                i=i
        else:
            b7=croix(x,y)
            b7
            i+=1
    if event.x<=200 and event.x>100 and event.y>200 and event.y<=300 and i%2==0:
        x=150
        y=250
        if a8!=0:
            i=i
            if a8==rond(x,y):
                i=i
        else:
            b8=croix(x,y)
            b8
            i+=1
    if event.x<=300 and event.x>200 and event.y>200 and event.y<=300 and i%2==0:
        x=250
        y=250
        if a9!=0:
            i=i
            if a9==rond(x,y):
                i=i
        else:
            b9=croix(x,y)       
            b9
            i+=1
 
                #victoire du Joueur 1
 
    if b1!=0 and b2!=0 and b3!=0 and victoire==0:
        victoire=1
        chaine.configure(text="Vainqueur : Joeur 1")
    if b1!=0 and b4!=0 and b7!=0 and victoire==0:
        victoire=1
        chaine.configure(text="Vainqueur : Joeur 1")
    if b1!=0 and b5!=0 and b9!=0 and victoire==0:
        victoire=1
        chaine.configure(text="Vainqueur : Joeur 1")
    if b2!=0 and b5!=0 and b8!=0 and victoire==0:
        victoire=1
        chaine.configure(text="Vainqueur : Joeur 1")
    if b3!=0 and b6!=0 and b9!=0 and victoire==0:
        victoire=1
        chaine.configure(text="Vainqueur : Joeur 1")
    if b3!=0 and b5!=0 and b7!=0 and victoire==0:
        victoire=1
        chaine.configure(text="Vainqueur : Joeur 1")
    if b4!=0 and b5!=0 and b6!=0 and victoire==0:
        victoire=1
        chaine.configure(text="Vainqueur : Joeur 1")
    if b7!=0 and b8!=0 and b9!=0 and victoire==0:
        victoire=1
        chaine.configure(text="Vainqueur : Joeur 1")
 
                #victoire du Joueur 2
 
    if a1!=0 and a2!=0 and a3!=0 and victoire==0:
        victoire=1
        chaine.configure(text="Vainqueur : Joeur 2")
    if a1!=0 and a4!=0 and a7!=0 and victoire==0:
        victoire=1
        chaine.configure(text="Vainqueur : Joeur 2")
    if a1!=0 and a5!=0 and a9!=0 and victoire==0:
        victoire=1
        chaine.configure(text="Vainqueur : Joeur 2")
    if a2!=0 and a5!=0 and a8!=0 and victoire==0:
        victoire=1
        chaine.configure(text="Vainqueur : Joeur 2")
    if a3!=0 and a6!=0 and a9!=0 and victoire==0:
        victoire=1
        chaine.configure(text="Vainqueur : Joeur 2")
    if a3!=0 and a5!=0 and a7!=0 and victoire==0:
        victoire=1
        chaine.configure(text="Vainqueur : Joeur 2")
    if a4!=0 and a5!=0 and a6!=0 and victoire==0:
        victoire=1
        chaine.configure(text="Vainqueur : Joeur 2")
    if a7!=0 and a8!=0 and a9!=0 and victoire==0:
        victoire=1
        chaine.configure(text="Vainqueur : Joeur 2")
 
#Partie interface graphique
 
 
chaine=Label(fen, text = "Afficher vainqueur")
chaine.grid(row=0, column=0, columnspan=2)
can= Canvas(fen, bg='white', width=300, height=300)
can.grid(row=1, column=0, columnspan=2, padx = 5, pady = 5)
can.create_line(100,0,100,300, width=2, fill='black')
can.create_line(200,0,200,300, width=2, fill='black')
can.create_line(0,100,300,100, width=2, fill='black')
can.create_line(0,200,300,200, width=2, fill='black')
Button(fen, text="Quitter", command=fen.destroy).grid(row=2, column=1, padx = 3, pady = 3, sticky = S+W+E)
Button(fen, text="Recommencer", command=recommence).grid(row=2, column=0, padx = 3, pady = 3, sticky = S+W+E)
can.bind("<Button-1>", morpion)
 
fen.mainloop()
]

et j'aimerai savoir s'il n'y'a rien que je peux simplifier ou rendre plus claire ou quelque chose à changer pour réduire la taille de mon code, des remarques, vos avis dessus et des critiques que vous pourriez me donner pour que je m’améliore, merci d'avance.