Пример #1
0
        public void Move_TowardsAHoleLeadsToSpinnerThenTowardsRock_BehavesAsExpectedForAllObstacles()
        {
            Grid grid      = new Grid(3, 3);
            var  startAt   = new Cell(2, 2); //at center
            var  facing    = CardinalDirection.North;
            var  obstacles = new Dictionary <Cell, Obstacle>();

            //add a hole to the left connected to a cell within the grid
            var cellWithHole  = new Cell(2, 1);
            var cellAfterHole = new Cell(3, 3);

            obstacles.Add(cellWithHole, new Hole(cellAfterHole));

            //add a spinner with 180 degrees rotation, so that robot will face south
            obstacles.Add(cellAfterHole, new Spinner(180));

            //add a rock to the right
            var cellWithRock = new Cell(3, 2);

            obstacles.Add(cellWithRock, new Rock());

            var robot = new Type1Robot(grid, startAt, facing, obstacles);

            //robot moved left and encounters the hole, falls to a new cell where it encounters the spinner.
            //then robot tries to move right but encounters a rock
            //Hence, robot now faces south, and remains at the cell after hole
            robot.Move(RelativeDirection.Left);
            robot.Move(RelativeDirection.Right);
            Assert.IsTrue(robot.Facing.Equals(CardinalDirection.South));
            Assert.IsTrue(robot.At.Equals(cellAfterHole));
        }
Пример #2
0
        public void Move_TowardsLeftFromExtremeLeft_ThrowsInvalidOperationException()
        {
            Grid grid      = new Grid(3, 3);
            var  startAt   = new Cell(2, 1); //in leftmost column
            var  facing    = CardinalDirection.North;
            var  obstacles = new Dictionary <Cell, Obstacle>()
            {
            };

            var robot = new Type1Robot(grid, startAt, facing, obstacles);

            //attempt moving to the left
            robot.Move(RelativeDirection.Left);
        }
Пример #3
0
        public void Constructor_ValidParameters_AllPropertiesAreInitialized()
        {
            Grid grid      = new Grid(3, 3);
            var  startAt   = new Cell(2, 2);
            var  facing    = CardinalDirection.North;
            var  obstacles = new Dictionary <Cell, Obstacle>();

            var robot = new Type1Robot(grid, startAt, facing, obstacles);

            Assert.IsTrue(robot.Grid.Equals(grid));
            Assert.IsTrue(robot.At.Equals(startAt));
            Assert.IsTrue(robot.Facing.Equals(facing));
            Assert.AreEqual(robot.Obstacles.Count, 0);
        }
Пример #4
0
        public void Move_TowardsARock_RemainsAtSameLocation()
        {
            Grid grid      = new Grid(3, 3);
            var  startAt   = new Cell(2, 2); //at center
            var  facing    = CardinalDirection.North;
            var  obstacles = new Dictionary <Cell, Obstacle>();

            //add a rock to the left
            var cellWithObstacle = new Cell(2, 1);

            obstacles.Add(cellWithObstacle, new Rock());

            var robot = new Type1Robot(grid, startAt, facing, obstacles);

            robot.Move(RelativeDirection.Left);

            Assert.IsTrue(robot.At.Equals(startAt));
        }
Пример #5
0
        public void Move_TowardsASpinnerWithRotation90FacingNorth_FacesEastSameLocation()
        {
            Grid grid      = new Grid(3, 3);
            var  startAt   = new Cell(2, 2); //at center
            var  facing    = CardinalDirection.North;
            var  obstacles = new Dictionary <Cell, Obstacle>();

            //add a hole to the left with 90 degrees rotation
            var cellWithObstacle = new Cell(2, 1);

            obstacles.Add(cellWithObstacle, new Spinner(90));

            var robot = new Type1Robot(grid, startAt, facing, obstacles);

            //robot encounters the spinner and spins to face a new direction
            //robot location remains the same
            robot.Move(RelativeDirection.Left);
            Assert.IsTrue(robot.Facing.Equals(CardinalDirection.East));
            Assert.IsTrue(robot.At.Equals(cellWithObstacle));
        }
Пример #6
0
        public void Move_TowardsAHole_MovesToConnectedCellFacingSame()
        {
            Grid grid      = new Grid(3, 3);
            var  startAt   = new Cell(2, 2); //at center
            var  facing    = CardinalDirection.North;
            var  obstacles = new Dictionary <Cell, Obstacle>();

            //add a hole to the left connected to a cell within the grid
            var cellWithObstacle = new Cell(2, 1);
            var connectedCell    = new Cell(3, 3);

            obstacles.Add(cellWithObstacle, new Hole(connectedCell));

            var robot = new Type1Robot(grid, startAt, facing, obstacles);

            //robot encounters the hole and moves to the connected cell
            //robot direction remains the same
            robot.Move(RelativeDirection.Left);
            Assert.IsTrue(robot.At.Equals(connectedCell));
            Assert.IsTrue(robot.Facing.Equals(facing));
        }
Пример #7
0
        static void Main(string[] args)
        {
            //welcome user
            Highlight(@"
--------------------------------------------------------------------
Hi! I am Gridomatic the Robot. I can move on any 2-dimensional grid. 
You can control my movement.
--------------------------------------------------------------------
");

            Console.WriteLine(@"I've been configured as follows...

Grid size: 3*3
Start location: [2,2]
Start direction: North

Obstacles:
    - Hole at location [2,1] connected to [3,3]
    - Spinner at location [3,3]
    - Rock at location [3,2]
");

            var grid      = new Grid(3, 3);
            var startAt   = new Cell(2, 2);
            var facing    = CardinalDirection.North;
            var obstacles = new Dictionary <Cell, Obstacle>();

            obstacles.Add(new Cell(2, 1), new Hole(new Cell(3, 3)));
            obstacles.Add(new Cell(3, 3), new Spinner(180));
            obstacles.Add(new Cell(3, 2), new Rock());

            var robot = new Type1Robot(grid, startAt, facing, obstacles);

            Highlight(@"
---------------------------------------------------------------------
Please command me as follows:
-- Press L for left
-- Press R for right
-- Press F for front
-- Press B for back
-- Press ESC to stop
---------------------------------------------------------------------

");

            //take series of commands from user to move, for example. Stop when user presses ESC
            while (true)
            {
                RelativeDirection?towards = GetNextCommand();
                if (towards == null)
                {
                    Highlight(@"
--------------------------------------------------------------------
I'm done for the day. Good bye!
--------------------------------------------------------------------
");
                    break;
                }
                try
                {
                    robot.Move((RelativeDirection)towards);
                    Highlight(String.Format(@"
I'm now at [{0},{1}] facing {2}.
",
                                            robot.At.Row, robot.At.Column, robot.Facing));
                }
                catch (InvalidOperationException)
                {
                    Console.WriteLine("\nError: I'm falling off the grid! Please try another move.\n");
                }
            }
        }