Example #1
0
 public override void UnMakeMove(Board board)
 {
     Piece.MoveTo(board, From);
     if (CapturedPiece != null)
     {
         CapturedPiece.PutMe(board);
     }
 }
Example #2
0
 public static int Evaluate(Board board)
 {
     var score = 0;
     foreach (Piece piece in board)
     {
         score += piece.Evaluation();
     }
     return (int)board.Side*score;
 }
Example #3
0
 /// <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();
 }
Example #4
0
 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();
 }
Example #5
0
 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);
 }
Example #6
0
 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;
 }
Example #7
0
 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);
     }
 }
Example #8
0
 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);
     }
 }
Example #9
0
 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);
     }
 }
Example #10
0
 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);
     }
 }
Example #11
0
        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);
        }
Example #12
0
 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;
     }
 }
Example #13
0
 /// <summary>
 /// Put itself on board if it is captured.
 /// </summary>
 /// <param name="board"></param>
 public void PutMe(Board board)
 {
     board[From] = this;
 }
Example #14
0
 /// <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;
 }
Example #15
0
        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;
        }
Example #16
0
 public EnPassant(Board board, Piece piece,Coordinate to)
     : base(board,piece,to)
 {
 }
Example #17
0
 public Ordinary(Board board, Piece piece, Coordinate to)
     : base(piece)
 {
     To = to;
     CapturedPiece = To.GetPiece(board);
 }
Example #18
0
 public override void MakeMove(Board board)
 {
     Piece.MoveTo(board, To);
 }
Example #19
0
 public override void UnMakeMove(Board board)
 {
     PromotedPiece.RemoveMe(board);
     if (CapturedPiece != null)
     {
         CapturedPiece.PutMe(board);
     }
     Piece.PutMe(board);
 }
Example #20
0
 public override void MakeMove(Board board)
 {
     Piece.RemoveMe(board);
     CapturedPiece = To.GetPiece(board);
     board[To] = PromotedPiece;
 }
Example #21
0
 /// <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;
 }
Example #22
0
 /// <summary>
 /// Remove itself on board
 /// </summary>
 public void RemoveMe(Board board)
 {
     board[From] = null;
 }
Example #23
0
        /// <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;
        }
Example #24
0
        /// <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;
        }
Example #25
0
        /// <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;
        }
Example #26
0
 /// <summary>
 /// Takebacks move
 /// </summary>
 /// <param name="board"></param>
 public abstract void UnMakeMove(Board board);