Validation for menu options for the coordinate input

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

Checking coordinates

The InputCoordinate method is within the Simulation class, see below:

1 private int InputCoordinate(char Coordinatename)
2 {
3 	int Coordinate;
4 	Console.Write("  Input " + Coordinatename + " coordinate: ");
5 	Coordinate = Convert.ToInt32(Console.ReadLine());
6 	return Coordinate;
7 }


This is called everytime a coordinate is required. It can easily be validated because at the LandscapeSize variable could be used. So:

 1 private int InputCoordinate(char Coordinatename)
 2 {
 3         do
 4         {
 5 	      Console.Write("  Input " + Coordinatename + " coordinate: ");
 6 	      Coordinate = Convert.ToInt32(Console.ReadLine());
 7         }
 8 	while (Coordinate < 0 || Coordinate >= LandscapeSize);
 9 	return Coordinate;
10 }


The Coordinate is invalid if it is less than zero or greater than or equal to the LandscapeSize (ie first element is 0).