Ajout du travail de la séance.
This commit is contained in:
parent
7605547ff4
commit
c834b7f4ac
80
README.md
80
README.md
@ -1,8 +1,9 @@
|
|||||||
# BUT3 Jeu
|
# BUT3 Jeu
|
||||||
|
|
||||||
Groupe : Dylan LANDRIN, Killian SCHIED
|
##### Groupe : Dylan LANDRIN, Killian SCHIED
|
||||||
|
|
||||||
Au cours de cette séance, nous avons implémenté 3 algorythmes en python:
|
## Séance du 12/09/24
|
||||||
|
Au cours de cette séance, nous avons implémenté 3 algorithmes en python:
|
||||||
|
|
||||||
### exploreMax
|
### exploreMax
|
||||||
Le but d'explore max est de déterminer si le joueur 1 peux faire un coup qui mettrais en echec joueur 2.
|
Le but d'explore max est de déterminer si le joueur 1 peux faire un coup qui mettrais en echec joueur 2.
|
||||||
@ -12,3 +13,78 @@ Le but de Min est l'inverse, il voit s'il peut mettre joueur 1 en échec
|
|||||||
|
|
||||||
### miniMax
|
### miniMax
|
||||||
miniMax appelle les explorations
|
miniMax appelle les explorations
|
||||||
|
|
||||||
|
## Séance du 17/09/24
|
||||||
|
Au cours de cette séance, nous avons apportés des modifications dans les fonctions utilisées dans l'algorithme miniMax() ainsi qu'un affichage de l'objet Game.
|
||||||
|
|
||||||
|
### exploreMax() et exploreMin()
|
||||||
|
Des optimisations ont été apportés dans ces fonctions afin de stopper la recherche lorsque une solution a été trouvée.
|
||||||
|
|
||||||
|
Exemple :
|
||||||
|
#### Avant
|
||||||
|
Test avec 5 allumettes :
|
||||||
|
```
|
||||||
|
$ python3 game.py
|
||||||
|
|
||||||
|
-----------------------
|
||||||
|
Game and algorithm data
|
||||||
|
-----------------------
|
||||||
|
== Game data ==
|
||||||
|
Remaining matches : 5
|
||||||
|
Game state : 0
|
||||||
|
Actual player : 1
|
||||||
|
== Algorithm data ==
|
||||||
|
exploreMax calls = 17
|
||||||
|
exploreMin calls = 19
|
||||||
|
First player winning ? = False
|
||||||
|
```
|
||||||
|
|
||||||
|
Test avec 14 allumettes :
|
||||||
|
|
||||||
|
```
|
||||||
|
-----------------------
|
||||||
|
Game and algorithm data
|
||||||
|
-----------------------
|
||||||
|
== Game data ==
|
||||||
|
Remaining matches : 14
|
||||||
|
Game state : 0
|
||||||
|
Actual player : 1
|
||||||
|
== Algorithm data ==
|
||||||
|
exploreMax calls = 916
|
||||||
|
exploreMin calls = 913
|
||||||
|
First player winning ? = True
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Après
|
||||||
|
Test avec 5 allumettes :
|
||||||
|
```
|
||||||
|
$ python3 game_optimized.py
|
||||||
|
|
||||||
|
-----------------------
|
||||||
|
Game and algorithm data
|
||||||
|
-----------------------
|
||||||
|
== Game data ==
|
||||||
|
Remaining matches : 5
|
||||||
|
Game state : 0
|
||||||
|
Actual player : 1
|
||||||
|
== Algorithm data ==
|
||||||
|
exploreMax calls = 3
|
||||||
|
exploreMin calls = 3
|
||||||
|
First player winning ? = False
|
||||||
|
```
|
||||||
|
|
||||||
|
Test avec 14 allumettes :
|
||||||
|
|
||||||
|
```
|
||||||
|
-----------------------
|
||||||
|
Game and algorithm data
|
||||||
|
-----------------------
|
||||||
|
== Game data ==
|
||||||
|
Remaining matches : 14
|
||||||
|
Game state : 0
|
||||||
|
Actual player : 1
|
||||||
|
== Algorithm data ==
|
||||||
|
exploreMax calls = 8
|
||||||
|
exploreMin calls = 7
|
||||||
|
First player winning ? = True
|
||||||
|
```
|
66
game.py
66
game.py
@ -1,19 +1,47 @@
|
|||||||
class Game(int):
|
class Game(int):
|
||||||
|
|
||||||
def __init__(self, start):
|
def __init__(self, start):
|
||||||
|
#Game variables
|
||||||
self.matches = start # number of matches
|
self.matches = start # number of matches
|
||||||
self.state = 0 #-1 = lost, 0 = draw, +1 = won
|
self.state = 0 #-1 = lost, 0 = draw, +1 = won
|
||||||
self.playerTurn = 1 # 1 = player 1 is playing, 2 = player 2 is playing
|
self.playerTurn = 1 # 1 = player 1 is playing, 2 = player 2 is playing
|
||||||
|
#Algorithm variables
|
||||||
|
self.exploreMaxCalls = 0 # Call counter of exploreMax
|
||||||
|
self.exploreMinCalls = 0 # Call counter of exploreMax
|
||||||
|
self.firstPlayerWinning = False # Result of MinMax
|
||||||
|
|
||||||
def miniMax(self):
|
def miniMax(self):
|
||||||
|
'''
|
||||||
|
`miniMax` function starts the MiniMax algorithm
|
||||||
|
|
||||||
|
Params :
|
||||||
|
self -- the current instance of the game
|
||||||
|
|
||||||
|
Return :
|
||||||
|
-1 = lost or 0 = draw or +1 = won -- depends on the possibility to win of the player
|
||||||
|
'''
|
||||||
if(self.playerTurn == 1):
|
if(self.playerTurn == 1):
|
||||||
res = self.exploreMax(self.matches)
|
res = self.exploreMax(self.matches)
|
||||||
if(self.playerTurn == 2):
|
if(self.playerTurn == 2):
|
||||||
res = self.exploreMin(self.matches)
|
res = self.exploreMin(self.matches)
|
||||||
print(res)
|
if(res == -1):
|
||||||
|
self.firstPlayerWinning = False
|
||||||
|
else:
|
||||||
|
self.firstPlayerWinning = True
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def exploreMax(self, n):
|
def exploreMax(self, n):
|
||||||
|
'''
|
||||||
|
`exploreMax` function tries to find if a player has possibilities to win the game
|
||||||
|
|
||||||
|
Params :
|
||||||
|
self -- the current instance of the game
|
||||||
|
n -- a number of matches
|
||||||
|
|
||||||
|
Return :
|
||||||
|
-1 = lose or 0 = draw or +1 = win -- depends on the possibility to win of the player
|
||||||
|
'''
|
||||||
|
self.exploreMaxCalls += 1
|
||||||
bestVal = -1
|
bestVal = -1
|
||||||
if(n<=0):
|
if(n<=0):
|
||||||
return 1
|
return 1
|
||||||
@ -26,6 +54,17 @@ class Game(int):
|
|||||||
return bestVal
|
return bestVal
|
||||||
|
|
||||||
def exploreMin(self, n):
|
def exploreMin(self, n):
|
||||||
|
'''
|
||||||
|
`exploreMin` function tries to find if a player has possibilities to win the game
|
||||||
|
|
||||||
|
Params :
|
||||||
|
self -- the current instance of the game
|
||||||
|
n -- a number of matches
|
||||||
|
|
||||||
|
Return :
|
||||||
|
-1 = lose or 0 = draw or +1 = win -- depends on the possibility to win of the player
|
||||||
|
'''
|
||||||
|
self.exploreMinCalls += 1
|
||||||
worstVal = 1
|
worstVal = 1
|
||||||
if(n<=0):
|
if(n<=0):
|
||||||
return -1
|
return -1
|
||||||
@ -37,6 +76,31 @@ class Game(int):
|
|||||||
break
|
break
|
||||||
return worstVal
|
return worstVal
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
'''
|
||||||
|
`__str__` function overrides the default function to get a custom display when printing the game object
|
||||||
|
|
||||||
|
Params :
|
||||||
|
self -- the current instance of the game
|
||||||
|
|
||||||
|
Return :
|
||||||
|
string -- contains all the game and algorithm values
|
||||||
|
'''
|
||||||
|
content = ( "-----------------------\n"
|
||||||
|
"Game and algorithm data\n"
|
||||||
|
"-----------------------\n"
|
||||||
|
"== Game data ==\n"
|
||||||
|
f"Remaining matches : {self.matches}\n"
|
||||||
|
f"Game state : {self.state}\n"
|
||||||
|
f"Actual player : {self.playerTurn}\n"
|
||||||
|
"== Algorithm data ==\n"
|
||||||
|
f"exploreMax calls = {self.exploreMaxCalls}\n"
|
||||||
|
f"exploreMin calls = {self.exploreMinCalls}\n"
|
||||||
|
f"First player winning ? = {self.firstPlayerWinning}\n"
|
||||||
|
)
|
||||||
|
return content
|
||||||
|
|
||||||
|
|
||||||
game = Game(5) # Creating Nim game with 5 matches
|
game = Game(5) # Creating Nim game with 5 matches
|
||||||
game.miniMax() # Checking if the player 1 is winning
|
game.miniMax() # Checking if the player 1 is winning
|
||||||
|
print(game) # Displays game and algorithm data
|
119
game_optimized.py
Normal file
119
game_optimized.py
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
class Game(int):
|
||||||
|
|
||||||
|
def __init__(self, start):
|
||||||
|
#Game variables
|
||||||
|
self.matches = start # number of matches
|
||||||
|
self.state = 0 #-1 = lost, 0 = draw, +1 = won
|
||||||
|
self.playerTurn = 1 # 1 = player 1 is playing, 2 = player 2 is playing
|
||||||
|
#Algorithm variables
|
||||||
|
self.exploreMaxCalls = 0 # Call counter of exploreMax
|
||||||
|
self.exploreMinCalls = 0 # Call counter of exploreMax
|
||||||
|
self.firstPlayerWinning = False # Result of MinMax
|
||||||
|
self.algorithmResFound = False # True if MiniMax algorithm found a result
|
||||||
|
|
||||||
|
def miniMax(self):
|
||||||
|
'''
|
||||||
|
`miniMax` function starts the MiniMax algorithm
|
||||||
|
|
||||||
|
Params :
|
||||||
|
self -- the current instance of the game
|
||||||
|
|
||||||
|
Return :
|
||||||
|
-1 = lost or 0 = draw or +1 = won -- depends on the possibility to win of the player
|
||||||
|
'''
|
||||||
|
self.algorithmResFound = False
|
||||||
|
if(self.playerTurn == 1):
|
||||||
|
res = self.exploreMax(self.matches)
|
||||||
|
if(self.playerTurn == 2):
|
||||||
|
res = self.exploreMin(self.matches)
|
||||||
|
if(res == -1):
|
||||||
|
self.firstPlayerWinning = False
|
||||||
|
else:
|
||||||
|
self.firstPlayerWinning = True
|
||||||
|
return res
|
||||||
|
|
||||||
|
def exploreMax(self, n):
|
||||||
|
'''
|
||||||
|
`exploreMax` function tries to find if a player has possibilities to win the game
|
||||||
|
|
||||||
|
Params :
|
||||||
|
self -- the current instance of the game
|
||||||
|
n -- a number of matches
|
||||||
|
|
||||||
|
Return :
|
||||||
|
-1 = lose or 0 = draw or +1 = win -- depends on the possibility to win of the player
|
||||||
|
'''
|
||||||
|
self.exploreMaxCalls += 1
|
||||||
|
bestVal = -1
|
||||||
|
if(self.algorithmResFound):
|
||||||
|
return 1
|
||||||
|
if(n<=0):
|
||||||
|
self.algorithmResFound = True
|
||||||
|
return 1
|
||||||
|
for i in range (1,4):
|
||||||
|
if(self.algorithmResFound):
|
||||||
|
break
|
||||||
|
res = self.exploreMin(n-i)
|
||||||
|
if(res > bestVal):
|
||||||
|
bestVal = res
|
||||||
|
if(bestVal == 1):
|
||||||
|
self.algorithmResFound = True
|
||||||
|
break
|
||||||
|
return bestVal
|
||||||
|
|
||||||
|
def exploreMin(self, n):
|
||||||
|
'''
|
||||||
|
`exploreMin` function tries to find if a player has possibilities to win the game
|
||||||
|
|
||||||
|
Params :
|
||||||
|
self -- the current instance of the game
|
||||||
|
n -- a number of matches
|
||||||
|
|
||||||
|
Return :
|
||||||
|
-1 = lose or 0 = draw or +1 = win -- depends on the possibility to win of the player
|
||||||
|
'''
|
||||||
|
self.exploreMinCalls += 1
|
||||||
|
worstVal = 1
|
||||||
|
if(self.algorithmResFound):
|
||||||
|
return -1
|
||||||
|
if(n<=0):
|
||||||
|
self.algorithmResFound = True
|
||||||
|
return -1
|
||||||
|
for i in range (1,4):
|
||||||
|
if(self.algorithmResFound):
|
||||||
|
break
|
||||||
|
res = self.exploreMax(n-i)
|
||||||
|
if(res < worstVal):
|
||||||
|
worstVal = res
|
||||||
|
if(worstVal == -1):
|
||||||
|
self.algorithmResFound = True
|
||||||
|
break
|
||||||
|
return worstVal
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
'''
|
||||||
|
`__str__` function overrides the default function to get a custom display when printing the game object
|
||||||
|
|
||||||
|
Params :
|
||||||
|
self -- the current instance of the game
|
||||||
|
|
||||||
|
Return :
|
||||||
|
string -- contains all the game and algorithm values
|
||||||
|
'''
|
||||||
|
content = ( "-----------------------\n"
|
||||||
|
"Game and algorithm data\n"
|
||||||
|
"-----------------------\n"
|
||||||
|
"== Game data ==\n"
|
||||||
|
f"Remaining matches : {self.matches}\n"
|
||||||
|
f"Game state : {self.state}\n"
|
||||||
|
f"Actual player : {self.playerTurn}\n"
|
||||||
|
"== Algorithm data ==\n"
|
||||||
|
f"exploreMax calls = {self.exploreMaxCalls}\n"
|
||||||
|
f"exploreMin calls = {self.exploreMinCalls}\n"
|
||||||
|
f"First player winning ? = {self.firstPlayerWinning}\n"
|
||||||
|
)
|
||||||
|
return content
|
||||||
|
|
||||||
|
game = Game(5) # Creating Nim game with 5 matches
|
||||||
|
game.miniMax() # Checking if the player 1 is winning
|
||||||
|
print(game) # Displays game and algorithm data
|
Loading…
Reference in New Issue
Block a user