// TODO: Add Validation public static Move FromLongAlgebraic(Position position, string move) { int[] squares = position.GetPieceSquares(); int originSquare = Bitboards.GetSquareFromName(move.Substring(0, 2)); int destinationSquare = Bitboards.GetSquareFromName(move.Substring(2, 2)); int capturePiece = squares[destinationSquare]; int movedPiece = squares[originSquare]; // Castle Moves if (movedPiece == Position.KING && Bitboards.SquareDistance[originSquare, destinationSquare] > 1) { int castleDirection = Bitboards.GetColumn(destinationSquare) == 6 ? Position.KING : Position.QUEEN; return(new Move(originSquare, destinationSquare, movedPiece, false, true, castleDirection)); } // En Passent and Promotion if (movedPiece == Position.PAWN) { // En Passent if (Bitboards.GetColumn(originSquare) != Bitboards.GetColumn(destinationSquare) && capturePiece == Position.EMPTY) { return(new Move(originSquare, destinationSquare, movedPiece, true, false)); } else if (move.Length > 4) { const string promotionMap = "-qrbn"; int promotionPiece = promotionMap.IndexOf(move[4]); return(new Move(originSquare, destinationSquare, movedPiece, capturePiece, promotionPiece)); } } // Everything else return(new Move(originSquare, destinationSquare, movedPiece, capturePiece)); }
public static int[] GenerateOffsets(Bitboard bitboard) { int[] result = new int[Bitboards.CountBits(bitboard)]; int cnt = 0; while (bitboard != 0) { result[cnt++] = Bitboards.BitScanForward(bitboard); Bitboards.PopLsb(ref bitboard); } return(result); }
public override string ToString() { string pieceStr = "KQRBNP"; StringBuilder sb = new StringBuilder(); sb.Append(Bitboards.GetNameFromSquare(OriginSquare())); sb.Append(Bitboards.GetNameFromSquare(DestinationSquare())); if (PromotionType() != Position.EMPTY) { sb.Append('='); sb.Append(pieceStr[PromotionType()]); } return(sb.ToString()); }
public static Bitboard[] GeneratePermutations(Bitboard occupancyBitboard) { int numBits = Bitboards.CountBits(occupancyBitboard); int numPossibilities = (int)Math.Pow(2, numBits); int[] offsets = GenerateOffsets(occupancyBitboard); Bitboard[] result = new Bitboard[numPossibilities]; for (int i = 0; i < numPossibilities; i++) { for (int bit = 0; bit < numBits; bit++) { bool bitIsSet = (i & (1 << bit)) != 0; if (bitIsSet) { result[i] |= Bitboards.SquareBitboards[offsets[bit]]; } } } return(result); }