Bonjour,
j'aimerais bien un peu d'aide pour un programme de puissance 4 utilisant le principe du minimax…

Voici les composantes du programme :
Nom : Choix.png
Affichages : 5835
Taille : 2,8 Ko
Choix.png
Nom : grille.png
Affichages : 11767
Taille : 35,5 Ko
grille.png
Nom : pion rouge.png
Affichages : 5668
Taille : 620 octets
pion rouge.png
Nom : pion jaune.png
Affichages : 5596
Taille : 611 octets
pion jaune.png

et le programme général...
puissance4.py
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
nmax=5
 
def Minimax(n,nmax,J,Grille,x) :
    if n!=1:
        Victoire = victoire (x,J,Grille)
        print('Victoire = ', Victoire,n)
        if Victoire == 1 :
            Victoire = 0
            return(1000)
        if Victoire == -1 :
            Victoire = 0
            return (0)
        if Grille[35]!=0 and Grille[36]!=0 and Grille[37]!=0 and Grille[38]!=0 and Grille[39]!=0 and Grille[40]!=0 and Grille[41]!=0:
            return (500)
    Valeur=[0]*7
    Ordonnée = 0
    J = J*-1
    while Ordonnée<7 :
        y=0
        z=0
        while Grille[Ordonnée+7*y] !=0 and z==0:
            y+=1
            if y==6:
                z=1
                y=0
        if z==1:
            Valeur[Ordonnée]=-1
        else :
            x=Ordonnée + 7 * y
            Grille[x]=J
            if n==nmax:
                Valeur[Ordonnée]=Analyse(x,Grille)
            else:
                n+=1
                Valeur[Ordonnée]=Minimax(n,nmax,J,Grille,x)
                n-=1
            Grille[x]=0
        Ordonnée+=1
    if J==-1:
        x=0
        while Valeur[x]==-1:
            x+=1
        ValeurB=Valeur[x]
        while x<7:
            if Valeur[x]<ValeurB and Valeur[x]!=-1 :
                ValeurB=Valeur[x]
            x+=1
    else :
        x=0
        ValeurB=0
        Best=0
        while x<7:
            if Valeur[x]>ValeurB :
                ValeurB=Valeur[x]
                Best=x
                print('best = ' ,Best,' Valeurb = ',ValeurB)
            x+=1
    print(ValeurB)
    J*=-1
    if n==1:
        print("Best = ",Best)
        return(Best)
    else:
        return(ValeurB)
 
def Analyse(x,Grille) :
    Axe = 1
    Valeur = 0
    while Axe!=5 :
        Alignement = [0]*7
        vuex = x
        if Axe == 1 :
            while vuex%7!=0 :
                vuex-=1
        if Axe == 3 :
            while vuex%7!=0 and vuex>6 :
                vuex-=8
        if Axe == 4 :
            while vuex%7!=6 and vuex>6 :
                vuex-=6
        if Axe == 2 :
            while vuex>6 :
                vuex-=7
        Aide = 0
        Alignement = [0]*7
        while Aide != 7:
            if vuex!=-1 :
                Alignement[Aide]=Grille[vuex]
                if vuex==x:
                    Position = Aide
                if Axe == 1:
                    vuex+=1
                    if vuex%7==0 :
                        vuex=-1
                if Axe == 2:
                    vuex+=7
                    if vuex>41 :
                        vuex=-1
                if Axe == 3:
                    vuex+=8
                    if vuex%7==0 or vuex>41 :
                        vuex=-1
                if Axe == 4:
                    vuex+=6
                    if vuex%7==6 or vuex>41 :
                        vuex=-1
            else :
                Alignement[Aide]=2
            Aide+=1
        Distance1 = -1
        Distance2 = -1
        Lot1=0
        Lot2=0
        Regard=Position
        while Regard >-1 :
            if Alignement[Regard]==-1 or Alignement[Regard]==2:
                Action=7
            if Alignement[Regard]==1:
                    Lot1+=1
                    Action=1
            if Alignement[Regard]==0 and Lot1==0:
                Distance1+=1
                Action=1
            if Alignement[Regard]==0 and Lot1!=0:
                Action=7
            Regard=Regard-Action
        Regard=Position
        while Regard<7 :
            if Alignement[Regard]==-1  or Alignement[Regard]==2:
                Action=7
            if Alignement[Regard]==1:
                    Lot2+=1
                    Action=1
            if Alignement[Regard]==0 and Lot2==0:
                        Distance2+=1
                        Action=1
            if Alignement[Regard]==0 and Lot2!=0:
                Action=7
            Regard = Regard+Action
        if Lot1 == 0 :
            Distance1 = 10
        if Lot2 == 0 :
            Distance2 = 10
        if Distance1==0 and Distance2 ==0 :
            Lot=Lot1+Lot2+1
            Distance=0
        if Distance1==Distance2 and Distance1!=0 :
            if Lot1>Lot2 :
                Distance=Distance1
                Lot=Lot1+1
            else :
                Distance=Distance2
                Lot=Lot2+1
        if Distance1<Distance2 :
            Distance=Distance1
            Lot=Lot1+1
        if Distance1>Distance2 :
            Distance=Distance2
            Lot=Lot2+1
        if Distance ==0:
            Valeur+=20
        if Distance==1:
            Valeur+=10
        if Distance==2:
            Valeur+=30
        if Lot==2 :
            Valeur+=10
        if Lot==3 :
            Valeur+=100
        if Lot>=4 :
            Valeur+=1000
        Regard=Position
        Distance1 = -1
        Distance2 = -1
        Lot1=0
        Lot2=0
        while Regard >-1 :
            if Alignement[Regard]==1 or Alignement[Regard]==2:
                Action=7
            if Alignement[Regard]==-1:
                    Lot1+=1
                    Action=1
            if Alignement[Regard]==0 and Lot1==0:
                Distance1+=1
                Action=1
            if Alignement[Regard]==0 and Lot1!=0:
                Action=7
            Regard=Regard-Action
        Regard=Position
        while Regard<7 :
            if Alignement[Regard]==1 or Alignement[Regard]==2 :
                Action=7
            if Alignement[Regard]==-1:
                    Lot2+=1
                    Action=1
            if Alignement[Regard]==0 and Lot2==0:
                        Distance2+=1
                        Action=1
            if Alignement[Regard]==0 and Lot2!=0:
                            Action=7
            Regard = Regard+Action
        if Lot1 == 0 :
            Distance1 = 10
        if Lot2 == 0 :
            Distance2 = 10
        if Distance1==0 and Distance2 ==0 :
            Lot=Lot1+Lot2+1
            Distance=0
        if Distance1==Distance2 and Distance1!=0 :
            if Lot1>Lot2 :
                Distance=Distance1
                Lot=Lot1+1
            else :
                Distance=Distance2
                Lot=Lot2+1
        if Distance1<Distance2 :
            Distance=Distance1
            Lot=Lot1+1
        if Distance1>Distance2 :
            Distance=Distance2
            Lot=Lot2+1
        if Lot >= 3 :
            Valeur+=500
        if Lot ==2 :
            Valeur+=50
        if Lot == 1 :
            Valeur +=5
        Axe+=1
    if x%7 == 3 :
        Valeur+=3
    if x%7 == 2 or x%7 == 4 :
        Valeur+=2
    if x%7 == 1 or x%7 == 5 :
        Valeur+=1
    return(Valeur)
 
 
 
def Clic(x) :
    #Répétition des touches
        pygame.key.set_repeat(673, 576)
        for event in pygame.event.get(): #Attente des événements
            if event.type == QUIT:
                Victoire = 3
            if event.type == MOUSEBUTTONDOWN:
              if event.button == 1:   #Si clic gauche
                  #On donne à x la valeur
                  x = event.pos[0]
                  y = 0
                  if x >= 577 :
                    y = 1
                    x = 672
                  if x >= 481 :
                    if y == 0 :
                        x = 576
                        y = 1
                  if x >= 385 :
                    if y == 0 :
                        x = 480
                        y = 1
                  if x >= 289 :
                    if y == 0 :
                        x = 384
                        y = 1
                  if x >= 193 :
                    if y == 0 :
                        x = 288
                        y = 1
                  if x >= 97 :
                    if y == 0 :
                        x = 192
                        y = 1
                  if y == 0 :
                        x = 96
                        y = 1
                  x = int(x/96)
        return(x)
 
 
def victoire(x,Tour,Grille) :
    Victoire=0
    #Réussite verticale
    if x >= 21 :
         if Grille[x-7]==Tour and Grille[x-14]==Tour and Grille[x-21]==Tour:
                    Victoire = Tour
    #Réussite horizontale
    vuex = 0
    if x%7!=0 :
        if Grille[x-1] == Tour:
            vuex = vuex + 1
            if x%7!=1 :
                if Grille[x-2] == Tour:
                    vuex = vuex + 1
                    if x%7!=2 :
                        if Grille[x-3] == Tour:
                            vuex = vuex + 1
    if x%7!=6 :
        if Grille[x+1] == Tour:
            vuex = vuex + 1
            if x%7!=5 :
                if Grille[x+2] == Tour:
                    vuex = vuex + 1
                    if x%7!=4 :
                        if Grille[x+3] == Tour:
                            vuex = vuex + 1
    if vuex >= 3 :
        Victoire = Tour
    #Réussite Diagonale Gauche
    vuex = 0
    if x%7!=0 and x>6:
        if Grille[x-8] == Tour:
            vuex = vuex + 1
            if x%7!=1 and x>13:
                if Grille[x-16] == Tour:
                    vuex = vuex + 1
                    if x%7!=2 and x>20:
                        if Grille[x-24] == Tour:
                            vuex = vuex + 1
    if x%7!=6 and x<35:
        if Grille[x+8] == Tour:
            vuex = vuex + 1
            if x%7!=5 and x<28:
                if Grille[x+16] == Tour:
                    vuex = vuex + 1
                    if x%7!=4 and x<21:
                        if Grille[x+24] == Tour:
                            vuex = vuex + 1
    if vuex >= 3 :
        Victoire = Tour
    #Réussite Diagonale Droite
    vuex = 0
    if x%7!=6 and x>6:
        if Grille[x-6] == Tour:
            vuex = vuex + 1
            if x%7!=5 and x>13:
                if Grille[x-12] == Tour:
                    vuex = vuex + 1
                    if x%7!=4 and x>20:
                        if Grille[x-18] == Tour:
                            vuex = vuex + 1
    if x%7!=0 and x<35:
        if Grille[x+6]== Tour:
            vuex = vuex + 1
            if x%7!=1 and x<28:
                if Grille[x+12] == Tour:
                    vuex = vuex + 1
                    if x%7!=2 and x<21:
                        if Grille[x+18] == Tour:
                            vuex = vuex + 1
    if vuex >= 3 :
        Victoire = Tour
    return(Victoire)
 
import pygame
from pygame.locals import *
pygame.init()
fenetre = pygame.display.set_mode((673, 576))
fond = pygame.image.load("F:/Choix.png").convert()
fenetre.blit(fond, (0,0))
font=pygame.font.Font(None, 24)
text = font.render("Voulez-vous jouer en Rouge ?", 1, (255,0,0))
fenetre.blit(text,(50,270))
text = font.render("Voulez-vous jouer en Jaune ?", 1, (255,255,0))
fenetre.blit(text,(390,270))
pygame.display.flip()
x = 0
while x == 0 :
    pygame.key.set_repeat(673, 576)
    for event in pygame.event.get():
            if event.type == QUIT:
                Victoire = 3
            if event.type == MOUSEBUTTONDOWN:
              if event.button == 1:
                  x = event.pos[0]
couleurjoueur = 1
if x >=337 :
    couleurjoueur = 2
fond = pygame.image.load("F:/Choix.png").convert()
fenetre.blit(fond, (0,0))
font=pygame.font.Font(None, 24)
text = font.render("Voulez-vous jouer en premier ?", 1, (255,0,0))
fenetre.blit(text,(50,270))
text = font.render("Voulez-vous jouer en second ?", 1, (255,255,0))
fenetre.blit(text,(390,270))
pygame.display.flip()
x = 0
while x == 0 :
    pygame.key.set_repeat(673, 576)
    for event in pygame.event.get():
            if event.type == QUIT:
                Victoire = 3
            if event.type == MOUSEBUTTONDOWN:
              if event.button == 1:
                  x = event.pos[0]
Tour = -1
if x >=337 :
    Tour = 1
grille = pygame.image.load("F:/grille.png").convert_alpha()
grille.set_colorkey((255,255,255))
rouge = pygame.image.load("F:/pion rouge.png").convert()
rouge.set_colorkey((255,255,255))
jaune = pygame.image.load("F:/pion jaune.png").convert()
jaune.set_colorkey((255,255,255))
x = 0
fenetre.blit(grille, (x, 0))
pygame.display.flip()
Victoire = 0
Cases = 0
x = 0
n=1
Grille = [0]*42
n=0
while Victoire == 0 and Cases != 42:
    if Tour == -1 :
        x = 0
        while x == 0 :
            x = 0
            while x ==0 :
                x = Clic(x)
                if Grille[x+34] != 0 :
                    x = 0
        y = 0
        while Grille[7*y+x-1]!= 0 :
            y = y+1
        print(x,y)
        if couleurjoueur == 1 and Tour == -1 :
           fenetre.blit(rouge,(-87+96*x,488-96*y))
        if couleurjoueur == 1 and Tour == 1 :
            fenetre.blit(jaune,(-87+96*x,488-96*y))
        if couleurjoueur == 2 and Tour == -1 :
            fenetre.blit(jaune,(-87+96*x,488-96*y))
        if couleurjoueur == 2 and Tour == 1 :
            fenetre.blit(rouge,(-87+96*x,488-96*y))
        pygame.display.flip()
        x=7*y+x-1
        Grille[x] = -1
        Cases = Cases + 1
        Victoire = victoire(x,Tour,Grille)
        Tour = 1
    if Tour == 1 and Victoire != -1 and Cases != 42:
    #Choix de l'Ordi
        J = -1
        n=1
        x=0
        x = Minimax(n,nmax,J,Grille,x)
        y = 0
        print(x)
        while Grille[x+7*y]!=0 :
            y+=1
            print(x, y)
        Grille[7*y+x]=1
        Cases = Cases + 1
        x=x+1
        if couleurjoueur == 1 and Tour == -1 :
            fenetre.blit(rouge,(-87+96*x,488-96*y))
        if couleurjoueur == 1 and Tour == 1 :
            fenetre.blit(jaune,(-87+96*x,488-96*y))
        if couleurjoueur == 2 and Tour == -1 :
            fenetre.blit(jaune,(-87+96*x,488-96*y))
        if couleurjoueur == 2 and Tour == 1 :
            fenetre.blit(rouge,(-87+96*x,488-96*y))
        pygame.display.flip()
        x = 7*y+x-1
        Victoire = victoire (x,Tour,Grille)
        Tour = -1
 
if Victoire == 1 :
    print ("Victoire de l'Ordi")
if Victoire == 0 :
    print ("Match nul...")
if Victoire == -1 :
    print("Victoire du Joueur")
pygame.quit()
quit()
Pour modifier la profondeur maximale de l'arbre du minimax, il suffit de choisir la valeur nmax à la ligne 1.

Actuellement, le programme peut battre des humains, mais il y a deux problèmes :
- Parfois, alors qu'il pourrait gagner, il ne joue pas le coup décisif (pour une profondeur maximale de 1) : il y aurait donc un problème dans la fonction d'évaluation (Analyse()), mais où ?
- Quand l'Ordi se rend compte qu'il va perdre, il joue la première colonne par défaut, même si elle est pleine, amenant à un crash (enfin, il me semble que c'est ça….)

Merci de me dire où se trouvent les failles de ces deux problèmes !
Majoran