-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminmax.py
More file actions
342 lines (308 loc) · 11.5 KB
/
minmax.py
File metadata and controls
342 lines (308 loc) · 11.5 KB
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
#Victor Mouradian, Wassim Serradj, Prosper Sebille, Matthieu Sarfati
import copy
import math
prof = 5
tailleM = 15
tour = 1
def Actions(Matrice, tailleM,tour):
valeur = []
if(tour >3 or tour == 2):
for i in range(tailleM):
for j in range(tailleM):
if Matrice[i][j]==0:
valeur.append([i,j])
if(tour == 1):
valeur.append([7,7])
if(tour == 3):
for i in range(tailleM):
for j in range(tailleM):
if Matrice[i][j]==0 and ((i in [ e for e in range(3,10)] or j in [e for e in range(3,10)] )== False):
valeur.append([i,j])
return valeur
def columns(Matrice, tailleM):
cols =[]
col=[]
for i in range(0,tailleM):
for j in range(0,tailleM):
col.append([Matrice[j][i],j,i])
cols.append(copy.deepcopy(col))
col.clear()
return cols
def diagonals(Matrice, tailleM):
diags =[]
diag = []
for i in range(0,tailleM):
diag.append([Matrice[i][i],i,i])
diags.append(copy.deepcopy(diag))
diag.clear()
j= 0
for i in range(tailleM-1,-1,-1):
diag.append([Matrice[i][j],i,j])
j +=1
diags.append(copy.deepcopy(diag))
for offset in range(tailleM):
diag = [ [row[i+offset],i,i+offset] for i,row in enumerate(Matrice) if 0 <= i+offset < len(row)]
diags.append(copy.deepcopy(diag))
diag.clear()
diag.clear()
return diags
def ThreatSearch(Matrice, tailleM, jou):
diags = diagonals(Matrice, tailleM)
for i in range(tailleM):
count = 0
for j in range(tailleM):
if Matrice[i][j] == jou:
count +=1
if Matrice[i][j] !=jou and Matrice[i][j] !=0:
count= 0
if Matrice[i][j] ==0:
if(count == 4):
return [1000000000,i,j]
if(count == 3):
return [1000000,i,j]
count = 0
for elem in diags:
count=0
for x in elem:
if x[0] == jou:
count +=1
if x[0] !=jou and x[0] !=0:
count= 0
if x[0] ==0:
if(count == 4):
return [1000000000,x[1],x[2]]
if(count == 3):
return [1000000,x[1],x[2]]
count=0
for i in range(15):
count = 0
for j in range(15):
if(Matrice[j][i] == jou):
count +=1
if Matrice[j][i] !=jou and Matrice[j][i] !=0:
count= 0
if(Matrice[j][i] ==0):
if(count == 4):
return [1000000000,j,i]
if(count == 3):
return [1000000,j,i]
count =0
return [-9999999,-1,-1]
def Result(Matrice, i,j,jou):
Matrice[i][j]=jou
return Matrice
def Utility(Matrice, tailleM,jou,ligne,colonne):
points = 0
index = -4
while(index <= 0):
adversaire = False
coequipier = 0
for i in range((colonne+index),(colonne+index+5)):
if((colonne+index)>=0 and (colonne+index+5)<=14):
if(Matrice[ligne][i] !=jou and Matrice[ligne][i]!= 0):
adversaire = True
if(Matrice[ligne][i] == jou):
coequipier += 1
adversaire = False
if(adversaire == False and (colonne+index)>=0 and (colonne+index+5)<=14):
points += 2**coequipier
if (adversaire == True and (ligne + index) >= 0 and (ligne + index + 5) <= 14):
points -= 8
index += 1
index = -4
while(index <= 0):
adversaire = False
coequipier = 0
for i in range((ligne+index),(ligne+index+5)):
if((ligne+index)>=0 and (ligne+index+5)<=14):
if(Matrice[i][colonne] !=jou and Matrice[i][colonne]!= 0):
adversaire = True
if(Matrice[i][colonne] == jou):
coequipier += 1
adversaire = False
if(adversaire == False and (ligne+index)>=0 and (ligne+index+5)<=14):
points += 2**coequipier
if (adversaire == True and (ligne + index) >= 0 and (ligne + index + 5) <= 14):
points -= 8
index+=1
index = 0
while(index < 5):
adversaire = False
coequipier = 0
for i in range(-4 + index,1+index):
if((ligne+i)>=0 and (colonne+i)>=0 and (ligne+i)<=14 and (colonne+i)<=14):
if(Matrice[ligne +i][colonne+i] !=jou and Matrice[ligne+i][colonne +i]!= 0):
adversaire = True
if(Matrice[ligne+i][colonne+i] == jou):
coequipier += 1
if(adversaire == False and (ligne+i)>=0 and (colonne+i)>=0 and (ligne+i)<=14 and (colonne+i)<=14):
points += 2**coequipier
if (adversaire == True and (ligne + index) >= 0 and (ligne + index + 5) <= 14):
points -= 8
index+=1
if(TerminalTest(Matrice)==jou):
points += 700
index = -4
while (index <= 0):
defenseur = False
nb_adversaires = 0
for i in range((colonne + index), (colonne + index + 5)):
if ((colonne + index) >= 0 and (colonne + index + 5) <= 14):
if (Matrice[ligne][i] != jou and Matrice[ligne][i] != 0):
nb_adversaires += 1
if (Matrice[ligne][i] == jou):
defenseur = True
if (defenseur == False and nb_adversaires == 3):
points += 50
if (defenseur == False and nb_adversaires == 4):
points += 1000
index += 1
index = -4
while (index <= 0):
defenseur = False
nb_adversaires = 0
for i in range((ligne + index), (ligne + index + 5)):
if ((ligne + index) >= 0 and (ligne + index + 5) <= 14):
if (Matrice[i][colonne] != jou and Matrice[i][colonne] != 0):
nb_adversaires += 1
if (Matrice[i][colonne] == jou):
defenseur = True
if (defenseur == False and nb_adversaires == 3):
points += 50
if (defenseur == False and nb_adversaires == 4):
points += 1000
index += 1
index = 0
while(index < 5):
defenseur = False
nb_adversaires = 0
for i in range(-4 + index,1+index):
if((ligne+i)>=0 and (colonne+i)>=0 and (ligne+i)<=14 and (colonne+i)<=14):
if(Matrice[ligne +i][colonne+i] !=jou and Matrice[ligne+i][colonne +i]!= 0):
nb_adversaires +=1
if(Matrice[ligne+i][colonne+i] == jou):
defenseur = True
if (defenseur == False and nb_adversaires == 3):
points += 50
if (defenseur == False and nb_adversaires == 4):
points += 1000
index+=1
return points
def TerminalTest(Matrice):
tailleM = 15
diags = diagonals(Matrice, tailleM)
for jou in [1,2]:
for elem in diags:
count = 0
for x in elem:
if(x == jou):
count +=1
if(x !=jou):
count =0
if(count == 5):
return jou
for i in range(15):
count = 0
for j in range(15):
if(Matrice[j][i] == jou):
count +=1
if(Matrice[j][i] !=jou):
count =0
if(count == 5):
return jou
for elem in Matrice:
count = 0
for x in elem:
if(x == jou):
count +=1
if(x !=jou):
count =0
if(count == 5):
return jou
verif = 0
for i in range(tailleM):
for j in range(tailleM):
if(Matrice[i][j] == 0):
verif += 1
if(verif == 0):return -1
return 0
def MaxValue(Matrice, tailleM,jou,p, profondeur, Alpha, Beta,tour,i,j):
if(TerminalTest(Matrice)==jou or profondeur == prof):
return Utility(Matrice, tailleM,jou[p],i,j)
res = Actions(Matrice, tailleM,tour)
v = -math.inf
for x in res:
v= max(v,MinValue(Result(copy.deepcopy(Matrice), x[0],x[1],jou[p]),tailleM,jou,(p+1)%2,copy.deepcopy(profondeur+1), Alpha, Beta,copy.deepcopy(tour+1),x[1],x[0]))
if(v > Beta):
return v
Alpha = max(Alpha,v)
return v
def MinValue(Matrice, tailleM, jou,p, profondeur, Alpha, Beta, tour,i,j):
if(TerminalTest(Matrice)==jou or profondeur == prof ):
return Utility(Matrice, tailleM,jou[p],i,j)
res = Actions(Matrice, tailleM,tour)
v = math.inf
for x in res:
v = min(v,MaxValue(Result(copy.deepcopy(Matrice), x[0],x[1],jou[p]),tailleM,jou,(p+1)%2,copy.deepcopy(profondeur+1), Alpha, Beta,copy.deepcopy(tour+1),x[1],x[0]))
if(v < Alpha):
return v
Beta = min(Beta,v)
return v
def takefirst(elem):
return elem[0]
def MiniMaxDecision(Matrice, tailleM,var, profondeur,tour):
res = Actions(Matrice, tailleM,tour)
point =[]
jou=[1,2]
p=var
point.append(ThreatSearch(Matrice, tailleM, 2))
for x in res:
profondeur =0
mm = MinValue(Result(copy.deepcopy(Matrice), x[0],x[1],jou[p]),tailleM,jou,(p+1)%2, profondeur,math.inf,-math.inf,copy.deepcopy(tour+1),x[1],x[0])
point.append([mm,x[0],x[1]])
point = sorted(point, key=lambda l:l[0], reverse = True)
print("POSSSIBILITES : ")
print(point)
return point[0]
def Main():
"""Matrice = [[0 for col in range(15)] for row in range(15)]
Matrice[4][0] = 1
Matrice[4][2] = 1
Matrice[4][3] = 2
Matrice[4][4] = 1
Matrice[4][5] = 2
print(Utility(Matrice, 15, 1, 5, 4))"""
Matrice = [[0 for col in range(tailleM)] for row in range(tailleM)]
result = 0
tour = 1
while result==0:
temp = MiniMaxDecision(copy.deepcopy(Matrice), tailleM,0,0,copy.deepcopy(tour))
Matrice = Result(Matrice,temp[1],temp[2],1)
printMat(Matrice, tailleM)
result = TerminalTest(Matrice)
tour +=1
userinput(Matrice, tailleM)
printMat(Matrice, tailleM)
result = TerminalTest(Matrice)
tour +=1
printMat(Matrice, tailleM)
def userinput(Matrice,tailleM):
x = input("Coordonées X de votre choix ?")
y = input("Coordonées Y de votre choix ?")
while(Matrice[int(x)][int(y)]!=0):
print("CASE DEJA PRISE ")
printMat(Matrice, tailleM)
x = input("Coordonées X de votre choix ?")
y = input("Coordonées Y de votre choix ?")
Result(Matrice,int(x),int(y),2)
def printMat(Matrice, tailleM):
lettre = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O']
print("+------------------------------------------------+")
print(" 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14")
for i in range(tailleM):
if i<10:
print(lettre[i]+" "+str([Matrice[i][j] for j in range(tailleM)]))
if i >=10:
print(lettre[i]+" "+str([Matrice[i][j] for j in range(tailleM)]))
print("+------------------------------------------------+")
Main()