コード例 #1
0
        /// <summary>
        /// Execute a given command, when command is executed it is removed from the list and a new state is saved into history.
        /// For fun I've added collision detection with edges and other rovers
        /// </summary>
        /// <param name="enableCollision">Will throw <see cref="ExceptionCollision"/> if there's a collision</param>
        /// <param name="otherRovers">used for collision detection</param>
        public void Execute(Utils.Command cmd, bool enableCollision, Rovers otherRovers)
        {
            Utils.Orientation nextOrientation =
                cmd == Utils.Command.Left ? Utils.RotateLeft(this.CurrentState.Orientation) :
                cmd == Utils.Command.Right ? Utils.RotateRight(this.CurrentState.Orientation) :
                this.CurrentState.Orientation;

            int dX = cmd == Utils.Command.Move ?
                     this.CurrentState.Orientation == Utils.Orientation.East ? +1 :
                     this.CurrentState.Orientation == Utils.Orientation.West ? -1 :
                     0 : 0;

            int dY = cmd == Utils.Command.Move ?
                     this.CurrentState.Orientation == Utils.Orientation.North ? +1 :
                     this.CurrentState.Orientation == Utils.Orientation.South ? -1 :
                     0 : 0;

            this.CurrentState = new RoverState(this.CurrentState.X + dX, this.CurrentState.Y + dY, nextOrientation);

            StateHistory.Add(this.CurrentState);

            if (enableCollision && (
                    this.CurrentState.X <= 0 ||
                    this.CurrentState.X >= w ||
                    this.CurrentState.Y <= 0 ||
                    this.CurrentState.Y >= h ||
                    otherRovers != null && otherRovers.Any((rover) => rover != this && rover.CurrentState == this.CurrentState)
                    ))
            {
                throw new ExceptionCollision(this);
            }
        }
コード例 #2
0
        public Plateau(int width, int height, Rovers rovers)
        {
            if (width <= 0 || height <= 0 || rovers == null || rovers.Count() == 0)
            {
                throw new Exception("Invalid parameters");
            }

            this.Width  = width;
            this.Height = height;
            this.Rovers = rovers;
        }