Пример #1
0
        //public Table(Int32 Width, Int32 Height) {
        //    Positions = new Robot[Width, Height];
        //}

        /// <summary>
        /// This will check if the passed robot can move to a given position, and will move it and apply the positions if it can, or will throw an exception.
        /// </summary>
        /// <param name="robot">The robot that is attempting to move</param>
        /// <param name="position">The position the robot is attempting to move to</param>
        /// <returns>true, indicating the robot was moved successfully. An exception is thrown otherwise.</returns>
        public Boolean Move(Robot robot, TablePosition position)
        {
            if (position.X < 0 || position.X >= this.Positions.GetLength(0) ||
                position.Y < 0 || position.Y >= this.Positions.GetLength(1))
            {
                return(false);
            }

            var currentlyThere = this.Positions[position.X, position.Y];

            //If the space is not empty, and robot there is not the moving robot, throw an exception
            if (currentlyThere != null && currentlyThere != robot)
            {
                throw new Exception("Position is not empty.");
            }

            if (robot.Placed == true)
            {
                //Remove it from its current position
                this.Positions[robot.Position.X, robot.Position.Y] = null;
            }

            currentlyThere = robot;
            robot.Position = position;

            return(true);
        }
Пример #2
0
        public Boolean Move(MoveVector vector)
        {
            if (this.Placed == false)
            {
                //Specialise exception to be MoveException etc depending on needs
                //Would normally throw exception, in this case, the command will just be ignored
                //throw new Exception("The robot has not been placed.");
                return(false);
            }
            var newPosition = new TablePosition {
                X = this.Position.X,
                Y = this.Position.Y
            };

            newPosition.X += vector.X;
            newPosition.Y += vector.Y;
            return(this.Table.Move(this, newPosition));


            //Check with the table that the position is valid
        }
Пример #3
0
        public String Command(String command)
        {
            if (command.IndexOf(Commands.Place) == 0)
            {
                if (command.Length < Commands.Place.Length + 1)
                {
                    throw new Exception("No parameters were passed.");
                }
                String[] placeParams   = command.Substring(Commands.Place.Length + 1).Split(',');
                var      placePosition = new TablePosition();

                if (placeParams.Length < 3)
                {
                    throw new Exception("Not enough parameters. Three parameters are required.");
                }

                try {
                    placePosition.X = Int32.Parse(placeParams[0]);
                } catch {
                    throw new Exception(placeParams[0] + " is not a valid number.");
                }

                try {
                    placePosition.Y = Int32.Parse(placeParams[1]);
                } catch {
                    throw new Exception(placeParams[1] + " is not a valid number.");
                }

                var direction = Robot.Directions.FirstOrDefault(d => d.Name == placeParams[2]);
                if (direction == null)
                {
                    throw new Exception("Could not find direction " + placeParams[2] + ".");
                }

                if (this.Table.Move(this, placePosition))
                {
                    this.Placed    = true;
                    this.Direction = direction;
                }
            }
            else if (command == Commands.Move)
            {
                Move(this.Direction);
            }
            else if (command == Commands.Left)
            {
                Turn(-1);
            }
            else if (command == Commands.Right)
            {
                Turn(1);
            }
            else if (command == Commands.Report)
            {
                if (this.Placed == false)
                {
                    throw new Exception("The robot has not been placed.");
                }
                return(this.Position.X + "," + this.Position.Y + "," + this.Direction.Name);
            }
            else
            {
                //Specialise exception to be CommandException etc depending on needs
                throw new UnknownCommandException("Command not recognised.");
            }
            return("");
        }