Exemplo n.º 1
0
        public CompassDirections GetCompassDirectionTurnLeft(CompassDirections direction)
        {
            CompassDirections tempDirection = CompassDirections.N;

            switch (direction)
            {
            case CompassDirections.N:
                tempDirection = CompassDirections.W;
                break;

            case CompassDirections.E:
                tempDirection = CompassDirections.N;
                break;

            case CompassDirections.S:
                tempDirection = CompassDirections.E;
                break;

            case CompassDirections.W:
                tempDirection = CompassDirections.S;
                break;

            default:
                break;
            }
            return(tempDirection);
        }
Exemplo n.º 2
0
        public static Point GetAdjacentPoint(Point current, CompassDirections direction)
        {
            switch (direction)
            {
            case CompassDirections.North:
                return(new Point(current.X, current.Y - 1));

            case CompassDirections.NorthEast:
                return(new Point(current.X + 1, current.Y - 1));

            case CompassDirections.East:
                return(new Point(current.X + 1, current.Y));

            case CompassDirections.SouthEast:
                return(new Point(current.X + 1, current.Y + 1));

            case CompassDirections.South:
                return(new Point(current.X, current.Y + 1));

            case CompassDirections.SouthWest:
                return(new Point(current.X - 1, current.Y + 1));

            case CompassDirections.West:
                return(new Point(current.X - 1, current.Y));

            case CompassDirections.NorthWest:
                return(new Point(current.X - 1, current.Y - 1));
            }
            return(current);
        }
Exemplo n.º 3
0
 public static float GetAngleOfDirection(CompassDirections direction)
 {
     if (direction != CompassDirections.North)
     {
         if (direction == CompassDirections.NorthEast)
         {
             return(0.7853982f);
         }
         if (direction == CompassDirections.NorthWest)
         {
             return(-0.7853982f);
         }
         if (direction == CompassDirections.South)
         {
             return(3.141593f);
         }
         if (direction == CompassDirections.SouthEast)
         {
             return(2.356194f);
         }
         if (direction == CompassDirections.SouthWest)
         {
             return(3.926991f);
         }
         if (direction == CompassDirections.East)
         {
             return(1.570796f);
         }
         if (direction == CompassDirections.West)
         {
             return(-1.570796f);
         }
     }
     return(0f);
 }
Exemplo n.º 4
0
        public void Turn(RelativeDirections direction)
        {
            if (!Enum.IsDefined(typeof(RelativeDirections), direction))
            {
                throw new RelativeDirectionNotValid(direction.ToString());
            }

            CompassDirections compassDirection;

            switch (direction)
            {
            case RelativeDirections.Left:
                compassDirection = (CompassDirections)(((int)Facade + 90) % 360);
                break;

            case RelativeDirections.Right:
                compassDirection = (CompassDirections)(((int)Facade + 270) % 360);
                break;

            default:
                throw new DevelopmentException($"There is uncovered switch-case state [{direction.ToString()}]");
            }

            Facade = compassDirection;
        }
Exemplo n.º 5
0
        public AxisDirectionModel GetNewDirection(CommandDirection command, CompassDirections direction)
        {
            AxisDirectionModel modelTemp = new AxisDirectionModel();

            modelTemp.compassDirection = GetNewCompassPoint(command, direction);
            modelTemp.axisDirection    = GetNewAxis(modelTemp.compassDirection);
            return(modelTemp);
        }
Exemplo n.º 6
0
        public void Place_WhenStatusIsOff_InvaliadPosition(CompassDirections direction, int positionX, int positionY)
        {
            var       robot = new ToyRobot(new TableSurface(), _exceptionFactory);
            Exception ex    = Assert.Throws <SafeException>(() => robot.Place(new RobotPosition {
                X = positionX, Y = positionY, Direction = direction
            }));

            Assert.Equal("the robot cannot place to expected postion", ex.Message);
        }
Exemplo n.º 7
0
        protected Vehicle(int x, int y, CompassDirections facade)
        {
            if (!Enum.IsDefined(typeof(CompassDirections), facade))
            {
                throw new CompassDirectionNotValid(facade.ToString());
            }

            CurrentPoint = new Point(x, y);
            Facade       = facade;
        }
Exemplo n.º 8
0
        protected Vehicle(int x, int y, CompassDirections direction)
        {
            if (!Enum.IsDefined(typeof(CompassDirections), direction))
            {
                throw new NotValidException($"Direction is not valid {direction.ToString()}", null);
            }

            CurrentPoint = new Point(x, y);
            Direction    = direction;
        }
Exemplo n.º 9
0
 private Axis GetNewAxis(CompassDirections direction)
 {
     if (direction == CompassDirections.N || direction == CompassDirections.S)
     {
         return(Axis.Y);
     }
     else
     {
         return(Axis.X);
     }
 }
Exemplo n.º 10
0
 private CompassDirections GetNewCompassPoint(CommandDirection command, CompassDirections direction)
 {
     if (command == CommandDirection.Right)
     {
         return(GetCompassDirectionTurnRight(direction));                                   //Turn Right
     }
     else
     {
         return(GetCompassDirectionTurnLeft(direction)); //Turn Left
     }
 }
Exemplo n.º 11
0
        public void Move_WhenStatusIsOn_AtBoundary_InValidMovement(CompassDirections currentDirection, int currentPositionX, int currentPositionY)
        {
            var robot = new ToyRobot(new TableSurface(), _exceptionFactory);

            robot.Place(new RobotPosition {
                X = currentPositionX, Y = currentPositionY, Direction = currentDirection
            });

            Exception ex = Assert.Throws <SafeException>(() => robot.MoveForward());

            Assert.Equal("please place this robot first", ex.Message);
        }
Exemplo n.º 12
0
 public int GetCost(Point currentNodeLoaction, CompassDirections moveDirection)
 {
     if (moveDirection == CompassDirections.NotSet)
     {
         return(0);
     }
     if ((((moveDirection == CompassDirections.East) || (moveDirection == CompassDirections.West)) || (moveDirection == CompassDirections.South)) || (moveDirection == CompassDirections.North))
     {
         return(10);
     }
     return(14);
 }
Exemplo n.º 13
0
        public void Right_WhenStatusIsOn(CompassDirections currentDirection, CompassDirections expectedDirection)
        {
            var robot = new ToyRobot(new TableSurface(), _exceptionFactory);

            robot.Place(new RobotPosition {
                X = 2, Y = 3, Direction = currentDirection
            });
            robot.RotateToRight();

            Assert.Equal(RobotStatus.On, robot.Status);
            Assert.Equal(expectedDirection, robot.Position.Direction);
        }
Exemplo n.º 14
0
        public void Place_WhenStatusIsOff_ValiadPosition(CompassDirections direction, int positionX, int positionY)
        {
            var robot = new ToyRobot(new TableSurface(), _exceptionFactory);

            robot.Place(new RobotPosition {
                X = positionX, Y = positionY, Direction = direction
            });

            Assert.Equal(RobotStatus.On, robot.Status);
            Assert.Equal(positionX, robot.Position.X);
            Assert.Equal(positionY, robot.Position.Y);
            Assert.Equal(direction, robot.Position.Direction);
        }
Exemplo n.º 15
0
        public MarsRover(int posXValue, int posYValue, CompassDirections facingCompass, int maxX, int maxY)
        {
            // Create a class constructor with multiple parameters
            PositionX             = posXValue;
            PositionY             = posYValue;
            RoverCompassDirection = facingCompass;
            MaxPositionX          = maxX;
            MaxPositionY          = maxY;

            AxisOfMotion = GetAxis(RoverCompassDirection);

            directionObj = new Direction();
        }
Exemplo n.º 16
0
        public void Move_WhenStatusIsOn_AtBoundary_ValidMovement(CompassDirections currentDirection, int currentPositionX, int currentPositionY, int expectedPositionX, int expectedPositionY)
        {
            var robot = new ToyRobot(new TableSurface(), _exceptionFactory);

            robot.Place(new RobotPosition {
                X = currentPositionX, Y = currentPositionY, Direction = currentDirection
            });
            robot.MoveForward();

            Assert.Equal(RobotStatus.On, robot.Status);
            Assert.Equal(expectedPositionX, robot.Position.X);
            Assert.Equal(expectedPositionY, robot.Position.Y);
            Assert.Equal(currentDirection, robot.Position.Direction);
        }
Exemplo n.º 17
0
        /// <summary>
        /// Returns compass direction.
        /// </summary>
        /// <param name="azimuth">Compass deviation.</param>
        /// <returns>Compass direction.</returns>
        public static CompassDirections GetCompassDirection(float azimuth)
        {
            CompassDirections compassDirection = CompassDirections.North;

            for (int i = 0; i < _directionBoundaries.Length; i++)
            {
                if (azimuth > _directionBoundaries[i])
                {
                    compassDirection = _directionDictionary[i];
                }
            }

            return(compassDirection);
        }
Exemplo n.º 18
0
        static void Main(string[] args)
        {
            FileOperation fileOps = new FileOperation();

            int length = args.Length;

            if (args.Length > 0 && !string.IsNullOrEmpty(args[0]))
            {
                Console.WriteLine("Path:");
                Console.WriteLine(args[0]);
                string[] fileStr           = fileOps.ReadFile(args[0]);
                int[]    intArrOfMaxPoints = { 0, 0 };

                if (fileStr.Length > 0 && !string.IsNullOrEmpty(fileStr[0]))
                {
                    intArrOfMaxPoints = GetMaxPointOfArea(fileStr[0]);
                }

                Dictionary <int, string[]> marsRoverInfo = GetMarsRoverInfo(fileStr);


                StringBuilder strbuilder = new StringBuilder("");

                for (int i = 1; i <= marsRoverInfo.Count; i++)
                {
                    string[]          tempInfoArray         = marsRoverInfo[i];
                    CompassDirections roverCompassDirection = GetCompassDirectionFromLetter(tempInfoArray[2]);

                    MarsRover marsRover = new MarsRover(
                        Convert.ToInt32(tempInfoArray[0]),
                        Convert.ToInt32(tempInfoArray[1]),
                        roverCompassDirection,
                        intArrOfMaxPoints[0],
                        intArrOfMaxPoints[1]);
                    marsRover.ChangePosition(tempInfoArray[3]);

                    strbuilder.Append(marsRover.PositionX.ToString() + "\t"
                                      + marsRover.PositionY.ToString() + "\t"
                                      + marsRover.RoverCompassDirection.ToString() + Environment.NewLine);
                }

                Console.WriteLine("\n");
                Console.WriteLine("Expected Outputs:");
                Console.WriteLine(strbuilder);
            }
            else
            {
                Console.WriteLine($"Oops! File Not Found: Check args !!");
            }
        }
        public int GetCost(Point currentNodeLoaction, CompassDirections moveDirection)
        {
            if (moveDirection == CompassDirections.NotSet)
            {
                return 0;
            }

            if (moveDirection == CompassDirections.East || moveDirection == CompassDirections.West || moveDirection == CompassDirections.South || moveDirection == CompassDirections.North)
            {
                return 10;
            }

            return 14;
        }
Exemplo n.º 20
0
        /// <summary>
        /// GetAntiCompassDirections 获取当前方向相反的方向。
        /// </summary>
        public static CompassDirections GetAntiCompassDirections(CompassDirections direction)
        {
            if (direction == CompassDirections.NotSet)
            {
                return(CompassDirections.NotSet);
            }

            if (direction == CompassDirections.North)
            {
                return(CompassDirections.South);
            }

            if (direction == CompassDirections.South)
            {
                return(CompassDirections.North);
            }

            if (direction == CompassDirections.East)
            {
                return(CompassDirections.West);
            }

            if (direction == CompassDirections.West)
            {
                return(CompassDirections.East);
            }

            if (direction == CompassDirections.NorthEast)
            {
                return(CompassDirections.SouthWest);
            }

            if (direction == CompassDirections.NorthWest)
            {
                return(CompassDirections.SouthEast);
            }

            if (direction == CompassDirections.SouthWest)
            {
                return(CompassDirections.NorthEast);
            }

            if (direction == CompassDirections.SouthEast)
            {
                return(CompassDirections.NorthWest);
            }

            return(CompassDirections.NotSet);
        }
Exemplo n.º 21
0
        /// <summary>
        /// GetAntiCompassDirections 获取当前方向相反的方向。
        /// </summary>       
        public static CompassDirections GetAntiCompassDirections(CompassDirections direction)
        {
            if (direction == CompassDirections.NotSet)
            {
                return CompassDirections.NotSet;
            }

            if (direction == CompassDirections.North)
            {
                return CompassDirections.South;
            }

            if (direction == CompassDirections.South)
            {
                return CompassDirections.North;
            }

            if (direction == CompassDirections.East)
            {
                return CompassDirections.West;
            }

            if (direction == CompassDirections.West)
            {
                return CompassDirections.East;
            }

            if (direction == CompassDirections.NorthEast)
            {
                return CompassDirections.SouthWest;
            }

            if (direction == CompassDirections.NorthWest)
            {
                return CompassDirections.SouthEast;
            }

            if (direction == CompassDirections.SouthWest)
            {
                return CompassDirections.NorthEast;
            }

            if (direction == CompassDirections.SouthEast)
            {
                return CompassDirections.NorthWest;
            }

            return CompassDirections.NotSet;
        } 
Exemplo n.º 22
0
        public void ActionIsMove_And_SurfaceIsNotBigEnough__VehiclePositionShouldChange()
        {
            const int x = 0;
            const int y = 0;
            const CompassDirections direction = CompassDirections.N;

            var rover = new Rover(x, y, direction);
            var sut   = new RoverContext(_surfaceMock.Object,
                                         rover);

            _surfaceMock.Setup(surface => surface.Contains(It.IsAny <Point>()))
            .Returns(false);

            Assert.Throws <VehicleConnectionLostException>(() => sut.Move(VehicleActions.M));
        }
Exemplo n.º 23
0
        public void ActionIsTurn__VehicleTurnMethodWillBeInvoked(VehicleActions vehicleAction, CompassDirections expectedDirection)
        {
            const int x = 0;
            const int y = 0;
            const CompassDirections direction = CompassDirections.N;

            var rover = new Rover(x, y, direction);
            var sut   = new RoverContext(_surfaceMock.Object,
                                         rover);

            sut.Move(vehicleAction);

            Assert.Equal(x, sut.Vehicle.CurrentPoint.X);
            Assert.Equal(y, sut.Vehicle.CurrentPoint.Y);
            Assert.Equal(expectedDirection, sut.Vehicle.Facade);
        }
Exemplo n.º 24
0
        public void ActionIsMove_And_SurfaceIsBigEnough__VehiclePositionShouldChange()
        {
            const int x = 0;
            const int y = 0;
            const CompassDirections direction = CompassDirections.N;

            var rover = new Rover(x, y, direction);
            var sut   = new RoverContext(_surfaceMock.Object,
                                         rover);

            sut.Move(VehicleActions.M);

            Assert.Equal(x, sut.Vehicle.CurrentPoint.X);
            Assert.Equal(y + 1, sut.Vehicle.CurrentPoint.Y);
            Assert.Equal(direction, sut.Vehicle.Facade);
        }
Exemplo n.º 25
0
        public void Turn(RelativeDirections relativeDirection)
        {
            if (!Enum.IsDefined(typeof(RelativeDirections), relativeDirection))
            {
                throw new NotValidException($"Direction is not valid ({relativeDirection.ToString()})", null);
            }

            var compassDirection = relativeDirection switch
            {
                RelativeDirections.Left => (CompassDirections)(((int)Direction + 90) % 360),
                RelativeDirections.Right => (CompassDirections)(((int)Direction + 270) % 360),
                _ => throw new Exception($"Direction is not implemented ({relativeDirection.ToString()})")
            };

            Direction = compassDirection;
        }
Exemplo n.º 26
0
        public Netterpillar(int x, int y, Sprite.CompassDirections initialDirection, bool isComputer)
        {
            NetterBody = new NetterBody[25 + 1];
            int incX = 0, incY = 0;

            IsComputer  = isComputer;
            NetterHeadN = Load(Application.StartupPath + "\\" + IMAGE_PATH + "\\" + (IsComputer ? "" : "Player") + "NetterHeadN.gif");
            NetterHeadS = Load(Application.StartupPath + "\\" + IMAGE_PATH + "\\" + (IsComputer ? "" : "Player") + "NetterHeadS.gif");
            NetterHeadE = Load(Application.StartupPath + "\\" + IMAGE_PATH + "\\" + (IsComputer ? "" : "Player") + "NetterHeadE.gif");
            NetterHeadW = Load(Application.StartupPath + "\\" + IMAGE_PATH + "\\" + (IsComputer ? "" : "Player") + "NetterHeadW.gif");
            for (int i = 0; i < NetterBodyLength; i++)
            {
                NetterBody[i] = new NetterBody(IsComputer);
            }

            // Position the Netterpillar on the given point
            Direction  = initialDirection;
            Location.X = x;
            Location.Y = y;
            // Position each of the body parts
            switch (Direction)
            {
            case Sprite.CompassDirections.East:
                incX = -1;
                break;

            case Sprite.CompassDirections.South:
                incY = -1;
                break;

            case Sprite.CompassDirections.West:
                incX = 1;
                break;

            case Sprite.CompassDirections.North:
                incY = 1;
                break;
            }
            for (int i = 0; i < NetterBodyLength; i++)
            {
                x += incX;
                y += incY;
                NetterBody[i].Location.X = x;
                NetterBody[i].Location.Y = y;
            }
        }
Exemplo n.º 27
0
        private Axis GetAxis(CompassDirections compassPoint)
        {
            Axis retVal = Axis.X;

            switch (compassPoint)
            {
            case CompassDirections.N:
            case CompassDirections.S:
                retVal = Axis.Y;
                break;

            case CompassDirections.W:
            case CompassDirections.E:
                retVal = Axis.X;
                break;
            }
            return(retVal);
        }
Exemplo n.º 28
0
        /// <summary>
        /// GetAngleOfDirection 获取当前方向与CompassDirections.North的夹角。
        /// </summary>       
        public static float GetAngleOfDirection(CompassDirections direction)
        {
            if (direction == CompassDirections.North)
            {
                return RotationAngles.A0;
            }

            if (direction == CompassDirections.NorthEast)
            {
                return RotationAngles.A45;
            }

            if (direction == CompassDirections.NorthWest)
            {
                return RotationAngles.A315;
            }

            if (direction == CompassDirections.South)
            {
                return RotationAngles.A180;
            }

            if (direction == CompassDirections.SouthEast)
            {
                return RotationAngles.A135;
            }

            if (direction == CompassDirections.SouthWest)
            {
                return RotationAngles.A225;
            }

            if (direction == CompassDirections.East)
            {
                return RotationAngles.A90;
            }

            if (direction == CompassDirections.West)
            {
                return RotationAngles.A270;
            }

            return RotationAngles.A0;
        }
Exemplo n.º 29
0
        /// <summary>
        /// GetAngleOfDirection 获取当前方向与CompassDirections.North的夹角。
        /// </summary>
        public static float GetAngleOfDirection(CompassDirections direction)
        {
            if (direction == CompassDirections.North)
            {
                return(RotationAngles.A0);
            }

            if (direction == CompassDirections.NorthEast)
            {
                return(RotationAngles.A45);
            }

            if (direction == CompassDirections.NorthWest)
            {
                return(RotationAngles.A315);
            }

            if (direction == CompassDirections.South)
            {
                return(RotationAngles.A180);
            }

            if (direction == CompassDirections.SouthEast)
            {
                return(RotationAngles.A135);
            }

            if (direction == CompassDirections.SouthWest)
            {
                return(RotationAngles.A225);
            }

            if (direction == CompassDirections.East)
            {
                return(RotationAngles.A90);
            }

            if (direction == CompassDirections.West)
            {
                return(RotationAngles.A270);
            }

            return(RotationAngles.A0);
        }
Exemplo n.º 30
0
 /// <summary>
 /// GetAdjacentPoint 获取某个方向上的相邻点
 /// </summary>       
 public static Point GetAdjacentPoint(Point current, CompassDirections direction)
 {
     switch (direction)
     {
         case CompassDirections.North:
             {
                 return new Point(current.R, current.C - 1);
             }
         case CompassDirections.South:
             {
                 return new Point(current.R, current.C + 1);
             }
         case CompassDirections.East:
             {
                 return new Point(current.R + 1, current.C);
             }
         case CompassDirections.West:
             {
                 return new Point(current.R - 1, current.C);
             }
         case CompassDirections.NorthEast:
             {
                 return new Point(current.R + 1, current.C - 1);
             }
         case CompassDirections.NorthWest:
             {
                 return new Point(current.R - 1, current.C - 1);
             }
         case CompassDirections.SouthEast:
             {
                 return new Point(current.R + 1, current.C + 1);
             }
         case CompassDirections.SouthWest:
             {
                 return new Point(current.R - 1, current.C + 1);
             }
         default:
             {
                 return current;
             }
     }
 }
        public Branch(CompassDirections branchDirection, int initialSize)
        {
            BranchMiddle = new Bitmap[initialSize-2];
            string imagePrefix;

            branchSize = initialSize;
            Direction = branchDirection;
            // Picks the prefix for the branch - Horizontal or vertical?
            imagePrefix = "Hor"; // Default direction is east-west (Horizontal)
            if (Direction==Sprite.CompassDirections.North||Direction==Sprite.CompassDirections.South) {
                imagePrefix = "Vert";
            }
            // Load the top, the middle parts and the end of the branch
            //  Magenta is the colorkey (which will be transparent) for the Load Method
            BranchStart = Load(Application.StartupPath+"\\"+IMAGE_PATH+"\\"+imagePrefix+"BranchStart.gif", Color.FromArgb(255, 255, 0, 204));
            for(int i=0; i<=branchSize-3; i++) {
                BranchMiddle[i] = Load(Application.StartupPath+"\\"+IMAGE_PATH+"\\"+imagePrefix+"BranchMiddle.gif", Color.FromArgb(255, 255, 0, 204));
            }
            BranchEnd = Load(Application.StartupPath+"\\"+IMAGE_PATH+"\\"+imagePrefix+"BranchEnd.gif", Color.FromArgb(255, 255, 0, 204));
        }
Exemplo n.º 32
0
        public Branch(CompassDirections branchDirection, int initialSize)
        {
            BranchMiddle = new Bitmap[initialSize - 2];
            string imagePrefix;

            branchSize = initialSize;
            Direction  = branchDirection;
            // Picks the prefix for the branch - Horizontal or vertical?
            imagePrefix = "Hor";             // Default direction is east-west (Horizontal)
            if (Direction == Sprite.CompassDirections.North || Direction == Sprite.CompassDirections.South)
            {
                imagePrefix = "Vert";
            }
            // Load the top, the middle parts and the end of the branch
            //  Magenta is the colorkey (which will be transparent) for the Load Method
            BranchStart = Load(Application.StartupPath + "\\" + IMAGE_PATH + "\\" + imagePrefix + "BranchStart.gif", Color.FromArgb(255, 255, 0, 204));
            for (int i = 0; i <= branchSize - 3; i++)
            {
                BranchMiddle[i] = Load(Application.StartupPath + "\\" + IMAGE_PATH + "\\" + imagePrefix + "BranchMiddle.gif", Color.FromArgb(255, 255, 0, 204));
            }
            BranchEnd = Load(Application.StartupPath + "\\" + IMAGE_PATH + "\\" + imagePrefix + "BranchEnd.gif", Color.FromArgb(255, 255, 0, 204));
        }
Exemplo n.º 33
0
        public static CompassDirections GetCompassDirectionFromLetter(string text)
        {
            CompassDirections tempCompassDirection = CompassDirections.N;

            switch (text)
            {
            case "N":
                tempCompassDirection = CompassDirections.N;
                break;

            case "E":
                tempCompassDirection = CompassDirections.E;
                break;

            case "S":
                tempCompassDirection = CompassDirections.S;
                break;

            case "W":
                tempCompassDirection = CompassDirections.W;
                break;
            }
            return(tempCompassDirection);
        }
Exemplo n.º 34
0
 public Rover(int x, int y, CompassDirections direction) : base(x, y, direction)
 {
 }
Exemplo n.º 35
0
 public Rover(int x, int y, CompassDirections facade) : base(x, y, facade)
 {
 }