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