From 7605547ff407d10b9a07de123592a729cb201154 Mon Sep 17 00:00:00 2001 From: schied Date: Thu, 12 Sep 2024 11:46:27 +0200 Subject: [PATCH] =?UTF-8?q?Cr=C3=A9ation=20de=20l'algo=20minimax?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 game.py diff --git a/game.py b/game.py new file mode 100644 index 0000000..47f90dc --- /dev/null +++ b/game.py @@ -0,0 +1,42 @@ +class Game(int): + + def __init__(self, start): + 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 + + def miniMax(self): + if(self.playerTurn == 1): + res = self.exploreMax(self.matches) + if(self.playerTurn == 2): + res = self.exploreMin(self.matches) + print(res) + return res + + def exploreMax(self, n): + bestVal = -1 + if(n<=0): + return 1 + for i in range (1,4): + res = self.exploreMin(n-i) + if(res > bestVal): + bestVal = res + if(bestVal == 1): + break + return bestVal + + def exploreMin(self, n): + worstVal = 1 + if(n<=0): + return -1 + for i in range (1,4): + res = self.exploreMax(n-i) + if(res < worstVal): + worstVal = res + if(worstVal == -1): + break + return worstVal + + +game = Game(5) # Creating Nim game with 5 matches +game.miniMax() # Checking if the player 1 is winning