Exemplo n.º 1
0
    def evaluate(self, history, whiteKing, whiteQueen, whiteBishop,
                 whiteKnight, whiteRook, whitePawn, blackKing, blackQueen,
                 blackBishop, blackKnight, blackRook, blackPawn,
                 whiteQueenCastle, whiteKingCastle, blackQueenCastle,
                 blackKingCastle, depth, playerIsWhite):
        Moves = self.Moves
        counter = 0

        self.currKing = whiteKing
        self.currQueen = whiteQueen
        self.currBishop = whiteBishop
        self.currKnight = whiteKnight
        self.currRook = whiteRook
        self.currPawn = whitePawn
        self.altKing = blackKing
        self.altQueen = blackQueen
        self.altBishop = blackBishop
        self.altKnight = blackKnight
        self.altRook = blackRook
        self.altPawn = blackPawn

        whitePossibleMoves = Moves.white_legalMoves(
            history, whiteKing, whiteQueen, whiteBishop, whiteKnight,
            whiteRook, whitePawn, blackKing, blackQueen, blackBishop,
            blackKnight, blackRook, blackPawn, whiteQueenCastle,
            whiteKingCastle, blackQueenCastle, blackKingCastle)
        blackPossibleMoves = Moves.black_legalMoves(
            history, whiteKing, whiteQueen, whiteBishop, whiteKnight,
            whiteRook, whitePawn, blackKing, blackQueen, blackBishop,
            blackKnight, blackRook, blackPawn, whiteQueenCastle,
            whiteKingCastle, blackQueenCastle, blackKingCastle)
        numOfWhitePossibleMoves = len(whitePossibleMoves.split())
        numOfBlackPossibleMoves = len(blackPossibleMoves.split())

        whiteKingMoves = Moves.whiteKing_moves(
            whiteKing, whiteQueen, whiteBishop, whiteKnight, whiteRook,
            whitePawn, blackKing, blackQueen, blackBishop, blackKnight,
            blackRook, blackPawn, whiteQueenCastle, whiteKingCastle,
            blackQueenCastle, blackKingCastle)
        blackKingMoves = Moves.blackKing_moves(
            whiteKing, whiteQueen, whiteBishop, whiteKnight, whiteRook,
            whitePawn, blackKing, blackQueen, blackBishop, blackKnight,
            blackRook, blackPawn, whiteQueenCastle, whiteKingCastle,
            blackQueenCastle, blackKingCastle)
        numOfWhiteKingMoves = len(whiteKingMoves.split())
        numofBlackKingMoves = len(blackKingMoves.split())

        boardFlipped = False
        if (playerIsWhite):
            pass
        elif (not playerIsWhite):
            self.flipBoard()
            boardFlipped = True
        """
		-> Our internal board is always represented with black pieces on top, white pieces at the bottom. 
		-> Our method usually will just evaluate white pieces, add it to our counter; flip the board, evaluate white pieces again, 
			and deduct them from our counter. 
		-> If player is white and if it's white turn to move, there's no problem at all with this approach, 
			since we will add white pieces' score to our counter, and deduct black pieces' score from our counter.
		-> If player is white and if it's black turn to move, observe that there is still no problem with this approach. 
			In particular, we'll add white pieces' score to our counter, and deduct black pieces' score from our counter. This is okay, because
			we want our board evaluation to be based on white player's perspective. We want to see if when black makes a move, how does it affect white player.
		-> When player is black, we just need to flip board before computing. The rest of the procedure remains the same.

		"""

        #give each piece on the board a score, rate the board
        #Player's perspective
        material = self.rateMaterial(self.currKing, self.currQueen,
                                     self.currBishop, self.currKnight,
                                     self.currRook, self.currPawn)
        counter = counter + self.rateAttack(
            self.currKing, self.currQueen, self.currBishop, self.currKnight,
            self.currRook, self.currPawn, self.altKing, self.altQueen,
            self.altBishop, self.altKnight, self.altRook, self.altPawn)
        counter = counter + material
        #score for white pieces (player is white)
        if (not boardFlipped):
            #rate Moveability
            counter = counter + self.rateMoveability(
                self.currKing, self.currQueen, self.currBishop,
                self.currKnight, self.currRook, self.currPawn, self.altKing,
                self.altQueen, self.altBishop, self.altKnight, self.altRook,
                self.altPawn, depth, material, numOfWhitePossibleMoves)
            #rate positional
            counter = counter + self.ratePositional(
                self.currKing, self.currQueen, self.currBishop,
                self.currKnight, self.currRook, self.currPawn, material,
                numOfWhiteKingMoves)
        #score for black pieces (player is black)
        elif (boardFlipped):
            #rate Moveability
            counter = counter + self.rateMoveability(
                self.currKing, self.currQueen, self.currBishop,
                self.currKnight, self.currRook, self.currPawn, self.altKing,
                self.altQueen, self.altBishop, self.altKnight, self.altRook,
                self.altPawn, depth, material, numOfBlackPossibleMoves)
            #rate positional
            counter = counter + self.ratePositional(
                self.currKing, self.currQueen, self.currBishop,
                self.currKnight, self.currRook, self.currPawn, material,
                numofBlackKingMoves)
        self.flipBoard()
        #we need to update our board flipped variable according to if our board is flipped now
        #if board is not flipped before, since we just do flipBoard(), our board is flipped now.
        if (boardFlipped == False):
            boardFlipped = True
        #if board is flipped before, since we just do flipBoard(), board is now back to its original state. So board is not flipped now.
        elif (boardFlipped == True):
            boardFlipped = False

        #Opponent's perspective
        material = self.rateMaterial(self.currKing, self.currQueen,
                                     self.currBishop, self.currKnight,
                                     self.currRook, self.currPawn)
        counter = counter - self.rateAttack(
            self.currKing, self.currQueen, self.currBishop, self.currKnight,
            self.currRook, self.currPawn, self.altKing, self.altQueen,
            self.altBishop, self.altKnight, self.altRook, self.altPawn)
        counter = counter - material
        #score for black pieces (player is white)
        if (boardFlipped):
            counter = counter - self.rateMoveability(
                self.currKing, self.currQueen, self.currBishop,
                self.currKnight, self.currRook, self.currPawn, self.altKing,
                self.altQueen, self.altBishop, self.altKnight, self.altRook,
                self.altPawn, depth, material, numOfBlackPossibleMoves)
            counter = counter - self.ratePositional(
                self.currKing, self.currQueen, self.currBishop,
                self.currKnight, self.currRook, self.currPawn, material,
                numofBlackKingMoves)
        #score for white pieces (player is black)
        elif (not boardFlipped):
            counter = counter - self.rateMoveability(
                self.currKing, self.currQueen, self.currBishop,
                self.currKnight, self.currRook, self.currPawn, self.altKing,
                self.altQueen, self.altBishop, self.altKnight, self.altRook,
                self.altPawn, depth, material, numOfWhitePossibleMoves)
            counter = counter - self.ratePositional(
                self.currKing, self.currQueen, self.currBishop,
                self.currKnight, self.currRook, self.currPawn, material,
                numOfWhiteKingMoves)
        self.flipBoard()
        #we need to update our board flipped variable according to if our board is flipped now
        #if board is not flipped before, since we just do flipBoard(), our board is flipped now.
        if (boardFlipped == False):
            boardFlipped = True
        #if board is flipped before, since we just do flipBoard(), board is now back to its original state. So board is not flipped now.
        elif (boardFlipped == True):
            boardFlipped = False

        #restore board to original state. black pieces on top, white pieces at bottom
        if (boardFlipped):
            self.flipBoard()
            boardFlipped = False

        return (counter + depth * 500)