Difference between revisions of "A predator for foxes - eagles"
(→In the Simulation class) |
(→Giving the Eagle a range) |
||
(8 intermediate revisions by the same user not shown) | |||
Line 102: | Line 102: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | *In the Simulation after the final iteration structure | + | *In the Simulation after the final iteration structure, If the timeperiod is a multiple of 3 then call the newly created CreateNewEagle method. |
− | + | <syntaxhighlight lang=csharp> | |
+ | if (TimePeriod % 3 == 0) | ||
+ | CreateNewEagle(); | ||
+ | </syntaxhighlight> | ||
*In the Simulation class – adapt the draw landscape so that if there is an Eagle in a location then an E is placed on the board. | *In the Simulation class – adapt the draw landscape so that if there is an Eagle in a location then an E is placed on the board. | ||
− | |||
− | ==Create a new procedure called | + | <syntaxhighlight lang=csharp> |
+ | if (Landscape[x, y].Eagle != null) | ||
+ | { | ||
+ | Console.Write("E"); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | Console.Write(" "); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | *Test the program to check that an Eagle appears after 3 time periods | ||
+ | |||
+ | ==Create a new procedure called EaglesEatFoxes that accepts the parameters of the EaglesX and EaglesY co-ordinate.== | ||
Create a new variable called FoxKilled and set to False | Create a new variable called FoxKilled and set to False | ||
Line 115: | Line 130: | ||
The procedure will search through every position on the Landscape and as long as FoxKilled = False the Eagle will attempt to Kill a fox. | The procedure will search through every position on the Landscape and as long as FoxKilled = False the Eagle will attempt to Kill a fox. | ||
− | The Eagle has a 30% chance of killing a fox – if it is unsuccessful it will then attempt to kill the next fox (and so on). | + | The Eagle has a 30% chance of killing a fox – if it is unsuccessful it will then attempt to kill the next fox (and so on). When the Eagle kills a fox set FoxKilled to true, this should prevent the eagle from killing any other foxes. |
− | + | A message is displayed to the user: | |
Eagle killed Fox at positon (X, Y) | Eagle killed Fox at positon (X, Y) | ||
Line 124: | Line 139: | ||
The Landscape Fox is set to Nothing. | The Landscape Fox is set to Nothing. | ||
+ | <syntaxhighlight lang=csharp> | ||
+ | private void EaglesEatFoxes(int EagleX, int EagleY) | ||
+ | { | ||
+ | bool FoxKilled = false; | ||
+ | |||
+ | for (int FoxX = 0; FoxX < LandscapeSize; FoxX++) | ||
+ | { | ||
+ | for (int FoxY = 0; FoxY < LandscapeSize; FoxY++) | ||
+ | { | ||
+ | if (Landscape[FoxX, FoxY].Fox != null && !FoxKilled) | ||
+ | { | ||
+ | int test = Rnd.Next(0, 100); | ||
+ | if (test % 5 == 0) | ||
+ | { | ||
+ | Console.WriteLine("Eagle killed fox at " + FoxX + "," + FoxY); | ||
+ | FoxKilled = true; | ||
+ | FoxCount--; | ||
+ | Landscape[FoxX, FoxY].Fox = null; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
==Study the AdvanceTimePeriod in the Simulation Class== | ==Study the AdvanceTimePeriod in the Simulation Class== | ||
Line 139: | Line 178: | ||
*If the Eagle is not killed then call the procedure: EaglesKillFoxes with the X & Y co-ordinates | *If the Eagle is not killed then call the procedure: EaglesKillFoxes with the X & Y co-ordinates | ||
*The If Show Detail is True inspect the Eagle | *The If Show Detail is True inspect the Eagle | ||
+ | |||
+ | <syntaxhighlight lang=csharp> | ||
+ | for (int x = 0; x < LandscapeSize; x++) | ||
+ | { | ||
+ | for (int y = 0; y < LandscapeSize; y++) | ||
+ | { | ||
+ | if (Landscape[x, y].Eagle != null) | ||
+ | { | ||
+ | if (ShowDetail) | ||
+ | { | ||
+ | Console.WriteLine("Eagle at (" + x + "," + y + "): "); | ||
+ | } | ||
+ | |||
+ | Landscape[x, y].Eagle.AdvanceGeneration(ShowDetail); | ||
+ | if (Landscape[x, y].Eagle.CheckIfDead()) | ||
+ | { | ||
+ | Landscape[x, y].Eagle = null; | ||
+ | EagleCount--; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | EaglesEatFoxes(x, y); | ||
+ | if (ShowDetail) | ||
+ | { | ||
+ | Landscape[x, y].Eagle.Inspect(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
Test this out showing detail and using pre-set values in order to check that the Eagle kills a fox | Test this out showing detail and using pre-set values in order to check that the Eagle kills a fox | ||
− | == | + | ==Giving the Eagle a range== |
+ | Within your EagleEatsFoxes method above, change the if statement to this instead: | ||
+ | |||
+ | <syntaxhighlight lang=csharp> | ||
+ | if (Landscape[FoxX, FoxY].Fox != null && !FoxKilled && DistanceBetween(FoxX, FoxY, EagleX, EagleY)<7) | ||
+ | </syntaxhighlight> |
Latest revision as of 06:55, 25 May 2017
Contents
- 1 Create the outline class for an Eagle Class
- 2 Create a new method within the Eagle class called AdvanceGeneration – this will take ShowDetail as a parameter
- 3 Adapt the Location Class to include an Eagle
- 4 In the Simulation class
- 5 Create a new procedure called EaglesEatFoxes that accepts the parameters of the EaglesX and EaglesY co-ordinate.
- 6 Study the AdvanceTimePeriod in the Simulation Class
- 7 Giving the Eagle a range
Create the outline class for an Eagle Class
- The class will inherit from the Animal class
- This will have two Constants:
- DefaultLifeSpan which will be of 6
- Probability of Death of value 0.05
- There will be a constructor class that makes use of the base class.
The Code
class Eagle : Animal
{
private const int DefaultLifespan = 6;
private const double DefaultProbabilityDeath = 0.05;
public Eagle(int Variability)
: base(DefaultLifespan, DefaultProbabilityDeath, Variability)
{
}
}
Create a new method within the Eagle class called AdvanceGeneration – this will take ShowDetail as a parameter
- The procedure will check if the Eagle has been killed by other factors
- if it has then it will state that the Eagle has been killed.
- If it does not get killed by other factors – the Age will be calculated and again it will check to see if the Eagle has died.
- If the Eagle has dies it will state “Eagle died of Old Age”
The Code
public void AdvanceGeneration(bool ShowDetail)
{
if (CheckIfKilledByOtherFactor())
{
IsAlive = false;
if (ShowDetail)
{
Console.WriteLine(" Eagle killed by other factor.");
}
}
else
{
CalculateNewAge();
}
if (!IsAlive)
{
if (ShowDetail)
{
Console.WriteLine(" Eagle has died of old age.");
}
}
}
I was able to copy most of this code out of the fox version of advance generation.
Adapt the Location Class to include an Eagle
This is my new Location class after adding the Eagle:
class Location
{
public Fox Fox;
public Warren Warren;
public Eagle Eagle;
public Location()
{
Fox = null;
Warren = null;
Eagle = null;
}
}
In the Simulation class
- Create a new Private attribute called EagleCount and set as zero
private int EagleCount = 0;
- Create a new Sub Routine called CreateNewEagle() – this will work in the same way as CreateNewFox() but based on Eagle rather than Fox.
private void CreateNewEagle()
{
int x, y;
do
{
x = Rnd.Next(0, LandscapeSize);
y = Rnd.Next(0, LandscapeSize);
} while (Landscape[x, y].Eagle != null);
if (ShowDetail)
{
Console.WriteLine(" New Eagle at (" + x + "," + y + ")");
}
Landscape[x, y].Eagle = new Eagle(Variability);
EagleCount++;
}
- In the Simulation after the final iteration structure, If the timeperiod is a multiple of 3 then call the newly created CreateNewEagle method.
if (TimePeriod % 3 == 0)
CreateNewEagle();
- In the Simulation class – adapt the draw landscape so that if there is an Eagle in a location then an E is placed on the board.
if (Landscape[x, y].Eagle != null)
{
Console.Write("E");
}
else
{
Console.Write(" ");
}
- Test the program to check that an Eagle appears after 3 time periods
Create a new procedure called EaglesEatFoxes that accepts the parameters of the EaglesX and EaglesY co-ordinate.
Create a new variable called FoxKilled and set to False
The procedure will search through every position on the Landscape and as long as FoxKilled = False the Eagle will attempt to Kill a fox.
The Eagle has a 30% chance of killing a fox – if it is unsuccessful it will then attempt to kill the next fox (and so on). When the Eagle kills a fox set FoxKilled to true, this should prevent the eagle from killing any other foxes.
A message is displayed to the user: Eagle killed Fox at positon (X, Y)
FoxCount is decremented by 1
The Landscape Fox is set to Nothing.
private void EaglesEatFoxes(int EagleX, int EagleY)
{
bool FoxKilled = false;
for (int FoxX = 0; FoxX < LandscapeSize; FoxX++)
{
for (int FoxY = 0; FoxY < LandscapeSize; FoxY++)
{
if (Landscape[FoxX, FoxY].Fox != null && !FoxKilled)
{
int test = Rnd.Next(0, 100);
if (test % 5 == 0)
{
Console.WriteLine("Eagle killed fox at " + FoxX + "," + FoxY);
FoxKilled = true;
FoxCount--;
Landscape[FoxX, FoxY].Fox = null;
}
}
}
}
}
Study the AdvanceTimePeriod in the Simulation Class
You will notice that there is a nested For Loop to go through each Warren and there is a nested For Loop to look for each Fox.
- Create a new Nested For Loop that looks for each Eagle
- If an Eagle is present then:
If ShowDetail then display: Eagle at (X,Y) Advance a generation for the Eagle
- If The Eagle at that position in the landscape is Dead then set the location Eagle aspect to Nothing and decrement EagleCount
- If the Eagle is not killed then call the procedure: EaglesKillFoxes with the X & Y co-ordinates
- The If Show Detail is True inspect the Eagle
for (int x = 0; x < LandscapeSize; x++)
{
for (int y = 0; y < LandscapeSize; y++)
{
if (Landscape[x, y].Eagle != null)
{
if (ShowDetail)
{
Console.WriteLine("Eagle at (" + x + "," + y + "): ");
}
Landscape[x, y].Eagle.AdvanceGeneration(ShowDetail);
if (Landscape[x, y].Eagle.CheckIfDead())
{
Landscape[x, y].Eagle = null;
EagleCount--;
}
else
{
EaglesEatFoxes(x, y);
if (ShowDetail)
{
Landscape[x, y].Eagle.Inspect();
}
}
}
}
}
Test this out showing detail and using pre-set values in order to check that the Eagle kills a fox
Giving the Eagle a range
Within your EagleEatsFoxes method above, change the if statement to this instead:
if (Landscape[FoxX, FoxY].Fox != null && !FoxKilled && DistanceBetween(FoxX, FoxY, EagleX, EagleY)<7)