Example #1
0
    def minimax_a_b_p(self,
                      boardObj,
                      maximizingPlayer,
                      alpha=-math.inf,
                      beta=math.inf,
                      depth=0):
        """ recursion, finds the best move based on the heuristic of the board """
        if depth == 0 or boardObj.Game_Finish():
            return boardObj  # .Heuristic(self.player,self.enemy)

        if maximizingPlayer:
            maxEval = Othello()
            maxEval.setHeuristic(-math.inf)

            test = self.posibleMoves(boardObj, self.player)
            if test == []:
                return boardObj

            for child in test:

                evalBoard = self.minimax_a_b_p(child,
                                               False,
                                               alpha=alpha,
                                               beta=beta,
                                               depth=depth - 1)

                if maxEval.Heuristic(self.player,
                                     self.enemy) < evalBoard.Heuristic(
                                         self.player, self.enemy):
                    maxEval = evalBoard

                alpha = max(alpha, evalBoard.Heuristic(self.player,
                                                       self.enemy))

                if beta <= alpha:
                    break

                return maxEval

        else:
            minEval = Othello()
            minEval.setHeuristic(math.inf)
            test = self.posibleMoves(boardObj, self.enemy)

            if test == []:
                return boardObj

            for child in test:
                evalBoard = self.minimax_a_b_p(child,
                                               True,
                                               alpha=alpha,
                                               beta=beta,
                                               depth=depth - 1)

                if minEval.Heuristic(self.player,
                                     self.enemy) > evalBoard.Heuristic(
                                         self.player, self.enemy):
                    minEval = evalBoard
                beta = min(beta, evalBoard.Heuristic(self.player, self.enemy))
                if beta <= alpha:
                    break
                return minEval