bool isInCheck(ToMoveData thisSide, ToMoveData otherSide) { Square sq = thisSide.KingSqInfo.Location; return isSquareAttacked(sq, otherSide); }
bool isSquareAttacked(Square sq, ToMoveData enemySide) { int file = sq.File; int rank = sq.Rank; Piece enemyKnight = enemySide.Knight; if (checkForEnemyKnightAt(file + 1, rank + 2, enemyKnight) || checkForEnemyKnightAt(file + 2, rank + 1, enemyKnight) || checkForEnemyKnightAt(file + 2, rank - 1, enemyKnight) || checkForEnemyKnightAt(file + 1, rank - 2, enemyKnight) || checkForEnemyKnightAt(file - 1, rank - 2, enemyKnight) || checkForEnemyKnightAt(file - 2, rank - 1, enemyKnight) || checkForEnemyKnightAt(file - 2, rank + 1, enemyKnight) || checkForEnemyKnightAt(file - 1, rank + 2, enemyKnight)) return true; Piece enemyPawn = enemySide.Pawn; if (checkForEnemyPawnAt(file - 1, rank - enemySide.PawnYDir, enemyPawn) || checkForEnemyPawnAt(file + 1, rank - enemySide.PawnYDir, enemyPawn)) return true; Piece enemyBishop = enemySide.Bishop; Piece enemyRook = enemySide.Rook; Piece enemyQueen = enemySide.Queen; if ((checkForSlidingAttacksFrom(file, rank, 1, 1, enemyBishop, enemyQueen)) || (checkForSlidingAttacksFrom(file, rank, 1, -1, enemyBishop, enemyQueen)) || (checkForSlidingAttacksFrom(file, rank, -1, -1, enemyBishop, enemyQueen)) || (checkForSlidingAttacksFrom(file, rank, -1, 1, enemyBishop, enemyQueen)) || (checkForSlidingAttacksFrom(file, rank, 0, 1, enemyRook, enemyQueen)) || (checkForSlidingAttacksFrom(file, rank, 1, 0, enemyRook, enemyQueen)) || (checkForSlidingAttacksFrom(file, rank, 0, -1, enemyRook, enemyQueen)) || (checkForSlidingAttacksFrom(file, rank, -1, 0, enemyRook, enemyQueen))) return true; Square enemyKingSq = enemySide.KingSqInfo.Location; int fileDiff = file - enemyKingSq.File; int rankDiff = rank - enemyKingSq.Rank; if (fileDiff >= -1 && fileDiff <= 1 && rankDiff >= -1 && rankDiff <= 1) return true; return false; }
public ChessPosition() { initChessEval(); Board = new SquareInfo[64]; emptyBoard = new SquareInfo[64]; Princ = new ChessMove[maxDepth, maxDepth]; killers = new ChessMove[2, 1000]; mvvLva = new int[8, 8]; searchHistory = new int[16, 64]; // Init the mvvLva array for (int v = 0; v <= 6; v++) for (int a = 0; a <= 6; a++) mvvLva[v, a] = v * 100 +6 - a; // Init the killers array for (int i = 0; i < 1000; i++) { killers[0, i] = new ChessMove(new Square(99, 99), new Square(99, 99)); killers[1, i] = new ChessMove(new Square(99, 99), new Square(99, 99)); } // Init the emptyBoard and Board arrays for (int i = 0; i <= 63; i++) { emptyBoard[i] = new SquareInfo(Pieces.None, new Square(i)); Board[i] = emptyBoard[i]; } // Init the hashKeys Random rng = new Random(); pieceKeys = new UInt64[16, 64]; for (int p = 0; p <= 15; p++) for (int sq = 0; sq <= 63; sq++) pieceKeys[p, sq] = (UInt64)rng.Next() + ((UInt64)rng.Next() << 15) + ((UInt64)rng.Next() << 30) + ((UInt64)rng.Next() << 45) + ((UInt64)rng.Next() << 60); castleKeys = new UInt64[16]; for (int i = 0; i <= 15; i++) castleKeys[i] = (UInt64)rng.Next() + ((UInt64)rng.Next() << 15) + ((UInt64)rng.Next() << 30) + ((UInt64)rng.Next() << 45) + ((UInt64)rng.Next() << 60); sideKey = (UInt64)rng.Next() + ((UInt64)rng.Next() << 15) + ((UInt64)rng.Next() << 30) + ((UInt64)rng.Next() << 45) + ((UInt64)rng.Next() << 60); // Init the pvTable pvTable = new Dictionary<ulong, int>(); EpSquare = new Square(0, 0); white = new ToMoveData(PieceColour.White, new Piece(PieceType.Pawn, PieceColour.White), new Piece(PieceType.Knight, PieceColour.White), new Piece(PieceType.Bishop, PieceColour.White), new Piece(PieceType.Rook, PieceColour.White), new Piece(PieceType.Queen, PieceColour.White), new Piece(PieceType.King, PieceColour.White), 1, 7, 1, CastleFlags.Wks, CastleFlags.Wqs, new Square("e1"), new Square("h1"), new Square("a1")); black = new ToMoveData(PieceColour.Black, new Piece(PieceType.Pawn, PieceColour.Black), new Piece(PieceType.Knight, PieceColour.Black), new Piece(PieceType.Bishop, PieceColour.Black), new Piece(PieceType.Rook, PieceColour.Black), new Piece(PieceType.Queen, PieceColour.Black), new Piece(PieceType.King, PieceColour.Black), 6, 0, -1, CastleFlags.Bks, CastleFlags.Bqs, new Square("e8"), new Square("h8"), new Square("a8")); PossibleMoves = new Collection<ChessMove>(); MoveHistory = new Collection<ChessMove>(); undoStack = new Stack<ChessMove>(); NewGame(); }