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++; }
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); }