public override void UnMakeMove(Board board) { Piece.MoveTo(board, From); if (CapturedPiece != null) { CapturedPiece.PutMe(board); } }
public static int Evaluate(Board board) { var score = 0; foreach (Piece piece in board) { score += piece.Evaluation(); } return (int)board.Side*score; }
/// <summary> /// /// </summary> /// <param name="board"></param> /// <param name="protocol">Protocol will be used to write output of bestline</param> public Engine(Board board, IProtocol protocol) { Exit = false; Board = board; Protocol = protocol; elapsedTime = new Stopwatch(); historyMoves = new HistoryMoves(); killerMoves = new KillerMoves(); }
public Zobrist(Board board) { this.board = board; pieces = new long[2, 6, 9, 9]; whiteCastle = new long[4]; blackCastle = new long[4]; enPassant = new long[9, 9]; Initialize(); AssignFirstZobristKey(); }
public override void MakeMove(Board board) { if (Piece.Color == Color.White) { CapturedPiece= To.To(Coordinate.Directions.South).GetPiece(board); } else { CapturedPiece = To.To(Coordinate.Directions.North).GetPiece(board); } CapturedPiece.RemoveMe(board); Piece.MoveTo(board,To); }
public static List<Move> AllPossiblePromotions(Board board, Piece piece, Coordinate square) { var moves = new List<Move>(); Move move = new Promote(board, piece, square, Promote.PromotionType.Queen); moves.Add(move); move = new Promote(board, piece, square, Promote.PromotionType.Rook); moves.Add(move); move = new Promote(board, piece, square, Promote.PromotionType.Bishop); moves.Add(move); move = new Promote(board, piece, square, Promote.PromotionType.Knight); moves.Add(move); return moves; }
public override void UnMakeMove(Board board) { if (Color.White == Piece.Color) { var rook = Coordinate.f1.GetPiece(board); Piece.MoveTo(board, Coordinate.e1); rook.MoveTo(board, Coordinate.h1); } else { var rook = Coordinate.f8.GetPiece(board); Piece.MoveTo(board, Coordinate.e8); rook.MoveTo(board, Coordinate.h8); } }
public override void MakeMove(Board board) { if (Color.White == Piece.Color) { var rook = Coordinate.a1.GetPiece(board); Piece.MoveTo(board, Coordinate.c1); rook.MoveTo(board, Coordinate.d1); } else { var rook = Coordinate.a8.GetPiece(board); Piece.MoveTo(board, Coordinate.c8); rook.MoveTo(board, Coordinate.d8); } }
public override void UnMakeMove(Board board) { if (Piece.Color == Color.White) { var rook = Coordinate.d1.GetPiece(board); Piece.MoveTo(board, Coordinate.e1); rook.MoveTo(board, Coordinate.a1); } else { var rook = Coordinate.d8.GetPiece(board); Piece.MoveTo(board, Coordinate.e8); rook.MoveTo(board, Coordinate.a8); } }
public override void MakeMove(Board board) { if (Color.White == Piece.Color) { var rook = Coordinate.h1.GetPiece(board); Piece.MoveTo(board, Coordinate.g1); rook.MoveTo(board, Coordinate.f1); } else { var rook = board[Coordinate.h8]; Piece.MoveTo(board, Coordinate.g8); rook.MoveTo(board, Coordinate.f8); } }
public static bool Available(Board board, Color side) { if (side == Color.White) { return Coordinate.f1.IsEmpty(board) && Coordinate.g1.IsEmpty(board) && !Coordinate.e1.IsAttackedSquare(board, Color.Black) && !Coordinate.f1.IsAttackedSquare(board, Color.Black) && !Coordinate.g1.IsAttackedSquare(board, Color.Black); } return Coordinate.f8.IsEmpty(board) && Coordinate.g8.IsEmpty(board) && !Coordinate.e8.IsAttackedSquare(board, Color.White) && !Coordinate.f8.IsAttackedSquare(board, Color.White) && !Coordinate.g8.IsAttackedSquare(board, Color.White); }
public Promote(Board board, Piece piece, Coordinate to, PromotionType type) : base(board, piece, to) { var color = piece.Color; switch (type) { case PromotionType.Queen: PromotedPiece = new Queen(to, piece.Color); break; case PromotionType.Rook: PromotedPiece = new Rook(to, piece.Color); break; case PromotionType.Bishop: PromotedPiece = new Bishop(to, piece.Color); break; case PromotionType.Knight: PromotedPiece = new Knight(to, piece.Color); break; default: break; } }
/// <summary> /// Put itself on board if it is captured. /// </summary> /// <param name="board"></param> public void PutMe(Board board) { board[From] = this; }
/// <summary> /// piece goes specified square /// </summary> /// <param name="board"></param> /// <param name="to"></param> public void MoveTo(Board board, Coordinate to) { RemoveMe(board); board[to] = this; From = to; }
public override List<Move> GenerateMoves(Board board) { var moves = new List<Move>(); Move move; if (Color == Color.White) { var upSquare = From.To(Coordinate.Directions.North); if (upSquare.IsEmpty(board) && From.Rank != 7)//Pawn can go up if it is empty square { move = new Ordinary(board, this, upSquare); moves.Add(move); var twoUpSquare = upSquare.To(Coordinate.Directions.North); if (From.Rank == 2 && twoUpSquare.IsEmpty(board))//Check can jump 2 square when it is on rank 2 { move = new Ordinary(board, this, twoUpSquare); moves.Add(move); } } if (From.Rank != 7) { Coordinate crossSquare = From.To(Coordinate.Directions.NorthEast); //Check for capture if (crossSquare.IsOnboard() && !crossSquare.IsEmpty(board) && crossSquare.GetPiece(board).Color == Color.Black) { move = new Ordinary(board, this, crossSquare); moves.Add(move); } crossSquare = From.To(Coordinate.Directions.NorthWest); if (crossSquare.IsOnboard() && !crossSquare.IsEmpty(board) && crossSquare.GetPiece(board).Color == Color.Black) { move = new Ordinary(board, this, crossSquare); moves.Add(move); } } if (From.Rank == 5)//Check possibility of enpassant move { var crossSquare = From.To(Coordinate.Directions.NorthEast); if (crossSquare.IsOnboard() && crossSquare.Equals(board.EnPassantSquare)) { move = new EnPassant(board, this, crossSquare); moves.Add(move); } else { crossSquare = From.To(Coordinate.Directions.NorthWest); if (crossSquare.IsOnboard() && crossSquare.Equals(board.EnPassantSquare)) { move = new EnPassant(board, this, crossSquare); moves.Add(move); } } } if (From.Rank == 7)//Check pawn promotions { upSquare = From.To(Coordinate.Directions.North); if (upSquare.IsEmpty(board)) { moves.AddRange(Promote.AllPossiblePromotions(board, this, upSquare)); } var crossSquare = From.To(Coordinate.Directions.NorthEast); if (crossSquare.IsOnboard() && !crossSquare.IsEmpty(board) && crossSquare.GetPiece(board).Color == Color.Black) { moves.AddRange(Promote.AllPossiblePromotions(board, this, crossSquare)); } crossSquare = From.To(Coordinate.Directions.NorthWest); if (crossSquare.IsOnboard() && !crossSquare.IsEmpty(board) && crossSquare.GetPiece(board).Color == Color.Black) { moves.AddRange(Promote.AllPossiblePromotions(board, this, crossSquare)); } } } else // For black pawn { var downSquare = From.To(Coordinate.Directions.South); if (downSquare.IsEmpty(board) && From.Rank != 2) { move = new Ordinary(board, this, downSquare); moves.Add(move); var twoDownSquare = downSquare.To(Coordinate.Directions.South); if (From.Rank == 7 && twoDownSquare.IsEmpty(board)) { move = new Ordinary(board, this, twoDownSquare); moves.Add(move); } } if (From.Rank != 2) { var crossSquare = From.To(Coordinate.Directions.SouthEast); if (crossSquare.IsOnboard() && !crossSquare.IsEmpty(board) && crossSquare.GetPiece(board).Color == Color.White) { move = new Ordinary(board, this, crossSquare); moves.Add(move); } crossSquare = From.To(Coordinate.Directions.SouthWest); if (crossSquare.IsOnboard() && !crossSquare.IsEmpty(board) && crossSquare.GetPiece(board).Color == Color.White) { move = new Ordinary(board, this, crossSquare); moves.Add(move); } } if (From.Rank == 4) { var crossSquare = From.To(Coordinate.Directions.SouthEast); if (crossSquare.IsOnboard() && crossSquare.Equals(board.EnPassantSquare)) { move = new EnPassant(board, this, crossSquare); moves.Add(move); } else { crossSquare = From.To(Coordinate.Directions.SouthWest); if (crossSquare.IsOnboard() && crossSquare.Equals(board.EnPassantSquare)) { move = new EnPassant(board, this, crossSquare); moves.Add(move); } } } if (From.Rank == 2) { downSquare = From.To(Coordinate.Directions.South); if (downSquare.IsEmpty(board)) { moves.AddRange(Promote.AllPossiblePromotions(board, this, downSquare)); } var crossSquare = From.To(Coordinate.Directions.SouthEast); if (crossSquare.IsOnboard() && !crossSquare.IsEmpty(board) && crossSquare.GetPiece(board).Color == Color.White) { moves.AddRange(Promote.AllPossiblePromotions(board, this, crossSquare)); } crossSquare = From.To(Coordinate.Directions.SouthWest); if (crossSquare.IsOnboard() && !crossSquare.IsEmpty(board) && crossSquare.GetPiece(board).Color == Color.White) { moves.AddRange(Promote.AllPossiblePromotions(board, this, crossSquare)); } } } return moves; }
public EnPassant(Board board, Piece piece,Coordinate to) : base(board,piece,to) { }
public Ordinary(Board board, Piece piece, Coordinate to) : base(piece) { To = to; CapturedPiece = To.GetPiece(board); }
public override void MakeMove(Board board) { Piece.MoveTo(board, To); }
public override void UnMakeMove(Board board) { PromotedPiece.RemoveMe(board); if (CapturedPiece != null) { CapturedPiece.PutMe(board); } Piece.PutMe(board); }
public override void MakeMove(Board board) { Piece.RemoveMe(board); CapturedPiece = To.GetPiece(board); board[To] = PromotedPiece; }
/// <summary> /// Get piece which sit on this square /// </summary> /// <param name="board">Board</param> /// <returns>Piece on this square</returns> public Piece GetPiece(Board board) { if (IsOnboard()) { return board[this]; } return null; }
/// <summary> /// Remove itself on board /// </summary> public void RemoveMe(Board board) { board[From] = null; }
/// <summary> /// Determine whether this square is empty or not /// </summary> /// <param name="board">board</param> /// <returns></returns> public bool IsEmpty(Board board) { #if(DEBUG) if (!IsOnboard()) { throw new OutOfBoardException(); } #endif return GetPiece(board) == null; }
/// <summary> /// Is square attacked by oppenent pieces /// </summary> /// <param name="board">Board</param> /// <param name="enemyColor">Enemy side color</param> /// <returns></returns> public bool IsAttackedSquare(Board board, Color enemyColor) { Piece piece; foreach (var direction in Coordinate.crossFourDirectionDelta) //Check cross directions for queen and bishop { Coordinate to = this; while ((to = to.To(direction)).IsOnboard()) { piece = to.GetPiece(board); if (piece != null) { if ((piece is Queen || piece is Bishop) && piece.Color == enemyColor) { return true; } break; } } } foreach (var direction in Coordinate.fourDirectionDelta)//check horizonal and vertical directions for queen and rook { Coordinate to = this; while ((to = to.To(direction)).IsOnboard()) { piece = to.GetPiece(board); if (piece != null) { if ((piece is Queen || piece is Rook) && piece.Color == enemyColor) { return true; } break; } } } foreach (var direction in Knight.Directions) // Knight directions { Coordinate to = this; to = to.To(direction); if (to.IsOnboard()) { piece = to.GetPiece(board); if (piece != null) { if ((piece is Knight) && piece.Color == enemyColor) { return true; } } } } King opponentking = enemyColor == Color.White ? board.WhiteKing : board.BlackKing; if (this.IsNeighboreSquare(opponentking.From)) //Check for oppenent king { return true; } if (enemyColor == Color.Black) //Check for pawns { piece = this.To(Coordinate.Directions.NorthEast).GetPiece(board); if (piece != null && piece is Pawn && piece.Color == enemyColor) { return true; } piece = this.To(Coordinate.Directions.NorthWest).GetPiece(board); if (piece != null && piece is Pawn && piece.Color == enemyColor) { return true; } } else // Black Pawns { piece = this.To(Coordinate.Directions.SouthEast).GetPiece(board); if (piece != null && piece is Pawn && piece.Color == enemyColor) { return true; } piece = this.To(Coordinate.Directions.SouthWest).GetPiece(board); if (piece != null && piece is Pawn && piece.Color == enemyColor) { return true; } } return false; }
/// <summary> /// Generate pseudo moves /// </summary> /// <param name="board"></param> /// <returns></returns> public virtual List<Move> GenerateMoves(Board board) { List<Move> moves = new List<Move>(); foreach (var direction in this.PieceDirection) { Coordinate destination = From; Piece piece; while ((destination = destination.To(direction)).IsOnboard()) { piece = board[destination]; if (piece != null && this.Color == piece.Color) break; moves.Add(new Ordinary(board, this, destination)); if (!Sliding || (piece != null && piece.Color != this.Color)) break; } } return moves; }
/// <summary> /// Takebacks move /// </summary> /// <param name="board"></param> public abstract void UnMakeMove(Board board);