def sortMoves(self, moves, history, whiteKing, whiteQueen, whiteBishop, whiteKnight, whiteRook, whitePawn, blackKing, blackQueen, blackBishop, blackKnight, blackRook, blackPawn, whiteQueenCastle, whiteKingCastle, blackQueenCastle, blackKingCastle, whiteTurn, depth): Moves = self.Moves Rating = self.Rating MoveAndScoreList = [] li = [] for i in range(len(moves)): currentMove = moves[i] try: originalX = int(currentMove[0]) originalY = int(currentMove[1]) newX = int(currentMove[2]) newY = int(currentMove[3]) except (ValueError): raise ValueError( "Error in sortMoves: the first four character of currentMove string must be integer." ) start = (originalX * 8) + originalY end = (newX * 8) + newY ##make currentMove #white pieces tempWhiteKing = Moves.makeMove(whiteKing, currentMove, "K") tempWhiteQueen = Moves.makeMove(whiteQueen, currentMove, "Q") tempWhiteBishop = Moves.makeMove(whiteBishop, currentMove, "B") tempWhiteKnight = Moves.makeMove(whiteKnight, currentMove, "H") tempWhiteRook = Moves.makeMove(whiteRook, currentMove, "R") tempWhitePawn = Moves.makeMove(whitePawn, currentMove, "P") #if castling, make castling move if ("WL" in currentMove or "WR" in currentMove): tempWhiteKing, tempWhiteRook = Moves.makeCastlingMove( whiteKing, whiteRook, currentMove) #black pieces tempBlackKing = Moves.makeMove(blackKing, currentMove, "k") tempBlackQueen = Moves.makeMove(blackQueen, currentMove, "q") tempBlackBishop = Moves.makeMove(blackBishop, currentMove, "b") tempBlackKnight = Moves.makeMove(blackKnight, currentMove, "h") tempBlackRook = Moves.makeMove(blackRook, currentMove, "r") tempBlackPawn = Moves.makeMove(blackPawn, currentMove, "p") if ("BL" in currentMove or "BR" in currentMove): tempBlackKing, tempBlackRook = Moves.makeCastlingMove( blackKing, blackRook, currentMove) tempHistory = currentMove ###update castling variables #copy castling variables from previous tempWhiteQueenCastle = whiteQueenCastle tempWhiteKingCastle = whiteKingCastle tempBlackQueenCastle = blackQueenCastle tempBlackKingCastle = blackKingCastle #update castling variable based on the currentMove we made #if currentMove is making white castling move, we can no longer castle again for white if ("WL" in currentMove or "WR" in currentMove): tempWhiteQueenCastle = False tempWhiteKingCastle = False #if currentMove is making black castling move, we can no longer castle again for black elif ("BL" in currentMove or "BR" in currentMove): tempBlackQueenCastle = False tempBlackKingCastle = False else: #if currentMove is moving whiteKing, white queen and king side castle become False if (((1 << start) & whiteKing) != 0): tempWhiteQueenCastle = False tempWhiteKingCastle = False #if currentMove is moving blackKing, black queen and king side castle become False elif (((1 << start) & blackKing) != 0): tempBlackQueenCastle = False tempBlackKingCastle = False #if currentMove is moving white left rook, white queenside castling become False elif (((1 << start) & whiteRook & (1 << 56)) != 0): tempWhiteQueenCastle = False #if currentMove is moving white right rook, white kingside castling become False elif (((1 << start) & whiteRook & (1 << 63)) != 0): tempWhiteKingCastle = False elif (((1 << start) & blackRook & (1 << 0)) != 0): tempBlackQueenCastle = False elif (((1 << start) & blackRook & (1 << 7)) != 0): tempBlackKingCastle = False currentScore = Rating.quickEvaluate( tempHistory, tempWhiteKing, tempWhiteQueen, tempWhiteBishop, tempWhiteKnight, tempWhiteRook, tempWhitePawn, tempBlackKing, tempBlackQueen, tempBlackBishop, tempBlackKnight, tempBlackRook, tempBlackPawn, tempWhiteQueenCastle, tempWhiteKingCastle, tempBlackQueenCastle, tempBlackKingCastle, depth + 1, self.playerIsWhite) moveAndScore = MoveAndScore(currentMove, currentScore) MoveAndScoreList.append(moveAndScore) #Idea: #if this is player's turn, we need to return list sorted in descending order. Because player want to choose the most maximum score move. #If this is opponent's turn, we need to return list sorted in ascending order. Because opponent want to choose the most minimum score move. #if this is player's turn, sort in descending order if (self.playerIsWhite == whiteTurn): MoveAndScoreList.sort(key=self.getScore, reverse=True) #if this is opponent's turn, sort in ascending order else: MoveAndScoreList.sort(key=self.getScore, reverse=False) for i in range(len(MoveAndScoreList)): li.append(MoveAndScoreList[i].move) return li
def sanitizeMove(self, moves, history, whiteKing, whiteQueen, whiteBishop, whiteKnight, whiteRook, whitePawn, blackKing, blackQueen, blackBishop, blackKnight, blackRook, blackPawn, whiteQueenCastle, whiteKingCastle, blackQueenCastle, blackKingCastle, whiteTurn): Moves = self.Moves moves = moves.split() li = [] for i in range(len(moves)): currentMove = moves[i] #white pieces tempWhiteKing = Moves.makeMove(whiteKing, currentMove, "K") tempWhiteQueen = Moves.makeMove(whiteQueen, currentMove, "Q") tempWhiteBishop = Moves.makeMove(whiteBishop, currentMove, "B") tempWhiteKnight = Moves.makeMove(whiteKnight, currentMove, "H") tempWhiteRook = Moves.makeMove(whiteRook, currentMove, "R") tempWhitePawn = Moves.makeMove(whitePawn, currentMove, "P") #if castling, make castling move if ("WL" in currentMove or "WR" in currentMove): tempWhiteKing, tempWhiteRook = Moves.makeCastlingMove( whiteKing, whiteRook, currentMove) #black pieces tempBlackKing = Moves.makeMove(blackKing, currentMove, "k") tempBlackQueen = Moves.makeMove(blackQueen, currentMove, "q") tempBlackBishop = Moves.makeMove(blackBishop, currentMove, "b") tempBlackKnight = Moves.makeMove(blackKnight, currentMove, "h") tempBlackRook = Moves.makeMove(blackRook, currentMove, "r") tempBlackPawn = Moves.makeMove(blackPawn, currentMove, "p") if ("BL" in currentMove or "BR" in currentMove): tempBlackKing, tempBlackRook = Moves.makeCastlingMove( blackKing, blackRook, currentMove) tempHistory = currentMove if (whiteTurn): if ((tempWhiteKing & Moves.whiteKing_illegalMoves( tempWhiteKing, tempWhiteQueen, tempWhiteBishop, tempWhiteKnight, tempWhiteRook, tempWhitePawn, tempBlackKing, tempBlackQueen, tempBlackBishop, tempBlackKnight, tempBlackRook, tempBlackPawn)) == 0): li.append(currentMove) elif (not whiteTurn): if ((tempBlackKing & Moves.blackKing_illegalMoves( tempWhiteKing, tempWhiteQueen, tempWhiteBishop, tempWhiteKnight, tempWhiteRook, tempWhitePawn, tempBlackKing, tempBlackQueen, tempBlackBishop, tempBlackKnight, tempBlackRook, tempBlackPawn)) == 0): li.append(currentMove) return li
def updateMove(self, move, gui=False): Moves = self.Moves ##For debugging # print("The move you entered is "+move) if (len(move) < 4): raise ValueError( "Error in updateMove: the move entered is not a valid move.") currentMove = None for i in range(len(self.currentAllLegalMoves)): #if player input a move of length 4, this move CANNOT automatically fulfill pawn promotion moves as player needs to choose the piece for promotion if (move in self.currentAllLegalMoves[i]): if (len(move) == 4): if (len(self.currentAllLegalMoves[i]) > 4): #if this is not a pawn promotion move, and is just castling or en passant move, assign the golden standard move to current move if (self.currentAllLegalMoves[i][5] != "P"): currentMove = self.currentAllLegalMoves[i] #if THIS IS A PAWN PROMOTION MOVE, raise Error since pawn promotion piece is not specified else: if (gui == True): raise PawnPromotionPieceNotDefinedError( "Error in updateMove: pawn promotion piece is not defined." ) else: pass #if this is not a pawn promotion move, and is just normal move, assign the golden standard move to current move else: currentMove = self.currentAllLegalMoves[i] elif (len(move) > 4): currentMove = self.currentAllLegalMoves[i] if (currentMove == None): raise ValueError( "Error in updateMove; the move entered is not a valid move.") try: originalX = int(currentMove[0]) originalY = int(currentMove[1]) newX = int(currentMove[2]) newY = int(currentMove[3]) except (ValueError): raise ValueError( "Error in updateMove: the first four character of move string must be integer." ) start = (originalX * 8) + originalY end = (newX * 8) + newY #white pieces tempWhiteKing = Moves.makeMove(self.whiteKing, currentMove, "K") tempWhiteQueen = Moves.makeMove(self.whiteQueen, currentMove, "Q") tempWhiteBishop = Moves.makeMove(self.whiteBishop, currentMove, "B") tempWhiteKnight = Moves.makeMove(self.whiteKnight, currentMove, "H") tempWhiteRook = Moves.makeMove(self.whiteRook, currentMove, "R") tempWhitePawn = Moves.makeMove(self.whitePawn, currentMove, "P") #if castling, make castling move if ("WL" in currentMove or "WR" in currentMove): tempWhiteKing, tempWhiteRook = Moves.makeCastlingMove( self.whiteKing, self.whiteRook, currentMove) #black pieces tempBlackKing = Moves.makeMove(self.blackKing, currentMove, "k") tempBlackQueen = Moves.makeMove(self.blackQueen, currentMove, "q") tempBlackBishop = Moves.makeMove(self.blackBishop, currentMove, "b") tempBlackKnight = Moves.makeMove(self.blackKnight, currentMove, "h") tempBlackRook = Moves.makeMove(self.blackRook, currentMove, "r") tempBlackPawn = Moves.makeMove(self.blackPawn, currentMove, "p") if ("BL" in currentMove or "BR" in currentMove): tempBlackKing, tempBlackRook = Moves.makeCastlingMove( self.blackKing, self.blackRook, currentMove) tempHistory = currentMove ###update castling variables #copy castling variables from previous tempWhiteQueenCastle = self.whiteQueenCastle tempWhiteKingCastle = self.whiteKingCastle tempBlackQueenCastle = self.blackQueenCastle tempBlackKingCastle = self.blackKingCastle #update castling variable based on the currentMove we made #if currentMove is making white castling move, we can no longer castle again for white if ("WL" in currentMove or "WR" in currentMove): tempWhiteQueenCastle = False tempWhiteKingCastle = False #if currentMove is making black castling move, we can no longer castle again for black elif ("BL" in currentMove or "BR" in currentMove): tempBlackQueenCastle = False tempBlackKingCastle = False else: #if currentMove is moving whiteKing, white queen and king side castle become False if (((1 << start) & self.whiteKing) != 0): tempWhiteQueenCastle = False tempWhiteKingCastle = False #if currentMove is moving blackKing, black queen and king side castle become False elif (((1 << start) & self.blackKing) != 0): tempBlackQueenCastle = False tempBlackKingCastle = False #if currentMove is moving white left rook, white queenside castling become False elif (((1 << start) & self.whiteRook & (1 << 56)) != 0): tempWhiteQueenCastle = False #if currentMove is moving white right rook, white kingside castling become False elif (((1 << start) & self.whiteRook & (1 << 63)) != 0): tempWhiteKingCastle = False elif (((1 << start) & self.blackRook & (1 << 0)) != 0): tempBlackQueenCastle = False elif (((1 << start) & self.blackRook & (1 << 7)) != 0): tempBlackKingCastle = False #update history self.history = tempHistory #update white pieces self.whiteKing = tempWhiteKing self.whiteQueen = tempWhiteQueen self.whiteBishop = tempWhiteBishop self.whiteKnight = tempWhiteKnight self.whiteRook = tempWhiteRook self.whitePawn = tempWhitePawn #update black pieces self.blackKing = tempBlackKing self.blackQueen = tempBlackQueen self.blackBishop = tempBlackBishop self.blackKnight = tempBlackKnight self.blackRook = tempBlackRook self.blackPawn = tempBlackPawn #update castling variables self.whiteQueenCastle = tempWhiteQueenCastle self.whiteKingCastle = tempWhiteKingCastle self.blackQueenCastle = tempBlackQueenCastle self.blackKingCastle = tempBlackKingCastle #if gui is True, we also need to update our chessBoard (arrays of arrays) if (gui == True): self.updateChessBoard() #if gui is False, we don't need to update our chessBoard (arrays of arrays) since we will call the drawBoard method in chessAI_console else: pass
def principalVariationSearch(self, alpha, beta, history, whiteKing, whiteQueen, whiteBishop, whiteKnight, whiteRook, whitePawn, blackKing, blackQueen, blackBishop, blackKnight, blackRook, blackPawn, whiteQueenCastle, whiteKingCastle, blackQueenCastle, blackKingCastle, whiteTurn, depth): Moves = self.Moves Rating = self.Rating #if we reach our max search depth, return the best score for the board at that level if (depth == self.maxDepth): bestScore = Rating.evaluate( history, whiteKing, whiteQueen, whiteBishop, whiteKnight, whiteRook, whitePawn, blackKing, blackQueen, blackBishop, blackKnight, blackRook, blackPawn, whiteQueenCastle, whiteKingCastle, blackQueenCastle, blackKingCastle, depth, self.playerIsWhite) return bestScore, "No Move" if (whiteTurn): moves = Moves.white_legalMoves( history, whiteKing, whiteQueen, whiteBishop, whiteKnight, whiteRook, whitePawn, blackKing, blackQueen, blackBishop, blackKnight, blackRook, blackPawn, whiteQueenCastle, whiteKingCastle, blackQueenCastle, blackKingCastle) else: moves = Moves.black_legalMoves( history, whiteKing, whiteQueen, whiteBishop, whiteKnight, whiteRook, whitePawn, blackKing, blackQueen, blackBishop, blackKnight, blackRook, blackPawn, whiteQueenCastle, whiteKingCastle, blackQueenCastle, blackKingCastle) #get the first legal move for the current player. After the moves have been sorted, we assume that the first move is always the best move. moves = self.sanitizeMove(moves, history, whiteKing, whiteQueen, whiteBishop, whiteKnight, whiteRook, whitePawn, blackKing, blackQueen, blackBishop, blackKnight, blackRook, blackPawn, whiteQueenCastle, whiteKingCastle, blackQueenCastle, blackKingCastle, whiteTurn) #if no legal move for current player, then it must be checkmate or stalemate #if this is player's turn, this is really bad. return -5000 #if this is opponent's turn, this is good. we want this. return 5000 #TODO: Check this AND FIX THIS! if (len(moves) == 0): if (self.playerIsWhite == whiteTurn): return -5000, "No Move" else: return 5000, "No Move" moves = self.sortMoves(moves, history, whiteKing, whiteQueen, whiteBishop, whiteKnight, whiteRook, whitePawn, blackKing, blackQueen, blackBishop, blackKnight, blackRook, blackPawn, whiteQueenCastle, whiteKingCastle, blackQueenCastle, blackKingCastle, whiteTurn, depth) firstLegalMoveIndex = 0 b = beta bestScore = -math.inf ####################################### throughroughly search firstLegalMove firstLegalMove = moves[firstLegalMoveIndex] try: originalX = int(firstLegalMove[0]) originalY = int(firstLegalMove[1]) newX = int(firstLegalMove[2]) newY = int(firstLegalMove[3]) except (ValueError): raise ValueError( "Error in principalVariationSearch: the first four character of firstLegalMove string must be integer." ) start = (originalX * 8) + originalY end = (newX * 8) + newY #white pieces tempWhiteKing = Moves.makeMove(whiteKing, firstLegalMove, "K") tempWhiteQueen = Moves.makeMove(whiteQueen, firstLegalMove, "Q") tempWhiteBishop = Moves.makeMove(whiteBishop, firstLegalMove, "B") tempWhiteKnight = Moves.makeMove(whiteKnight, firstLegalMove, "H") tempWhiteRook = Moves.makeMove(whiteRook, firstLegalMove, "R") tempWhitePawn = Moves.makeMove(whitePawn, firstLegalMove, "P") #if castling, make castling move if ("WL" in firstLegalMove or "WR" in firstLegalMove): tempWhiteKing, tempWhiteRook = Moves.makeCastlingMove( whiteKing, whiteRook, firstLegalMove) #black pieces tempBlackKing = Moves.makeMove(blackKing, firstLegalMove, "k") tempBlackQueen = Moves.makeMove(blackQueen, firstLegalMove, "q") tempBlackBishop = Moves.makeMove(blackBishop, firstLegalMove, "b") tempBlackKnight = Moves.makeMove(blackKnight, firstLegalMove, "h") tempBlackRook = Moves.makeMove(blackRook, firstLegalMove, "r") tempBlackPawn = Moves.makeMove(blackPawn, firstLegalMove, "p") if ("BL" in firstLegalMove or "BR" in firstLegalMove): tempBlackKing, tempBlackRook = Moves.makeCastlingMove( blackKing, blackRook, firstLegalMove) tempHistory = firstLegalMove ###update castling variables #copy castling variables from previous tempWhiteQueenCastle = whiteQueenCastle tempWhiteKingCastle = whiteKingCastle tempBlackQueenCastle = blackQueenCastle tempBlackKingCastle = blackKingCastle #update castling variable based on the firstLegalMove we made #if firstLegalMove is making white castling move, we can no longer castle again for white if ("WL" in firstLegalMove or "WR" in firstLegalMove): tempWhiteQueenCastle = False tempWhiteKingCastle = False #if firstLegalMove is making black castling move, we can no longer castle again for black elif ("BL" in firstLegalMove or "BR" in firstLegalMove): tempBlackQueenCastle = False tempBlackKingCastle = False else: #if firstLegalMove is moving whiteKing, white queen and king side castle become False if (((1 << start) & whiteKing) != 0): tempWhiteQueenCastle = False tempWhiteKingCastle = False #if firstLegalMove is moving blackKing, black queen and king side castle become False elif (((1 << start) & blackKing) != 0): tempBlackQueenCastle = False tempBlackKingCastle = False #if firstLegalMove is moving white left rook, white queenside castling become False elif (((1 << start) & whiteRook & (1 << 56)) != 0): tempWhiteQueenCastle = False #if firstLegalMove is moving white right rook, white kingside castling become False elif (((1 << start) & whiteRook & (1 << 63)) != 0): tempWhiteKingCastle = False elif (((1 << start) & blackRook & (1 << 0)) != 0): tempBlackQueenCastle = False elif (((1 << start) & blackRook & (1 << 7)) != 0): tempBlackKingCastle = False """ # original algorithm score,bestMove=-self.principalVariationSearch(-b,-alpha,tempHistory,tempWhiteKing,tempWhiteQueen,tempWhiteBishop,tempWhiteKnight,tempWhiteRook,tempWhitePawn,tempBlackKing,tempBlackQueen,tempBlackBishop,tempBlackKnight,tempBlackRook,tempBlackPawn,tempWhiteQueenCastle,tempWhiteKingCastle,tempBlackQueenCastle,tempBlackKingCastle,not whiteTurn,depth+1) """ #Alternate way of writing to original algorithm score, bestMove = self.principalVariationSearch( -b, -alpha, tempHistory, tempWhiteKing, tempWhiteQueen, tempWhiteBishop, tempWhiteKnight, tempWhiteRook, tempWhitePawn, tempBlackKing, tempBlackQueen, tempBlackBishop, tempBlackKnight, tempBlackRook, tempBlackPawn, tempWhiteQueenCastle, tempWhiteKingCastle, tempBlackQueenCastle, tempBlackKingCastle, not whiteTurn, depth + 1) score = -score #In principal variation search, we assume that our first move is the best move, and thus yield the best score. bestScore = self.max(bestScore, score) #can also write bestScore=score alpha = self.max(alpha, score) bestMoveIndex = firstLegalMoveIndex if (alpha >= beta): return alpha, moves[bestMoveIndex] b = alpha + 1 ####################################### simply and quickly search through remaining move to confirm our assumption that firstLegalMove is the best move for i in range(len(moves)): #if current iteration is firstLegalMoveIndex, skip, since we already thoroughly searched this move. if (i == firstLegalMoveIndex): continue #if current iteration is not firstLegalMoveIndex, simply and quickly search through current move. currentMove = moves[i] try: originalX = int(currentMove[0]) originalY = int(currentMove[1]) newX = int(currentMove[2]) newY = int(currentMove[3]) except (ValueError): raise ValueError( "Error in principalVariationSearch: the first four character of currentMove string must be integer." ) start = (originalX * 8) + originalY end = (newX * 8) + newY #white pieces tempWhiteKing = Moves.makeMove(whiteKing, currentMove, "K") tempWhiteQueen = Moves.makeMove(whiteQueen, currentMove, "Q") tempWhiteBishop = Moves.makeMove(whiteBishop, currentMove, "B") tempWhiteKnight = Moves.makeMove(whiteKnight, currentMove, "H") tempWhiteRook = Moves.makeMove(whiteRook, currentMove, "R") tempWhitePawn = Moves.makeMove(whitePawn, currentMove, "P") #if castling, make castling move if ("WL" in currentMove or "WR" in currentMove): tempWhiteKing, tempWhiteRook = Moves.makeCastlingMove( whiteKing, whiteRook, currentMove) #black pieces tempBlackKing = Moves.makeMove(blackKing, currentMove, "k") tempBlackQueen = Moves.makeMove(blackQueen, currentMove, "q") tempBlackBishop = Moves.makeMove(blackBishop, currentMove, "b") tempBlackKnight = Moves.makeMove(blackKnight, currentMove, "h") tempBlackRook = Moves.makeMove(blackRook, currentMove, "r") tempBlackPawn = Moves.makeMove(blackPawn, currentMove, "p") if ("BL" in currentMove or "BR" in currentMove): tempBlackKing, tempBlackRook = Moves.makeCastlingMove( blackKing, blackRook, currentMove) tempHistory = currentMove ###update castling variables #copy castling variables from previous tempWhiteQueenCastle = whiteQueenCastle tempWhiteKingCastle = whiteKingCastle tempBlackQueenCastle = blackQueenCastle tempBlackKingCastle = blackKingCastle #update castling variable based on the currentMove we made #if currentMove is making white castling move, we can no longer castle again for white if ("WL" in currentMove or "WR" in currentMove): tempWhiteQueenCastle = False tempWhiteKingCastle = False #if currentMove is making black castling move, we can no longer castle again for black elif ("BL" in currentMove or "BR" in currentMove): tempBlackQueenCastle = False tempBlackKingCastle = False else: #if currentMove is moving whiteKing, white queen and king side castle become False if (((1 << start) & whiteKing) != 0): tempWhiteQueenCastle = False tempWhiteKingCastle = False #if currentMove is moving blackKing, black queen and king side castle become False elif (((1 << start) & blackKing) != 0): tempBlackQueenCastle = False tempBlackKingCastle = False #if currentMove is moving white left rook, white queenside castling become False elif (((1 << start) & whiteRook & (1 << 56)) != 0): tempWhiteQueenCastle = False #if currentMove is moving white right rook, white kingside castling become False elif (((1 << start) & whiteRook & (1 << 63)) != 0): tempWhiteKingCastle = False elif (((1 << start) & blackRook & (1 << 0)) != 0): tempBlackQueenCastle = False elif (((1 << start) & blackRook & (1 << 7)) != 0): tempBlackKingCastle = False #TODO: check this! """ # original algorithm score,bestMove=-self.principalVariationSearch(-b,-alpha,tempHistory,tempWhiteKing,tempWhiteQueen,tempWhiteBishop,tempWhiteKnight,tempWhiteRook,tempWhitePawn,tempBlackKing,tempBlackQueen,tempBlackBishop,tempBlackKnight,tempBlackRook,tempBlackPawn,tempWhiteQueenCastle,tempWhiteKingCastle,tempBlackQueenCastle,tempBlackKingCastle,not whiteTurn,depth+1) """ #Alternate way of writing to original algorithm score, bestMove = self.principalVariationSearch( -b, -alpha, tempHistory, tempWhiteKing, tempWhiteQueen, tempWhiteBishop, tempWhiteKnight, tempWhiteRook, tempWhitePawn, tempBlackKing, tempBlackQueen, tempBlackBishop, tempBlackKnight, tempBlackRook, tempBlackPawn, tempWhiteQueenCastle, tempWhiteKingCastle, tempBlackQueenCastle, tempBlackKingCastle, not whiteTurn, depth + 1) score = -score if (score > alpha and score < beta): """ # original algorithm score,bestMove=-self.principalVariationSearch(-beta,-score,tempHistory,tempWhiteKing,tempWhiteQueen,tempWhiteBishop,tempWhiteKnight,tempWhiteRook,tempWhitePawn,tempBlackKing,tempBlackQueen,tempBlackBishop,tempBlackKnight,tempBlackRook,tempBlackPawn,tempWhiteQueenCastle,tempWhiteKingCastle,tempBlackQueenCastle,tempBlackKingCastle,not whiteTurn,depth+1) """ #alternate way of writing to original algorithm score, bestMove = self.principalVariationSearch( -beta, -score, tempHistory, tempWhiteKing, tempWhiteQueen, tempWhiteBishop, tempWhiteKnight, tempWhiteRook, tempWhitePawn, tempBlackKing, tempBlackQueen, tempBlackBishop, tempBlackKnight, tempBlackRook, tempBlackPawn, tempWhiteQueenCastle, tempWhiteKingCastle, tempBlackQueenCastle, tempBlackKingCastle, not whiteTurn, depth + 1) score = -score ##For debugging # print("researched") if (score > bestScore): bestMoveIndex = i bestScore = score alpha = self.max(alpha, score) if (alpha >= beta): return alpha, moves[bestMoveIndex] b = alpha + 1 return bestScore, moves[bestMoveIndex]
def getAllCurrentLegalMoves(self, whiteTurn): Moves = self.Moves if (whiteTurn): moves = Moves.white_legalMoves( self.history, self.whiteKing, self.whiteQueen, self.whiteBishop, self.whiteKnight, self.whiteRook, self.whitePawn, self.blackKing, self.blackQueen, self.blackBishop, self.blackKnight, self.blackRook, self.blackPawn, self.whiteQueenCastle, self.whiteKingCastle, self.blackQueenCastle, self.blackKingCastle) else: moves = Moves.black_legalMoves( self.history, self.whiteKing, self.whiteQueen, self.whiteBishop, self.whiteKnight, self.whiteRook, self.whitePawn, self.blackKing, self.blackQueen, self.blackBishop, self.blackKnight, self.blackRook, self.blackPawn, self.whiteQueenCastle, self.whiteKingCastle, self.blackQueenCastle, self.blackKingCastle) moves = moves.split() li = [] for i in range(len(moves)): currentMove = moves[i] #white pieces tempWhiteKing = Moves.makeMove(self.whiteKing, currentMove, "K") tempWhiteQueen = Moves.makeMove(self.whiteQueen, currentMove, "Q") tempWhiteBishop = Moves.makeMove(self.whiteBishop, currentMove, "B") tempWhiteKnight = Moves.makeMove(self.whiteKnight, currentMove, "H") tempWhiteRook = Moves.makeMove(self.whiteRook, currentMove, "R") tempWhitePawn = Moves.makeMove(self.whitePawn, currentMove, "P") #if castling, make castling move if ("WL" in currentMove or "WR" in currentMove): tempWhiteKing, tempWhiteRook = Moves.makeCastlingMove( self.whiteKing, self.whiteRook, currentMove) #black pieces tempBlackKing = Moves.makeMove(self.blackKing, currentMove, "k") tempBlackQueen = Moves.makeMove(self.blackQueen, currentMove, "q") tempBlackBishop = Moves.makeMove(self.blackBishop, currentMove, "b") tempBlackKnight = Moves.makeMove(self.blackKnight, currentMove, "h") tempBlackRook = Moves.makeMove(self.blackRook, currentMove, "r") tempBlackPawn = Moves.makeMove(self.blackPawn, currentMove, "p") if ("BL" in currentMove or "BR" in currentMove): tempBlackKing, tempBlackRook = Moves.makeCastlingMove( self.blackKing, self.blackRook, currentMove) tempHistory = currentMove if (whiteTurn): if ((tempWhiteKing & Moves.whiteKing_illegalMoves( tempWhiteKing, tempWhiteQueen, tempWhiteBishop, tempWhiteKnight, tempWhiteRook, tempWhitePawn, tempBlackKing, tempBlackQueen, tempBlackBishop, tempBlackKnight, tempBlackRook, tempBlackPawn)) == 0): li.append(currentMove) elif (not whiteTurn): if ((tempBlackKing & Moves.blackKing_illegalMoves( tempWhiteKing, tempWhiteQueen, tempWhiteBishop, tempWhiteKnight, tempWhiteRook, tempWhitePawn, tempBlackKing, tempBlackQueen, tempBlackBishop, tempBlackKnight, tempBlackRook, tempBlackPawn)) == 0): li.append(currentMove) self.currentAllLegalMoves = li