Пример #1
0
        public void RobotMovesOffGrid()
        {
            _grid = new Grid(30, 30);
            _coord = new Coordinate(0, 0);
            _robot = new Robot(_coord, Orientation.S, _grid);

            _robot.Move(Instruction.M, _grid);
        }
Пример #2
0
        public void CreateRobot()
        {
            _grid = new Grid(30, 30);
            _coord = new Coordinate(10, 11);
            _robot = new Robot(_coord, Orientation.N, _grid);

            Assert.AreEqual(_coord.X, _robot.Pos.X);
            Assert.AreEqual(_coord.Y, _robot.Pos.Y);
            Assert.AreEqual(Orientation.N, _robot.Or);
        }
Пример #3
0
        public Robot(Coordinate pos, Orientation or, Grid grid)
        {
            if (!grid.IsValid(pos))
            {
                throw new Exception("Robot placed outside of grid");
            }

            _pos = pos;
            _or = or;
        }
Пример #4
0
 public void Move(Instruction inst, Grid grid)
 {
     switch (inst)
     {
         //Modulus used to cycle orientations. Limited definition of modulus with reference to negative
         //numbers result in initial incrementation by setsize to guarantee  positive result
         case Instruction.R:
             _or = (Orientation)Enum.ToObject(typeof(Orientation),
                 (((int)_or + NumOrientations + 1) % NumOrientations));
             break;
         case Instruction.L:
             _or = (Orientation)Enum.ToObject(typeof(Orientation),
                 (((int)_or + NumOrientations - 1) % NumOrientations));
             break;
         case Instruction.M:
             Coordinate newPos;
             switch (_or)
             {
                 case Orientation.N:
                     newPos = new Coordinate(_pos.X, _pos.Y + 1);
                     break;
                 case Orientation.E:
                     newPos = new Coordinate(_pos.X + 1, _pos.Y);
                     break;
                 case Orientation.S:
                     newPos = new Coordinate(_pos.X, _pos.Y - 1);
                     break;
                 case Orientation.W:
                     newPos = new Coordinate(_pos.X - 1, _pos.Y);
                     break;
                 default:
                     newPos = new Coordinate(-1, -1);
                     break;
             }
             if (grid.IsValid(newPos))
             {
                 _pos = newPos;
             }
             else
             {
                 throw new InvalidOperationException();
             }
         break;
     }
 }
Пример #5
0
        public void FirstRobotMovementTest()
        {
            //1 2 N
            //LMLMLMLMM

            _grid = new Grid(5, 5);
            _robot = new Robot(new Coordinate(1, 2), Orientation.N, _grid);

            _robot.Move(Instruction.L, _grid);
            _robot.Move(Instruction.M, _grid);
            _robot.Move(Instruction.L, _grid);
            _robot.Move(Instruction.M, _grid);
            _robot.Move(Instruction.L, _grid);
            _robot.Move(Instruction.M, _grid);
            _robot.Move(Instruction.L, _grid);
            _robot.Move(Instruction.M, _grid);
            _robot.Move(Instruction.M, _grid);

            Assert.AreEqual(1, _robot.Pos.X);
            Assert.AreEqual(3, _robot.Pos.Y);
            Assert.AreEqual(Orientation.N, _robot.Or);
        }
Пример #6
0
 public void TestIsValidTrue()
 {
     var grid = new Grid(30,30);
     Assert.IsTrue(grid.IsValid(new Coordinate(30,1)));
 }
Пример #7
0
 public void TestIsValidFalseTooHigh()
 {
     var grid = new Grid(30,30);
     Assert.IsFalse(grid.IsValid(new Coordinate(31,3)));
 }
Пример #8
0
 public void TestIsValidFalseNeg()
 {
     var grid = new Grid(30,30);
     Assert.IsFalse(grid.IsValid(new Coordinate(20,-1)));
 }
Пример #9
0
 public void CreateRobotInvalidPosition()
 {
     _grid = new Grid(30, 30);
     _coord = new Coordinate(1, -1);
     _robot = new Robot(_coord, Orientation.N, _grid);
 }
Пример #10
0
 /// <summary>
 ///  Static method creates singleton grid of specified width and height
 /// </summary>
 private static void CreateGrid(int width, int height)
 {
     _grid = new Grid(width, height);
 }