Add an option on the menu to find the largest warren

From TRCCompSci - AQA Computer Science
Revision as of 13:07, 14 March 2017 by Alex1 (talk | contribs) (Finding the largest warren)
Jump to: navigation, search

Finding the largest warren

new code:

 1  
 2 if (menuOption == 5)
 3 {
 4    int maxWarrenCheck = 0;
 5    int currentX = 0;
 6    int currentY = 0;
 7       for (int a = 0; a < LandscapeSize; a++)
 8       {
 9          for (int b = 0; b < LandscapeSize; b++)
10          {
11          if (Landscape[a, b].Warren != null && Landscape[a, b].Warren.GetRabbitCount() > maxWarrenCheck)
12          {
13             maxWarrenCheck = Landscape[a, b].Warren.GetRabbitCount();
14             currentX = a;
15             currentY = b;
16          }
17        }
18 }
19    Console.WriteLine("Max warren is at {0},{1} and is {2} rabbits large", currentX, currentY, maxWarrenCheck);

The two for loops on L7 and L9 loop through each square on the grid. L11 Checks if there is a warren there and if the warren is larger than the max found so far. L13 - L15 Stores the new maximum warren size and its location L19 Outputs the maximum warren position and its size.