Example #1
0
        /*
         * Knight can move two squares horizontally then one square vertically,
         * or moving one square horizontally then two squares vertically
         */
        public override bool IsValidMove(CoOrdinate to, History history)
        {
            if (to.XCoOrdinate == CurrentCoOrdinate.XCoOrdinate || to.YCoOrdinate == CurrentCoOrdinate.YCoOrdinate)
            {
                return(false);
            }

            //Cannot kill its own piece
            Piece foundPiece;

            if (history.LayOut.TryGetValue(to, out foundPiece) && Color == foundPiece.Color)
            {
                return(false);
            }

            if (Math.Abs(to.XCoOrdinate - CurrentCoOrdinate.XCoOrdinate) == 2)
            {
                return(Math.Abs(to.YCoOrdinate - CurrentCoOrdinate.YCoOrdinate) == 1);
            }
            else if (Math.Abs(to.YCoOrdinate - CurrentCoOrdinate.YCoOrdinate) == 2)
            {
                return(Math.Abs(to.XCoOrdinate - CurrentCoOrdinate.XCoOrdinate) == 1);
            }
            return(false);
        }
Example #2
0
        //Queen move is a combination of Rook and Bishop move
        public override bool IsValidMove(CoOrdinate to, History history)
        {
            var isValidBisopMove = new Bishop(Color, CurrentCoOrdinate).IsValidMove(to, history);
            var isValidRookMove  = new Rook(Color, CurrentCoOrdinate).IsValidMove(to, history);

            return(isValidBisopMove || isValidRookMove);
        }
Example #3
0
 public bool Equals(CoOrdinate incomingCoOrdinate)
 {
     if (XCoOrdinate == incomingCoOrdinate.XCoOrdinate && YCoOrdinate == incomingCoOrdinate.YCoOrdinate)
     {
         return(true);
     }
     return(false);
 }
Example #4
0
        protected Piece(Color color, CoOrdinate coOrdinate, PieceType name)
        {
            Type           = name;
            Color          = color;
            FromCoOrdinate = CurrentCoOrdinate = coOrdinate;
            var    outPutDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string path            = Path.Combine(@"..\..\Images", Color.ToString());

            Image = Image.FromFile(Path.Combine(path, Type + "_" + Color.ToString() + ".png"));
            Image = (Image)(new Bitmap(Image, new Size(30, 30)));
        }
Example #5
0
        public override Dictionary <CoOrdinate, Piece> SetUp(Color color)
        {
            Dictionary <CoOrdinate, Piece> pieces = new Dictionary <CoOrdinate, Piece>();
            int        xCoOrdinateFirst           = 4;
            int        yCoOrdinate = GetYCoOrdinate(color);
            CoOrdinate coOrdinate  = new CoOrdinate(xCoOrdinateFirst, yCoOrdinate);

            pieces.Add(coOrdinate, new Queen(color, coOrdinate));

            return(pieces);
        }
Example #6
0
        public override Dictionary <CoOrdinate, Piece> SetUp(Color color)
        {
            int yCoOrdinate = color == Color.White ? 1 : 6;
            Dictionary <CoOrdinate, Piece> pieces = new Dictionary <CoOrdinate, Piece>();

            for (int x = 0; x < NUMBEROFPAWNS; x++)
            {
                CoOrdinate coOrdinate = new CoOrdinate(x, yCoOrdinate);
                pieces.Add(coOrdinate, new Pawn(color, coOrdinate));
            }

            return(pieces);
        }
Example #7
0
        //For rook to move all cells should be empty in straight direction
        public override bool IsValidMove(CoOrdinate to, History history)
        {
            if (to.XCoOrdinate == CurrentCoOrdinate.XCoOrdinate && Math.Abs(to.YCoOrdinate - CurrentCoOrdinate.YCoOrdinate) > 0)
            {
                return(AreCellsEmpty(CurrentCoOrdinate.XCoOrdinate, CurrentCoOrdinate.YCoOrdinate, to.YCoOrdinate, history, CoOrdinateType.X));
            }
            if (to.YCoOrdinate == CurrentCoOrdinate.YCoOrdinate && Math.Abs(to.XCoOrdinate - CurrentCoOrdinate.YCoOrdinate) > 0)
            {
                return(AreCellsEmpty(CurrentCoOrdinate.YCoOrdinate, CurrentCoOrdinate.XCoOrdinate, to.XCoOrdinate, history, CoOrdinateType.Y));
            }

            return(false);
        }
Example #8
0
        public bool PlayTurn(PlayerName playerType, Piece piece, CoOrdinate to)
        {
            var     player  = _playerDetailsByType[playerType];
            History history = _boardState.GetState(_board.CurrentMoveNumber);

            if (player.IsValidMove(piece, to, history))
            {
                player.PlayTurn(piece, to);
                _board.UpdateState(piece);
                _status = _boardState.GetStatus(_board.CurrentMoveNumber);
                return(true);
            }
            return(false);
        }
Example #9
0
        public bool IsValidMove(Piece piece, CoOrdinate to, History history)
        {
            if (!_myPieces.Contains(piece, new PieceComparer()))
            {
                return(false);
            }

            if (piece.IsValidMove(to, history))
            {
                return(true);
            }

            return(false);
        }
Example #10
0
        public override Dictionary <CoOrdinate, Piece> SetUp(Color color)
        {
            int xCoOrdinateFirst  = 0;
            int xCoOrdinateSecond = 7;
            int yCoOrdinate       = GetYCoOrdinate(color);
            Dictionary <CoOrdinate, Piece> pieces = new Dictionary <CoOrdinate, Piece>();

            CoOrdinate coOrdinate = new CoOrdinate(xCoOrdinateFirst, yCoOrdinate);

            pieces.Add(coOrdinate, new Rook(color, coOrdinate));
            coOrdinate = new CoOrdinate(xCoOrdinateSecond, yCoOrdinate);
            pieces.Add(coOrdinate, new Rook(color, coOrdinate));

            return(pieces);
        }
Example #11
0
        public override Dictionary <CoOrdinate, Piece> SetUp(Color color)
        {
            int xCoOrdinateFirst  = 2;
            int xCoOrdinateSecond = 5;
            int yCoOrdinate       = GetYCoOrdinate(color);
            Dictionary <CoOrdinate, Piece> pieces = new Dictionary <CoOrdinate, Piece>();
            CoOrdinate coOrdinate = new CoOrdinate(xCoOrdinateFirst, yCoOrdinate);
            var        piece      = new Bishop(color, coOrdinate);

            pieces.Add(piece.FromCoOrdinate, piece);
            coOrdinate = new CoOrdinate(xCoOrdinateSecond, yCoOrdinate);
            piece      = new Bishop(color, coOrdinate);
            pieces.Add(piece.FromCoOrdinate, piece);
            return(pieces);
        }
Example #12
0
        //Bishop can only move only diagonally
        public override bool IsValidMove(CoOrdinate to, History history)
        {
            if (to.XCoOrdinate == CurrentCoOrdinate.XCoOrdinate || to.YCoOrdinate == CurrentCoOrdinate.YCoOrdinate)
            {
                return(false);
            }

            CoOrdinate finalCheckCoOrdinate;

            //For Bishop to move all cells should be empty in diagonal direction
            if (to.XCoOrdinate > CurrentCoOrdinate.XCoOrdinate)
            {
                //Try to move diagonally to the right of current position
                finalCheckCoOrdinate = AreCellsEmpty(CurrentCoOrdinate.XCoOrdinate, CurrentCoOrdinate.YCoOrdinate, to.YCoOrdinate, history, true);
            }
            else
            {
                //Try to move diagonally to the left of current position
                finalCheckCoOrdinate = AreCellsEmpty(CurrentCoOrdinate.XCoOrdinate, CurrentCoOrdinate.YCoOrdinate, to.YCoOrdinate, history, false);
            }

            return(to.Equals(finalCheckCoOrdinate));
        }
Example #13
0
 public Rook(Color color, CoOrdinate coOrdinate)
     : base(color, coOrdinate, PieceType.Rook)
 {
 }
Example #14
0
        private CoOrdinate AreCellsEmpty(int staticIndex, int startIndex, int finalIndex, History history, bool isRightHalf)
        {
            Piece foundPiece;

            if (startIndex < finalIndex)
            {
                for (int i = startIndex + 1; i <= finalIndex; i++)
                {
                    if (isRightHalf)
                    {
                        staticIndex++;
                    }
                    else
                    {
                        staticIndex--;
                    }

                    var checkCoOrdinate = new CoOrdinate(staticIndex, i);
                    if (history.LayOut.TryGetValue(checkCoOrdinate, out foundPiece))
                    {
                        if (!CanPieceBeMoved(finalIndex, foundPiece, i))
                        {
                            if (i != finalIndex)
                            {
                                return(checkCoOrdinate);
                            }
                            else
                            {
                                return(new CoOrdinate(-1, -1));
                            }
                        }
                    }
                }
            }
            else
            {
                for (int i = startIndex - 1; i >= finalIndex; i--)
                {
                    if (isRightHalf)
                    {
                        staticIndex++;
                    }
                    else
                    {
                        staticIndex--;
                    }

                    var checkCoOrdinate = new CoOrdinate(staticIndex, i);
                    if (history.LayOut.TryGetValue(checkCoOrdinate, out foundPiece))
                    {
                        if (!CanPieceBeMoved(finalIndex, foundPiece, i))
                        {
                            if (i != finalIndex)
                            {
                                return(checkCoOrdinate);
                            }
                            else
                            {
                                return(new CoOrdinate(-1, -1));
                            }
                        }
                    }
                }
            }
            return(new CoOrdinate(staticIndex, finalIndex));
        }
Example #15
0
 public Queen(Color color, CoOrdinate coOrdinate)
     : base(color, coOrdinate, PieceType.Queen)
 {
 }
Example #16
0
 public King(Color color, CoOrdinate coOrdinate)
     : base(color, coOrdinate, PieceType.King)
 {
 }
Example #17
0
        /*
         * A pawn moves straight forward one square, if that square is vacant. If it has not yet moved,
         * a pawn also has the option of moving two squares straight forward, provided both squares are vacant.
         * Pawns cannot move backwards.
         * Pawns are the only pieces that capture differently from how they move. A pawn can capture an enemy piece on either of the two squares diagonally in front of the pawn (but cannot move to those squares if they are vacant).
         */
        public override bool IsValidMove(CoOrdinate to, History history)
        {
            Piece foundPiece;

            if (to.XCoOrdinate == CurrentCoOrdinate.XCoOrdinate)
            {
                //The next cell should be empty if moving forward
                if (!history.LayOut.TryGetValue(to, out foundPiece))
                {
                    int maxMoveMagnitude = CurrentCoOrdinate == FromCoOrdinate ? 2 : 1;

                    if (maxMoveMagnitude == 2 && Math.Abs(to.YCoOrdinate - CurrentCoOrdinate.YCoOrdinate) == 2)
                    {
                        if (Color == Color.White)
                        {
                            var diffInCoOrdinate = to.YCoOrdinate - CurrentCoOrdinate.YCoOrdinate;
                            if (diffInCoOrdinate > 0 && diffInCoOrdinate <= maxMoveMagnitude)
                            {
                                return(AreCellsEmpty(CurrentCoOrdinate.XCoOrdinate, CurrentCoOrdinate.YCoOrdinate, to.YCoOrdinate - 1, history, CoOrdinateType.X));
                            }
                        }
                        else
                        {
                            var diffInCoOrdinate = CurrentCoOrdinate.YCoOrdinate - to.YCoOrdinate;
                            if (diffInCoOrdinate > 0 && diffInCoOrdinate <= maxMoveMagnitude)
                            {
                                return(AreCellsEmpty(CurrentCoOrdinate.XCoOrdinate, to.YCoOrdinate, CurrentCoOrdinate.YCoOrdinate - 1, history, CoOrdinateType.X));
                            }
                        }
                    }
                    else
                    {
                        //Pwan cannot move backwards
                        if (Color == Color.White)
                        {
                            return((to.YCoOrdinate - CurrentCoOrdinate.YCoOrdinate) == 1);
                        }
                        else
                        {
                            return((CurrentCoOrdinate.YCoOrdinate - to.YCoOrdinate) == 1);
                        }
                    }
                }
            }

            //Pawn can move diagonally only of there is piece of another color in that position
            var diagonalYCoOrdinate     = Color == Color.White ? CurrentCoOrdinate.YCoOrdinate + 1 : CurrentCoOrdinate.YCoOrdinate - 1;
            var leftDiagonalCoOrdinate  = new CoOrdinate(CurrentCoOrdinate.XCoOrdinate - 1, diagonalYCoOrdinate);
            var rightDiagonalCoOrdinate = new CoOrdinate(CurrentCoOrdinate.XCoOrdinate + 1, diagonalYCoOrdinate);

            if (history.LayOut.TryGetValue(leftDiagonalCoOrdinate, out foundPiece) && foundPiece.Color != Color)
            {
                return(to.Equals(leftDiagonalCoOrdinate));
            }
            if (history.LayOut.TryGetValue(rightDiagonalCoOrdinate, out foundPiece) && foundPiece.Color != Color)
            {
                return(to.Equals(rightDiagonalCoOrdinate));
            }

            return(false);
        }
Example #18
0
 public Pawn(Color color, CoOrdinate coOrdinate)
     : base(color, coOrdinate, PieceType.Pawn)
 {
 }
Example #19
0
 public void Move(CoOrdinate coOrdinate)
 {
     FromCoOrdinate    = CurrentCoOrdinate;
     CurrentCoOrdinate = coOrdinate;
 }
Example #20
0
 public Knight(Color color, CoOrdinate coOrdinate)
     : base(color, coOrdinate, PieceType.Knight)
 {
 }
Example #21
0
 public abstract bool IsValidMove(CoOrdinate to, History history);
Example #22
0
        //King can move in any direction, only if there is piece of another color in that position
        public override bool IsValidMove(CoOrdinate to, History history)
        {
            Piece foundPiece;

            if (history.LayOut.TryGetValue(to, out foundPiece) && Color == foundPiece.Color)
            {
                return(false);
            }

            if (to.XCoOrdinate == CurrentCoOrdinate.XCoOrdinate)
            {
                return(Math.Abs(to.YCoOrdinate - CurrentCoOrdinate.YCoOrdinate) == 1);
            }

            if (to.YCoOrdinate == CurrentCoOrdinate.YCoOrdinate)
            {
                return(Math.Abs(to.XCoOrdinate - CurrentCoOrdinate.XCoOrdinate) == 1);
            }

            var diagonalYCoOrdinate         = Color == Color.White ? CurrentCoOrdinate.YCoOrdinate + 1 : CurrentCoOrdinate.YCoOrdinate - 1;
            var diagonalBackwardYCoOrdinate = Color == Color.White ? CurrentCoOrdinate.YCoOrdinate - 1 : CurrentCoOrdinate.YCoOrdinate + 1;

            var leftDiagonalCoOrdinate         = new CoOrdinate(CurrentCoOrdinate.XCoOrdinate - 1, diagonalYCoOrdinate);
            var leftBackwardDiagonalCoOrdinate = new CoOrdinate(CurrentCoOrdinate.XCoOrdinate - 1, diagonalBackwardYCoOrdinate);

            var rightDiagonalCoOrdinate         = new CoOrdinate(CurrentCoOrdinate.XCoOrdinate + 1, diagonalYCoOrdinate);
            var rightBackwardDiagonalCoOrdinate = new CoOrdinate(CurrentCoOrdinate.XCoOrdinate + 1, diagonalBackwardYCoOrdinate);


            if (history.LayOut.TryGetValue(leftDiagonalCoOrdinate, out foundPiece))
            {
                if (foundPiece.Color == Color)
                {
                    foundPiece = null;
                }
                else
                {
                    return(to.Equals(leftDiagonalCoOrdinate));
                }
            }

            if (history.LayOut.TryGetValue(leftBackwardDiagonalCoOrdinate, out foundPiece))
            {
                if (foundPiece.Color == Color)
                {
                    foundPiece = null;
                }
                else
                {
                    return(to.Equals(leftBackwardDiagonalCoOrdinate));
                }
            }

            if (history.LayOut.TryGetValue(rightDiagonalCoOrdinate, out foundPiece))
            {
                if (foundPiece.Color == Color)
                {
                    foundPiece = null;
                }
                else
                {
                    return(to.Equals(rightDiagonalCoOrdinate));
                }
            }

            if (history.LayOut.TryGetValue(rightBackwardDiagonalCoOrdinate, out foundPiece))
            {
                if (foundPiece.Color == Color)
                {
                    foundPiece = null;
                }
                else
                {
                    return(to.Equals(rightBackwardDiagonalCoOrdinate));
                }
            }

            if (foundPiece == null)
            {
                return(to.Equals(leftDiagonalCoOrdinate) || to.Equals(leftBackwardDiagonalCoOrdinate) || to.Equals(rightDiagonalCoOrdinate) || to.Equals(rightBackwardDiagonalCoOrdinate));
            }

            return(false);
        }
Example #23
0
 public Bishop(Color color, CoOrdinate coOrdinate)
     : base(color, coOrdinate, PieceType.Bishop)
 {
 }
Example #24
0
 public void PlayTurn(Piece piece, CoOrdinate to)
 {
     piece.Move(to);
 }