internal static void GetCastleMoves(byte color, Board board, Move[] moveList, ref int pos) { if (color == PieceColor.White) { if (board.WhiteCanCastleOO) { if ((Constants.WhiteCastleMaskOO & board.OccupiedSquares) == 0) moveList[pos++] = Constants.WhiteCastlingOO; } if (board.WhiteCanCastleOOO) { if ((Constants.WhiteCastleMaskOOO & board.OccupiedSquares) == 0) moveList[pos++] = Constants.WhiteCastlingOOO; } } else if (color == PieceColor.Black) { if (board.BlackCanCastleOO) { if ((Constants.BlackCastleMaskOO & board.OccupiedSquares) == 0) moveList[pos++] = Constants.BlackCastlingOO; } if (board.BlackCanCastleOOO) { if ((Constants.BlackCastleMaskOOO & board.OccupiedSquares) == 0) moveList[pos++] = Constants.BlackCastlingOOO; } } }
internal static void GetAllMoves(byte color, Move[] allMoves, ref int pos, Board board) { GetPawnMoves(color, board.GetPieceSet(color, PieceType.Pawn), board, allMoves, ref pos); GetKnightMoves(color, board.GetPieceSet(color, PieceType.Knight), board, allMoves, ref pos); GetKingMoves(color, board.GetPieceSet(color, PieceType.King), board, allMoves, ref pos); GetBishopMoves(color, board.GetPieceSet(color, PieceType.Bishop), board, allMoves, ref pos); GetRookMoves(color, board.GetPieceSet(color, PieceType.Rook), board, allMoves, ref pos); GetQueenMoves(color, board.GetPieceSet(color, PieceType.Queen), board, allMoves, ref pos); GetCastleMoves(color, board, allMoves, ref pos); }
internal static void GetKingMoves(byte color, BitBoard king, Board board, Move[] moveList, ref int pos) { BitBoard targets; byte fromIndex; byte toIndex; while (king != 0) { fromIndex = (byte)BitBoard.BitScanForwardReset(ref king); // search for LS1B and then reset it targets = King.GetAllTargets(color, Constants.SquareMask[fromIndex], board); while (targets != 0) { toIndex = (byte)BitBoard.BitScanForwardReset(ref targets); // search for LS1B and then reset it moveList[pos++] = new Move(fromIndex, toIndex, PieceType.King, board.pieceSet[toIndex].Type, PieceType.None); } } }
internal void UndoMove(Move move) { //ARRAY this.pieceSet[move.FromSquare] = this.pieceSet[move.ToSquare]; // muove il pezzo this.pieceSet[move.ToSquare] = move.IsCapture() ? new Piece(SideToMove, move.PieceCaptured) : new Piece(PieceColor.None, PieceType.None); // svuota o riempie la casella di partenza //BITBOARDS BitBoard From = Constants.SquareMask[move.FromSquare]; BitBoard To = Constants.SquareMask[move.ToSquare]; BitBoard FromTo = From | To; this.SideToMove = this.SideToMove.GetOpposite(); // aggiorna la bitboard this.bitBoardSet[this.SideToMove][move.PieceMoved] ^= FromTo; if (this.SideToMove == PieceColor.White) this.WhitePieces ^= FromTo; else this.BlackPieces ^= FromTo; if (move.PieceMoved == PieceType.King) this.kingSquare[this.SideToMove] = move.FromSquare; if (move.IsCapture()) { this.bitBoardSet[this.SideToMove.GetOpposite()][move.PieceCaptured] ^= To; //aggiorna i pezzi dell'avversario if (this.SideToMove == PieceColor.White) this.BlackPieces ^= To; else this.WhitePieces ^= To; this.OccupiedSquares ^= From; this.EmptySquares ^= From; } else { this.OccupiedSquares ^= FromTo; this.EmptySquares ^= FromTo; } }
internal bool IsMoveLegal(Move move, BitBoard pinned) { if (move.PieceMoved == PieceType.King) return !this.IsAttacked(Constants.SquareMask[move.ToSquare], this.SideToMove); if (this.IsAttacked(this.bitBoardSet[SideToMove][PieceType.King], this.SideToMove)) { bool islegal = true; this.MakeMove(move); islegal = !this.IsAttacked(this.bitBoardSet[SideToMove.GetOpposite()][PieceType.King], this.SideToMove.GetOpposite()); this.UndoMove(move); return islegal; } return (pinned == 0) || ((pinned & Constants.SquareMask[move.FromSquare]) == 0) || MovePackHelper.AreSquareAligned(move.FromSquare, move.ToSquare, this.kingSquare[this.SideToMove]); }
internal static void GetLegalMoves(byte color, Move[] allMoves, ref int pos, Board board) { BitBoard pinned = board.GetPinnedPieces(); GetAllMoves(color, allMoves, ref pos, board); int last = pos; int cur = 0; while (cur != last) { if (!board.IsMoveLegal(allMoves[cur], pinned)) { allMoves[cur] = allMoves[--last]; } else { cur++; } } pos = last; }
internal static void GetPawnMoves(byte color, BitBoard pawns, Board board, Move[] moveList, ref int pos) { BitBoard targets; BitBoard epTargets; byte fromIndex; byte toIndex; while (pawns != 0) { fromIndex = (byte)BitBoard.BitScanForwardReset(ref pawns); // search for LS1B and then reset it targets = Pawn.GetAllTargets(color, Constants.SquareMask[fromIndex], board); while (targets != 0) { toIndex = (byte)BitBoard.BitScanForwardReset(ref targets); // search for LS1B and then reset it // en passant if (board.EnPassantSquare != Square.Invalid) { epTargets = color == PieceColor.White ? MovePackHelper.WhitePawnAttacks[fromIndex] : MovePackHelper.BlackPawnAttacks[fromIndex]; if ((epTargets & Constants.SquareMask[board.EnPassantSquare]) != 0) moveList[pos++] = new Move(fromIndex, toIndex, PieceType.Pawn, PieceType.Pawn, PieceType.Pawn); } // promotions if (Square.GetRankIndex(toIndex) == 7 && color == PieceColor.White || Square.GetRankIndex(toIndex) == 0 && color == PieceColor.Black) { moveList[pos++] = new Move(fromIndex, toIndex, PieceType.Pawn, board.pieceSet[toIndex].Type, PieceType.Queen); moveList[pos++] = new Move(fromIndex, toIndex, PieceType.Pawn, board.pieceSet[toIndex].Type, PieceType.Rook); moveList[pos++] = new Move(fromIndex, toIndex, PieceType.Pawn, board.pieceSet[toIndex].Type, PieceType.Bishop); moveList[pos++] = new Move(fromIndex, toIndex, PieceType.Pawn, board.pieceSet[toIndex].Type, PieceType.Knight); } else moveList[pos++] = new Move(fromIndex, toIndex, PieceType.Pawn, board.pieceSet[toIndex].Type, PieceType.None); // no promotions } } }