Difference between revisions of "Add an option on the menu to find the largest warren"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Finding the largest warren)
(Finding the largest warren)
Line 11: Line 11:
 
         for (int b = 0; b < LandscapeSize; b++)
 
         for (int b = 0; b < LandscapeSize; b++)
 
         {
 
         {
            if (Landscape[a, b].Warren != null && Landscape[a, b].Warren.GetRabbitCount() > maxWarrenCheck)
+
        if (Landscape[a, b].Warren != null && Landscape[a, b].Warren.GetRabbitCount() > maxWarrenCheck)
                            {
+
        {
                                maxWarrenCheck = Landscape[a, b].Warren.GetRabbitCount();
+
            maxWarrenCheck = Landscape[a, b].Warren.GetRabbitCount();
                                currentX = a;
+
            currentX = a;
                                currentY = b;
+
            currentY = b;
                            }
+
        }
                        }
+
      }
                    }
+
}
                    Console.WriteLine("Max warren is at {0},{1} and is {2} rabbits large", currentX, currentY, maxWarrenCheck);
+
  Console.WriteLine("Max warren is at {0},{1} and is {2} rabbits large", currentX, currentY, maxWarrenCheck);
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
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.

Revision as of 14:07, 14 March 2017

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.