public static void AddQuietMove(Board.Board board, int move, MoveList list) { Debug.Assert(Validators.SqOnBoard(MoveOperations.FromSq(move))); Debug.Assert(Validators.SqOnBoard(MoveOperations.ToSq(move))); list.Moves[list.Count].Move = move; /* Captures generate 1.000.000 or higher score, therefore we have to * score the move just below this, to ensure this is second highest priority for search * after capture moves. */ if (board.SearchKillers[0, board.Ply] == move) { list.Moves[list.Count].Score = 900000; } else if (board.SearchKillers[1, board.Ply] == move) { list.Moves[list.Count].Score = 800000; } else { int from = MoveOperations.FromSq(move); int to = MoveOperations.ToSq(move); list.Moves[list.Count].Score = board.SearchHistory[board[from], to]; } list.Count++; }
// Capture move private static void AddWhitePawnCapMove(Board.Board board, int from, int to, int capt, MoveList list) { Debug.Assert(Validators.PieceValidEmpty(capt), String.Format("Invalid capture square {0}", capt)); Debug.Assert(Validators.SqOnBoard(from), String.Format("Invalid from square {0}", from)); Debug.Assert(Validators.SqOnBoard(to), String.Format("Invalid to square {0}", to)); // If White pawn promotion. if (Conversion.getRanksBrd(from) == (int)Rank.RANK_7) { MoveGen.AddCaptureMove(board, MoveOperations.CreateMove(from, to, capt, (int)Piece.wQ, 0), list); MoveGen.AddCaptureMove(board, MoveOperations.CreateMove(from, to, capt, (int)Piece.wR, 0), list); MoveGen.AddCaptureMove(board, MoveOperations.CreateMove(from, to, capt, (int)Piece.wB, 0), list); MoveGen.AddCaptureMove(board, MoveOperations.CreateMove(from, to, capt, (int)Piece.wN, 0), list); } else { MoveGen.AddCaptureMove(board, MoveOperations.CreateMove(from, to, capt, (int)Piece.EMPTY, 0), list); } }
public static void AddEnPassantMove(Board.Board board, int move, MoveList list) { Debug.Assert(Validators.SqOnBoard(MoveOperations.FromSq(move))); Debug.Assert(Validators.SqOnBoard(MoveOperations.ToSq(move))); list.Moves[list.Count].Move = move; list.Moves[list.Count].Score = 105 + 1000000; list.Count++; }
public static void AddCaptureMove(Board.Board board, int move, MoveList list) { Debug.Assert(Validators.SqOnBoard(MoveOperations.FromSq(move))); Debug.Assert(Validators.SqOnBoard(MoveOperations.ToSq(move))); Debug.Assert(Validators.PieceValidEmpty(MoveOperations.Captured(move)), String.Format("Invalid piece {0} was captured", MoveOperations.Captured(move))); var captured = MoveOperations.Captured(move); var attacker = board[MoveOperations.FromSq(move)]; list.Moves[list.Count].Move = move; list.Moves[list.Count].Score = MvvLva.Scores[captured, attacker] + 1000000; list.Count++; }
public static void TakeMove(Board.Board board) { Debug.Assert(BoardOperations.CheckBoard(board)); // Decrement the plys. board.HistoryPly--; board.Ply--; int move = board.History[board.HistoryPly].Move; int from = MoveOperations.FromSq(move); int to = MoveOperations.ToSq(move); Debug.Assert(Validators.SqOnBoard(from), String.Format("Invalid From Square {0}", Io.SqToString(from))); Debug.Assert(Validators.SqOnBoard(to), String.Format("Invalid To Square {0}", Io.SqToString(to))); // If there is an EnPas square, hash it out. if (board.EnPas != (int)Square.NO_SQ) { HashEnPas(board); } HashCastle(board); // Reverse relevant board structures. board.CastlePerm = board.History[board.HistoryPly].CastlePerm; board.FiftyMoves = board.History[board.HistoryPly].FiftyMoves; board.EnPas = board.History[board.HistoryPly].EnPas; // If the previous move have an EnPas square set, we need to hash it back in if (board.EnPas != (int)Square.NO_SQ) { HashEnPas(board); } // Hash the castling back incase it's changed. HashCastle(board); // Change the side to move back. board.Side ^= 1; HashSide(board); if ((move & MoveOperations.MoveFlagEnPas) != 0) { if (board.Side == (int)Colour.WHITE) // If EnPas move we need to add the pawn { AddPiece(to - 10, board, (int)Piece.bP); } else // Else black { AddPiece(to + 10, board, (int)Piece.wP); } } else if ((move & MoveOperations.MoveFlagCastle) != 0) { // If castle we need to move the rook. switch (to) { case (int)Square.C1: MovePiece((int)Square.D1, (int)Square.A1, board); break; case (int)Square.C8: MovePiece((int)Square.D8, (int)Square.A8, board); break; case (int)Square.G1: MovePiece((int)Square.F1, (int)Square.H1, board); break; case (int)Square.G8: MovePiece((int)Square.F8, (int)Square.H8, board); break; default: throw new Exception( "The move is a castling move, but the to square doesn't match any eligible moves"); } } // Move the piece back to the original square. MovePiece(to, from, board); if (Data.IsPieceKing(board[from])) { board.KingSq[board.Side] = from; } int captured = MoveOperations.Captured(move); if (captured != (int)Piece.EMPTY) { Debug.Assert(Validators.PieceValid(captured), "Invalid piece captured"); AddPiece(to, board, captured); } int promotedPce = MoveOperations.Promoted(move); if (promotedPce != (int)Piece.EMPTY) // If there is a promoted piece // A piece cannot be promoted to a pawn. { Debug.Assert((Validators.PieceValid(promotedPce) || !Data.IsPiecePawn(promotedPce)), String.Format("Promoted piece {0} is invalid. PieceValid returned {1}, and IsPiecePawn returned {2}", (Piece)promotedPce, Validators.PieceValid(promotedPce), Data.IsPiecePawn(promotedPce))); ClearPiece(from, board); AddPiece(from, board, ((Data.PIECE_COLOURS[promotedPce] == Colour.WHITE) ? (int)Piece.wP : (int)Piece.bP)); } Debug.Assert(BoardOperations.CheckBoard(board)); }
// Possibly not static // Returns true if legal move, or false otherwise /// <summary> /// Makes the input move on the board, if the move is legal. /// If illegal, nothing happens and the method returns false. /// </summary> /// <param name="board"> The board to make the move on </param> /// <param name="move"> The move to perform </param> public static bool Make_Move(Board.Board board, int move) { Debug.Assert(BoardOperations.CheckBoard(board), "The CheckBoard method returned false."); int from = MoveOperations.FromSq(move); int to = MoveOperations.ToSq(move); int side = board.Side; Debug.Assert(Validators.SqOnBoard(from), String.Format("Invalid From Square {0}", Io.SqToString(from))); Debug.Assert(Validators.SqOnBoard(to), String.Format("Invalid To Square {0}", Io.SqToString(to))); Debug.Assert(Validators.SideValid(side), String.Format("Invalid Side with integer value {0}", side)); Debug.Assert(Validators.PieceValid(board[from]), String.Format("Invalid From Square {0}", Io.SqToString(from))); board.History[board.HistoryPly].PosKey = board.PosKey; // Now we check for special moves. if ((move & MoveOperations.MoveFlagEnPas) != 0) { if (side == (int)Colour.WHITE) // If EnPas move we need to kill the pawn { ClearPiece(to - 10, board); } else // Else black { ClearPiece(to + 10, board); } } else if ((move & MoveOperations.MoveFlagCastle) != 0) { // If castle we need to move the rook. switch (to) { case (int)Square.C1: MovePiece((int)Square.A1, (int)Square.D1, board); break; case (int)Square.C8: MovePiece((int)Square.A8, (int)Square.D8, board); break; case (int)Square.G1: MovePiece((int)Square.H1, (int)Square.F1, board); break; case (int)Square.G8: MovePiece((int)Square.H8, (int)Square.F8, board); break; default: throw new Exception( "The move is a castling move, but the to square doesn't match any eligible moves"); } } if (board.EnPas != (int)Square.NO_SQ) { MakeMove.HashEnPas(board); } MakeMove.HashCastle(board); // POSSIBLE ERROR, MAYBE IT SHOULD BE IN IF STATEMENT ABOVE. // Store information in history array. board.History[board.HistoryPly].Move = move; board.History[board.HistoryPly].EnPas = board.EnPas; board.History[board.HistoryPly].CastlePerm = board.CastlePerm; board.History[board.HistoryPly].FiftyMoves = board.FiftyMoves; // Adjust castle permissions if rook or king has moved. board.CastlePerm &= MakeMove.castlePerm[from]; board.CastlePerm &= MakeMove.castlePerm[to]; board.EnPas = (int)Square.NO_SQ; HashCastle(board); int captured = MoveOperations.Captured(move); board.FiftyMoves++; // If there is a piece captured if (captured != (int)Piece.EMPTY) { // Test if the piece is valid. Debug.Assert(Validators.PieceValid(captured), String.Format("Invalid piece ID integer {0}", captured)); ClearPiece(to, board); board.FiftyMoves = 0; // reset 50moves, cuz a piece has been captured. } board.HistoryPly++; board.Ply++; if (Data.IsPiecePawn(board[from])) { board.FiftyMoves = 0; // Reset 50moves, cuz a pawn has moved. if ((move & MoveOperations.MoveFlagPawnStart) != 0) { if (side == (int)Colour.WHITE) { board.EnPas = from + 10; Debug.Assert(Conversion.getRanksBrd(board.EnPas) == (int)Rank.RANK_3, String.Format("Invalid board state: the enPas square {0} is wrong", Io.SqToString(board.EnPas))); } else { board.EnPas = from - 10; Debug.Assert(Conversion.getRanksBrd(board.EnPas) == (int)Rank.RANK_6, String.Format("Invalid board state: the enPas square {0} is wrong", Io.SqToString(board.EnPas))); } HashEnPas(board); } } MovePiece(from, to, board); int promotedPce = MoveOperations.Promoted(move); if (promotedPce != (int)Piece.EMPTY) // If there is a promoted piece // A piece cannot be promoted to a pawn. { Debug.Assert(Validators.PieceValid(promotedPce) && !Data.IsPiecePawn(promotedPce), String.Format("Promoted piece {0} is invalid", (Piece)promotedPce)); ClearPiece(to, board); AddPiece(to, board, promotedPce); } if (Data.IsPieceKing(board[to])) { // If piece is king set the KingSQ board.KingSq[side] = to; } // Exclusive or, changes the side from white to black or black to white. board.Side ^= 1; HashSide(board); Debug.Assert(BoardOperations.CheckBoard(board)); // Make sure that the new king square isn't attacked. if (Attack.IsSqAttacked(board.KingSq[side], board.Side, board)) { TakeMove(board); return(false); } return(true); }
/// <summary> /// Generates all available moves for the current position on the board in the parameter MoveList. /// If the onlyCaptures parameter is true, it will only generate capture moves. /// </summary> /// <param name="board"> The current board position </param> /// <param name="list"> The MoveList to parse the available moves to. </param> /// <param name="onlyCaptures"> True if only generating for captures. </param> public static void GenerateAllMoves(Board.Board board, MoveList list, bool onlyCaptures) { Debug.Assert(BoardOperations.CheckBoard(board)); list.Count = 0; var side = (Colour)board.Side; var piece = side == Colour.WHITE ? (int)Piece.wP : (int)Piece.bP; for (var pceNum = 0; pceNum < board.PceNum[piece]; ++pceNum) { var sq = board.PList[piece, pceNum]; Debug.Assert(Validators.IsSq(sq), String.Format("Sq {0} is invalid", sq)); var leftCaptureSq = side == Colour.WHITE ? sq + 9 : sq - 9; var rightCaptureSq = side == Colour.WHITE ? sq + 11 : sq - 11; if (!onlyCaptures) { var sqInfront = side == Colour.WHITE ? sq + 10 : sq - 10; if (board[sqInfront] == (int)Piece.EMPTY && Validators.SqOnBoard(sqInfront)) { if (side == Colour.WHITE) { MoveGen.AddWhitePawnMove(board, sq, sqInfront, list); // Check if the number two square infront of the pawn is empty and that the rank is 2. if (Conversion.getRanksBrd(sq) == (int)Rank.RANK_2 && board[sq + 20] == (int)Piece.EMPTY) { MoveGen.AddQuietMove(board, MoveOperations.CreateMove(sq, sq + 20, (int)Piece.EMPTY, (int)Piece.EMPTY, MoveOperations.MoveFlagPawnStart), list); } } else // BLACK { MoveGen.AddBlackPawnMove(board, sq, sqInfront, list); // Check if the number two square infront of the pawn is empty and that the rank is 7. if (Conversion.getRanksBrd(sq) == (int)Rank.RANK_7 && board[sq - 20] == (int)Piece.EMPTY) { MoveGen.AddQuietMove(board, MoveOperations.CreateMove(sq, sq - 20, (int)Piece.EMPTY, (int)Piece.EMPTY, MoveOperations.MoveFlagPawnStart), list); } } } } if (Validators.IsSq(leftCaptureSq) && Data.PIECE_COLOURS[board[leftCaptureSq]] == (Colour)((int)side ^ 1)) { if (side == Colour.WHITE) { MoveGen.AddWhitePawnCapMove(board, sq, leftCaptureSq, board[leftCaptureSq], list); } else { // BLACK MoveGen.AddBlackPawnCapMove(board, sq, leftCaptureSq, board[leftCaptureSq], list); } } if (Validators.IsSq(rightCaptureSq) && Data.PIECE_COLOURS[board[rightCaptureSq]] == (Colour)((int)side ^ 1)) { if (side == Colour.WHITE) { MoveGen.AddWhitePawnCapMove(board, sq, rightCaptureSq, board[rightCaptureSq], list); } else { // BLACK MoveGen.AddBlackPawnCapMove(board, sq, rightCaptureSq, board[rightCaptureSq], list); } } if (Validators.IsSq(board.EnPas)) { if (leftCaptureSq == board.EnPas) { MoveGen.AddEnPassantMove(board, MoveOperations.CreateMove(sq, leftCaptureSq, (int)Piece.EMPTY , (int)Piece.EMPTY, MoveOperations.MoveFlagEnPas), list); } else if (rightCaptureSq == board.EnPas) { MoveGen.AddEnPassantMove(board, MoveOperations.CreateMove(sq, rightCaptureSq, (int)Piece.EMPTY , (int)Piece.EMPTY, MoveOperations.MoveFlagEnPas), list); } } } // Castling if (!onlyCaptures) { if (side == Colour.WHITE) { // If there is White King Castle Permission. if ((board.CastlePerm & (int)Castling.WKCA) != 0) { // Check if squares are empty before we call the less efficient SqAttacked method. var isSqG1F1Empty = board[(int)Square.F1] == (int)Piece.EMPTY && board[ (int)Square.G1] == (int)Piece.EMPTY; if (isSqG1F1Empty) { var IsSqE1OrF1NotAtt = !Attack.IsSqAttacked((int)Square.F1, (int)Colour.BLACK, board) && !Attack.IsSqAttacked((int)Square.E1, (int)Colour.BLACK, board); if (IsSqE1OrF1NotAtt) { MoveGen.AddQuietMove(board, MoveOperations.CreateMove((int)Square.E1, (int)Square.G1, (int)Piece.EMPTY, (int)Piece.EMPTY, MoveOperations.MoveFlagCastle), list); } } } // If there is White Queen Castle Permission. if ((board.CastlePerm & (int)Castling.WQCA) != 0) { var isSqB1C1D1Empty = board[(int)Square.B1] == (int)Piece.EMPTY && board[(int)Square.C1] == (int)Piece.EMPTY && board[(int)Square.D1] == (int)Piece.EMPTY; if (isSqB1C1D1Empty) { var isSqE1OrD1NotAtt = !Attack.IsSqAttacked((int)Square.E1, (int)Colour.BLACK, board) && !Attack.IsSqAttacked((int)Square.D1, (int)Colour.BLACK, board); if (isSqE1OrD1NotAtt) { MoveGen.AddQuietMove(board, MoveOperations.CreateMove((int)Square.E1, (int)Square.C1, (int)Piece.EMPTY, (int)Piece.EMPTY, MoveOperations.MoveFlagCastle), list); } } } } else { // If there is Black King Castle Permission. if ((board.CastlePerm & (int)Castling.BKCA) != 0) { // Check if squares are empty before we call the less efficient SqAttacked method. var isSqG8F8Empty = board[(int)Square.F8] == (int)Piece.EMPTY && board[(int)Square.G8] == (int)Piece.EMPTY; if (isSqG8F8Empty) { var isSqE8F8NotAttacked = !Attack.IsSqAttacked((int)Square.E8, (int)Colour.WHITE, board) && !Attack.IsSqAttacked((int)Square.F8, (int)Colour.WHITE, board); if (isSqE8F8NotAttacked) { MoveGen.AddQuietMove(board, MoveOperations.CreateMove((int)Square.E8, (int)Square.G8, (int)Piece.EMPTY, (int)Piece.EMPTY, MoveOperations.MoveFlagCastle), list); } } } // If there is White Queen Castle Permission. if ((board.CastlePerm & (int)Castling.BQCA) != 0) { // Check if squares are empty before we call the less efficient SqAttacked method. var isSqB8C8D8Empty = board[(int)Square.B8] == (int)Piece.EMPTY && board[(int)Square.C8] == (int)Piece.EMPTY && board[(int)Square.D8] == (int)Piece.EMPTY; if (isSqB8C8D8Empty) { var isSqE8D8NotAttacked = !Attack.IsSqAttacked((int)Square.E8, (int)Colour.WHITE, board) && !Attack.IsSqAttacked((int)Square.D8, (int)Colour.WHITE, board); if (isSqE8D8NotAttacked) { MoveGen.AddQuietMove(board, MoveOperations.CreateMove((int)Square.E8, (int)Square.C8, (int)Piece.EMPTY, (int)Piece.EMPTY, MoveOperations.MoveFlagCastle), list); } } } } } // Loop sliding pieces var pceIndex = MoveGen.LOOP_SLIDE_INDEX[board.Side]; // Increments AFTER it's run var pce = MoveGen.LOOP_SLIDE_PCE[pceIndex++]; while (pce != 0) { Debug.Assert(Validators.PieceValid(pce), String.Format("You have reached an invalid sliding piece {0}", pce)); for (var pceNum = 0; pceNum < board.PceNum[pce]; ++pceNum) { var sq = board.PList[pce, pceNum]; // Square check Debug.Assert(Validators.IsSq(sq), String.Format("Sq {0} is false", sq)); for (var index = 0; index < MoveGen.NUM_DIR[pce]; ++index) { var t_sq = sq + MoveGen.PCE_DIR[pce, index]; // Check if offboard if (!Validators.SqOnBoard(t_sq)) { continue; } while (Validators.SqOnBoard(t_sq)) { if (board[t_sq] != (int)Piece.EMPTY) { // 0 EXCLUSIVE OR 1 returns 1 = COLOUR.WHITE // 1 EXCLUSIVE OR 1 returns 0 = COLOUR.BLACK if ((int)Data.PIECE_COLOURS[board[t_sq]] == (board.Side ^ 1)) { AddCaptureMove(board, MoveOperations.CreateMove(sq, t_sq, board[t_sq], (int)Piece.EMPTY, 0), list); } break; } if (!onlyCaptures) { AddQuietMove(board, MoveOperations.CreateMove(sq, t_sq, (int)Piece.EMPTY, (int)Piece.EMPTY, 0), list); } t_sq += MoveGen.PCE_DIR[pce, index]; } } } pce = MoveGen.LOOP_SLIDE_PCE[pceIndex++]; } // Loop non_sliding pieces pceIndex = MoveGen.LOOP_NON_SLIDE_INDEX[board.Side]; pce = MoveGen.LOOP_NON_SLIDE_PCE[pceIndex++]; while (pce != 0) { Debug.Assert(Validators.PieceValid(pce), String.Format("You have reached an invalid sliding piece {0}", pce)); for (var pceNum = 0; pceNum < board.PceNum[pce]; ++pceNum) { var sq = board.PList[pce, pceNum]; // Square check Debug.Assert(Validators.IsSq(sq), String.Format("Sq {0} is invalid", sq)); for (var index = 0; index < MoveGen.NUM_DIR[pce]; ++index) { var t_sq = sq + MoveGen.PCE_DIR[pce, index]; // Check if offboard if (!Validators.SqOnBoard(t_sq)) { continue; } if (board[t_sq] != (int)Piece.EMPTY) { // 0 EXCLUSIVE OR 1 returns 1 = COLOUR.WHITE // 1 EXCLUSIVE OR 1 returns 0 = COLOUR.BLACK if ((int)Data.PIECE_COLOURS[board[t_sq]] == (board.Side ^ 1)) { AddCaptureMove(board, MoveOperations.CreateMove(sq, t_sq, board[t_sq], (int)Piece.EMPTY, 0), list); } continue; } if (!onlyCaptures) { AddQuietMove(board, MoveOperations.CreateMove(sq, t_sq, (int)Piece.EMPTY, (int)Piece.EMPTY, 0), list); } } } pce = MoveGen.LOOP_NON_SLIDE_PCE[pceIndex++]; } }