-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIAMinMax_save.py
More file actions
100 lines (75 loc) · 3.41 KB
/
IAMinMax_save.py
File metadata and controls
100 lines (75 loc) · 3.41 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
from IA import *
import random
from pprint import pprint
class IAMinMax(IA):
def __init__(self,game,player):
IA.__init__(self,game,player,"IA MinMax")
def nextMove(self):
max = -100000
coupMax = 0
value = 0
profondeur = 1
#Parcours de l'ensemble des coups possibles
for move in self.getGame().getAvailableChoices():
gameCopy = self.getGame().copyBoard()
print()
print('Test du coup : ', move)
gameCopy.playMove(move,self.getPlayerType())
if gameCopy.getCurrentTurnPlayer() != self.getPlayerType():
value = self.min(gameCopy,profondeur)
else:
value = self.max(gameCopy, profondeur)
print('Fin du test. valeur : ',value)
print()
if value > max or (value == max and random.randint(0,9) >= 8):
max = value
coupMax = move
#print('coup max = ', coupMax)
#On joue le coup le mieux côté
self.playMove(coupMax)
#Fonction récursive min de l'algorithme minmax
def min(self,game,profondeur):
if profondeur == 0 or game.isOver():
return self.eval(game)
value = 0
min = 100000
max = -100000
for move in game.getAvailableChoices():
gameCopy = game.copyBoard()
gameCopy.playMove(move, gameCopy.getCurrentTurnPlayer())
if gameCopy.getCurrentTurnPlayer() != self.getPlayerType(): # L'ia va rejouer, on cherche donc le max
value = self.min(gameCopy, profondeur - 1)
if value > max or (value == max and random.randint(0, 9) >= 8):
max = value
else:
value = self.max(gameCopy,profondeur-1)
if value < min or (value == min and random.randint(0,9) >= 8):
min = value
return max if self.getGame().getCurrentTurnPlayer() != self.getPlayerType() else min
#Fonction récursive max de l'algorithme minmax
def max(self,game,profondeur):
if profondeur == 0 or game.isOver():
return self.eval(game)
value = 0
max = -100000
min = 100000
for move in game.getAvailableChoices():
gameCopy = game.copyBoard()
gameCopy.playMove(move, gameCopy.getCurrentTurnPlayer())
if gameCopy.getCurrentTurnPlayer() == self.getPlayerType(): # L'ia va rejouer, on cherche donc le max
value = self.max(gameCopy, profondeur - 1)
if value < min or (value == min and random.randint(0, 9) >= 8):
min = value
else:
value = self.min(gameCopy,profondeur-1)
if value > max or (value == max and random.randint(0,9) >= 8):
max = value
return max if self.getGame().getCurrentTurnPlayer() == self.getPlayerType() else min
#Fonction heuristique de l'algorithme minmax
def eval(self,game):
if game.isOver():
if game.getWinner() == self.getPlayerType():
return 1000 - game.getOtherScore(self.getPlayerType()) # On gagne la partie, grosse valeur
else:
return - 1000 + game.getScore(self.getPlayerType()) # On perd la partie, on doit avoir le + de carrés possibles
return int(game.getScore(self.getPlayerType()) - game.getOtherScore(self.getPlayerType()))