Beispiel #1
0
        public Point GetFacingPoint(CastleDirection direction)
        {
            Point preview = new Point(Position.X, Position.Y);

            switch (Facing)
            {
            case CastleDirection.Up:
                preview.Y -= 1;
                break;

            case CastleDirection.Down:
                preview.Y += 1;
                break;

            case CastleDirection.Left:
                preview.X -= 1;
                break;

            case CastleDirection.Right:
                preview.X += 1;
                break;
            }

            return(preview);
        }
Beispiel #2
0
        public Point PreviewMove(CastleDirection direction)
        {
            Point preview = new Point(Position.X, Position.Y);

            switch (direction)
            {
            case CastleDirection.Up:
                this.Facing = CastleDirection.Up;
                preview.Y  -= 1;
                break;

            case CastleDirection.Down:
                this.Facing = CastleDirection.Down;
                preview.Y  += 1;
                break;

            case CastleDirection.Left:
                this.Facing = CastleDirection.Left;
                preview.X  -= 1;
                break;

            case CastleDirection.Right:
                this.Facing = CastleDirection.Right;
                preview.X  += 1;
                break;
            }

            return(preview);
        }
Beispiel #3
0
        public int GetNextRoom(CastleDirection roomDirection)
        {
            Char targetChat;

            switch (roomDirection)
            {
            case CastleDirection.Up:
                targetChat = 'N';
                break;

            case CastleDirection.Down:
                targetChat = 'S';
                break;

            case CastleDirection.Left:
                targetChat = 'W';
                break;

            case CastleDirection.Right:
                targetChat = 'E';
                break;

            case CastleDirection.UpStairs:
                targetChat = 'U';
                break;

            case CastleDirection.DownStairs:
                targetChat = 'D';
                break;

            default:
                targetChat = 'N';
                break;
            }

            int index = Exits.IndexOf(targetChat, 0);

            if (index >= 0)
            {
                StringBuilder numericValues = new StringBuilder();
                for (int start = index; start < Exits.Length; start++)
                {
                    String value = Exits.Substring(start + 1, 1);
                    int    intValue;
                    if (int.TryParse(value, out intValue))
                    {
                        numericValues.Append(intValue);
                    }
                    else
                    {
                        break;
                    }
                }
                return(int.Parse(numericValues.ToString()));
            }
            else
            {
                return(-1);
            }
        }
Beispiel #4
0
        public CastleCommand(IReadOnlyDictionary <string, IReadOnlyList <string> > commandParams) : this()
        {
            IReadOnlyList <string> paramsList;

            if (commandParams.TryGetValue("direction", out paramsList))
            {
                Direction = (CastleDirection)Enum.Parse(typeof(CastleDirection), paramsList.FirstOrDefault(), true);
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("CastleCommand did not receive a 'direction' value.");
            }
        }
Beispiel #5
0
        public CastleCommand(ICommand command) : this()
        {
            var cCmd = command as CastleCommand;

            if (cCmd != null)
            {
                Direction = cCmd.Direction;
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("Attempted to make a new CastleCommand from a different ICommand.");
                throw new InvalidCastException("Attempted to make a new CastleCommand from a different ICommand.");
            }
        }
Beispiel #6
0
        /// <summary>
        /// Perform the castle move.
        /// </summary>
        /// <param name="color"><see cref="PieceColor"/></param>
        /// <param name="castleDirection"><see cref="CastleDirection"/></param>
        /// <returns>False if the castle is illegal.</returns>
        public bool Castle(PieceColor color, CastleDirection castleDirection)
        {
            // Define castling pieces
            var pieces = color == PieceColor.Black
                ? BlackPieces
                : WhitePieces;
            var king = pieces.King;

            Piece     rook;
            Direction direction;
            Direction oppositeDirection;

            switch (castleDirection)
            {
            case CastleDirection.Kingside:
                rook              = pieces.RookH;
                direction         = Direction.Right;
                oppositeDirection = Direction.Left;
                break;

            case CastleDirection.Queenside:
                rook              = pieces.RookA;
                direction         = Direction.Left;
                oppositeDirection = Direction.Right;
                break;

            default:
                throw new ArgumentOutOfRangeException("castleDirection");
            }

            // Can't castle if either piece has moved
            if (king.Moved || rook.Moved)
            {
                return(false);
            }

            // Can't castle if any pieces in between king and rook
            var square = NextSquare(king.Square, direction);

            while (square.Piece != rook)
            {
                if (square.Piece != null)
                {
                    return(false);
                }
                square = NextSquare(square, direction);
            }

            // Adjust piece/square pointers
            var kingFrom = king.Square;
            var kingTo   = NextSquare(kingFrom, direction, 2);

            king.Square    = kingTo;
            kingTo.Piece   = king;
            kingFrom.Piece = null;
            var rookFrom = king.Square;
            var rookTo   = NextSquare(kingTo, oppositeDirection);

            rook.Square    = rookTo;
            rookTo.Piece   = rook;
            rookFrom.Piece = null;

            // Recalculate all possible next moves
            foreach (var p in Pieces)
            {
                var pVar = p;
                SetMoves(ref pVar);
            }

            return(true);
        }
Beispiel #7
0
        private bool CanCastle(IDictionary <Position, Piece> pieces, out CastleDirection castleDirection)
        {
            castleDirection = CastleDirection.None;
            if (NumberMoves > 0)
            {
                return(false);
            }

            var startingRookPositions = Colour == Colour.White
                ? Constants.WhiteRookStartingPositions
                : Constants.BlackRookStartingPositions;

            var castleBlockerLeft  = false;
            var castleBlockerRight = false;

            var castleDeltaLeftOne   = new Position(0, -1);
            var castleDeltaLeftTwo   = new Position(0, -2);
            var castleDeltaLeftThree = new Position(0, -3);
            var castleDeltaRightOne  = new Position(0, 1);
            var castleDeltaRightTwo  = new Position(0, 2);

            if (pieces.ContainsKey(Position + castleDeltaLeftOne) ||
                pieces.ContainsKey(Position + castleDeltaLeftTwo) ||
                pieces.ContainsKey(Position + castleDeltaLeftThree))
            {
                castleBlockerLeft = true;
            }

            if (pieces.ContainsKey(Position + castleDeltaRightOne) ||
                pieces.ContainsKey(Position + castleDeltaRightTwo))
            {
                castleBlockerRight = true;
            }

            if (castleBlockerLeft && castleBlockerRight)
            {
                return(false);
            }

            var validRooks = FetchValidCastlingRooks(pieces, startingRookPositions);

            if (!validRooks.Any())
            {
                return(false);
            }

            if (castleBlockerLeft)
            {
                castleDirection = CastleDirection.Right;
            }
            else if (castleBlockerRight)
            {
                castleDirection = CastleDirection.Left;
            }
            else
            {
                castleDirection = CastleDirection.Both;
            }

            return(true);
        }
Beispiel #8
0
        /// <summary>
        /// Perform the castle move.
        /// </summary>
        /// <param name="color"><see cref="PieceColor"/></param>
        /// <param name="castleDirection"><see cref="CastleDirection"/></param>
        /// <returns>False if the castle is illegal.</returns>
        public bool Castle(PieceColor color, CastleDirection castleDirection)
        {
            // Define castling pieces
            var pieces = color == PieceColor.Black
                ? BlackPieces
                : WhitePieces;
            var king = pieces.King;

            Piece rook;
            Direction direction;
            Direction oppositeDirection;
            switch (castleDirection)
            {
                case CastleDirection.Kingside:
                    rook = pieces.RookH;
                    direction = Direction.Right;
                    oppositeDirection = Direction.Left;
                    break;
                case CastleDirection.Queenside:
                    rook = pieces.RookA;
                    direction = Direction.Left;
                    oppositeDirection = Direction.Right;
                    break;
                default:
                    throw new ArgumentOutOfRangeException("castleDirection");
            }

            // Can't castle if either piece has moved
            if (king.Moved || rook.Moved)
                return false;

            // Can't castle if any pieces in between king and rook
            var square = NextSquare(king.Square, direction);
            while (square.Piece != rook)
            {
                if (square.Piece != null)
                    return false;
                square = NextSquare(square, direction);
            }

            // Adjust piece/square pointers
            var kingFrom = king.Square;
            var kingTo = NextSquare(kingFrom, direction, 2);
            king.Square = kingTo;
            kingTo.Piece = king;
            kingFrom.Piece = null;
            var rookFrom = king.Square;
            var rookTo = NextSquare(kingTo, oppositeDirection);
            rook.Square = rookTo;
            rookTo.Piece = rook;
            rookFrom.Piece = null;

            // Recalculate all possible next moves
            foreach (var p in Pieces)
            {
                var pVar = p;
                SetMoves(ref pVar);
            }

            return true;
        }