Display a message when a cell with no warren is selected for warren inspection...Adding a loop too

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

Problem

when you inspect a warren during a simulation, and you enter coordinates that don't contain a warren no message is displayed. The code below is from the Simulation class:

 1 if (menuOption == 4)
 2 {
 3 	x = InputCoordinate('x');
 4 	y = InputCoordinate('y');
 5 	if (Landscape[x, y].Warren != null)
 6 	{
 7 		Landscape[x, y].Warren.Inspect();
 8 		Console.Write("View individual rabbits (y/n)?");
 9 		viewRabbits = Console.ReadLine();
10 		if (viewRabbits == "y")
11 		{
12 			Landscape[x, y].Warren.ListRabbits();
13 		}
14 	}
15 }


Solution

 1 if (menuOption == 4)
 2 {
 3 	x = InputCoordinate('x');
 4 	y = InputCoordinate('y');
 5 	if (Landscape[x, y].Warren != null)
 6 	{
 7 		Landscape[x, y].Warren.Inspect();
 8 		Console.Write("View individual rabbits (y/n)?");
 9 		viewRabbits = Console.ReadLine();
10 		if (viewRabbits == "y")
11 		{
12 			Landscape[x, y].Warren.ListRabbits();
13 		}
14 	}
15         else
16         {
17                 Console.WriteLine("the coordinates entered do not contain a warren!!");
18         }
19 }