Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 31.08 KB | None | 0 0
  1. Module Module1
  2.     'Skeleton Program code for the AQA A Level Paper 1 2017 examination
  3.     'this code should be used in conjunction with the Preliminary Material
  4.     'written by the AQA Programmer Team
  5.     'developed in the Visual Studio 2008 programming environment
  6.  
  7.  
  8.     Sub Main()
  9.             Dim MenuOption As Integer
  10.             Dim LandscapeSize As Integer
  11.             Dim InitialWarrenCount As Integer
  12.             Dim InitialFoxCount As Integer
  13.             Dim Variability As Integer
  14.             Dim FixedInitialLocations As Boolean
  15.             Do
  16.                 Console.WriteLine("Predator Prey Simulation Main Menu")
  17.                 Console.WriteLine()
  18.                 Console.WriteLine("1. Run simulation with default settings")
  19.                 Console.WriteLine("2. Run simulation with custom settings")
  20.                 Console.WriteLine("3. Exit")
  21.                 Console.WriteLine()
  22.                 Console.Write("Select option: ")
  23.                 MenuOption = CInt(Console.ReadLine())
  24.                 If MenuOption = 1 Or MenuOption = 2 Then
  25.                     If MenuOption = 1 Then
  26.                         LandscapeSize = 15
  27.                         InitialWarrenCount = 5
  28.                         InitialFoxCount = 5
  29.                         Variability = 0
  30.                         FixedInitialLocations = True
  31.                     Else
  32.                         Console.Write("Landscape Size: ")
  33.                         LandscapeSize = CInt(Console.ReadLine())
  34.                         Console.Write("Initial number of warrens: ")
  35.                         InitialWarrenCount = CInt(Console.ReadLine())
  36.                         Console.Write("Initial number of foxes: ")
  37.                         InitialFoxCount = CInt(Console.ReadLine())
  38.                         Console.Write("Randomness variability (percent): ")
  39.                         Variability = CInt(Console.ReadLine())
  40.                         FixedInitialLocations = False
  41.                     End If
  42.                     Dim Sim As New Simulation(LandscapeSize, InitialWarrenCount, InitialFoxCount, Variability, FixedInitialLocations)
  43.                 End If
  44.             Loop While MenuOption <> 3
  45.             Console.ReadKey()
  46.         End Sub
  47.  
  48.         Class Location
  49.             Public Fox As Fox
  50.             Public Warren As Warren
  51.  
  52.             Public Sub New()
  53.                 Fox = Nothing
  54.                 Warren = Nothing
  55.             End Sub
  56.         End Class
  57.  
  58.         Class Simulation
  59.             Private Landscape(,) As Location
  60.             Private TimePeriod As Integer = 0
  61.             Private WarrenCount As Integer = 0
  62.             Private FoxCount As Integer = 0
  63.             Private ShowDetail As Boolean = False
  64.             Private LandscapeSize As Integer
  65.             Private Variability As Integer
  66.             Private Shared Rnd As New Random()
  67.  
  68.             Public Sub New(ByVal LandscapeSize As Integer, ByVal InitialWarrenCount As Integer, ByVal InitialFoxCount As Integer, ByVal Variability As Integer, ByVal FixedInitialLocations As Boolean)
  69.                 Dim MenuOption As Integer
  70.                 Dim x As Integer
  71.                 Dim y As Integer
  72.                 Dim ViewRabbits As String
  73.                 Me.LandscapeSize = LandscapeSize
  74.                 Me.Variability = Variability
  75.                 Landscape = New Location(LandscapeSize, LandscapeSize) {}
  76.                 CreateLandscapeAndAnimals(InitialWarrenCount, InitialFoxCount, FixedInitialLocations)
  77.                 DrawLandscape()
  78.                 Do
  79.                     Console.WriteLine()
  80.                     Console.WriteLine("1. Advance to next time period showing detail")
  81.                     Console.WriteLine("2. Advance to next time period hiding detail")
  82.                     Console.WriteLine("3. Inspect fox")
  83.                     Console.WriteLine("4. Inspect warren")
  84.                     Console.WriteLine("5. Exit")
  85.                     Console.WriteLine()
  86.                     Console.Write("Select option: ")
  87.                     MenuOption = CInt(Console.ReadLine())
  88.                     If MenuOption = 1 Then
  89.                         TimePeriod += 1
  90.                         ShowDetail = True
  91.                         AdvanceTimePeriod()
  92.                     End If
  93.                     If MenuOption = 2 Then
  94.                         TimePeriod += 1
  95.                         ShowDetail = False
  96.                         AdvanceTimePeriod()
  97.                     End If
  98.                     If MenuOption = 3 Then
  99.                         x = InputCoordinate("x")
  100.                         y = InputCoordinate("y")
  101.                         If Not Landscape(x, y).Fox Is Nothing Then
  102.                             Landscape(x, y).Fox.Inspect()
  103.                         End If
  104.                     End If
  105.                     If MenuOption = 4 Then
  106.                         x = InputCoordinate("x")
  107.                         y = InputCoordinate("y")
  108.                         If Not Landscape(x, y).Warren Is Nothing Then
  109.                             Landscape(x, y).Warren.Inspect()
  110.                             Console.Write("View individual rabbits (y/n)?")
  111.                             ViewRabbits = Console.ReadLine()
  112.                             If ViewRabbits = "y" Then
  113.                                 Landscape(x, y).Warren.ListRabbits()
  114.                             End If
  115.                         End If
  116.                     End If
  117.                 Loop While (WarrenCount > 0 Or FoxCount > 0) And MenuOption <> 5
  118.                 Console.ReadKey()
  119.             End Sub
  120.  
  121.             Private Function InputCoordinate(ByVal CoordinateName As Char) As Integer
  122.                 Dim Coordinate As Integer
  123.                 Console.Write("  Input " & CoordinateName & " coordinate: ")
  124.                 Coordinate = CInt(Console.ReadLine())
  125.                 Return Coordinate
  126.             End Function
  127.  
  128.             Private Sub AdvanceTimePeriod()
  129.                 Dim NewFoxCount As Integer = 0
  130.                 If ShowDetail Then
  131.                     Console.WriteLine()
  132.                 End If
  133.                 For x = 0 To LandscapeSize - 1
  134.                     For y = 0 To LandscapeSize - 1
  135.                         If Not Landscape(x, y).Warren Is Nothing Then
  136.                             If ShowDetail Then
  137.                                 Console.WriteLine("Warren at (" & x & "," & y & "):")
  138.                                 Console.Write("  Period Start: ")
  139.                                 Landscape(x, y).Warren.Inspect()
  140.                             End If
  141.                             If FoxCount > 0 Then
  142.                                 FoxesEatRabbitsInWarren(x, y)
  143.                             End If
  144.                             If Landscape(x, y).Warren.NeedToCreateNewWarren() Then
  145.                                 CreateNewWarren()
  146.                             End If
  147.                             Landscape(x, y).Warren.AdvanceGeneration(ShowDetail)
  148.                             If ShowDetail Then
  149.                                 Console.Write("  Period End: ")
  150.                                 Landscape(x, y).Warren.Inspect()
  151.                                 Console.ReadKey()
  152.                             End If
  153.                             If Landscape(x, y).Warren.WarrenHasDiedOut() Then
  154.                                 Landscape(x, y).Warren = Nothing
  155.                                 WarrenCount -= 1
  156.                             End If
  157.                         End If
  158.                     Next
  159.                 Next
  160.                 For x = 0 To LandscapeSize - 1
  161.                     For y = 0 To LandscapeSize - 1
  162.                         If Not Landscape(x, y).Fox Is Nothing Then
  163.                             If ShowDetail Then
  164.                                 Console.WriteLine("Fox at (" & x & "," & y & "): ")
  165.                             End If
  166.                             Landscape(x, y).Fox.AdvanceGeneration(ShowDetail)
  167.                             If Landscape(x, y).Fox.CheckIfDead() Then
  168.                                 Landscape(x, y).Fox = Nothing
  169.                                 FoxCount -= 1
  170.                             Else
  171.                                 If Landscape(x, y).Fox.ReproduceThisPeriod() Then
  172.                                     If ShowDetail Then
  173.                                         Console.WriteLine("  Fox has reproduced. ")
  174.                                     End If
  175.                                     NewFoxCount += 1
  176.                                 End If
  177.                                 If ShowDetail Then
  178.                                     Landscape(x, y).Fox.Inspect()
  179.                                 End If
  180.                                 Landscape(x, y).Fox.ResetFoodConsumed()
  181.                             End If
  182.                         End If
  183.                     Next
  184.                 Next
  185.                 If NewFoxCount > 0 Then
  186.                     If ShowDetail Then
  187.                         Console.WriteLine("New foxes born: ")
  188.                         For f = 0 To NewFoxCount - 1
  189.                             CreateNewFox()
  190.                         Next
  191.                     End If
  192.                 End If
  193.                 If ShowDetail Then
  194.                     Console.ReadKey()
  195.                 End If
  196.                 DrawLandscape()
  197.                 Console.WriteLine()
  198.             End Sub
  199.  
  200.             Private Sub CreateLandscapeAndAnimals(ByVal InitialWarrenCount As Integer, ByVal InitialFoxCount As Integer, ByVal FixedInitialLocations As Boolean)
  201.                 For x = 0 To LandscapeSize - 1
  202.                     For y = 0 To LandscapeSize - 1
  203.                         Landscape(x, y) = New Location()
  204.                     Next
  205.                 Next
  206.                 If FixedInitialLocations Then
  207.                     Landscape(1, 1).Warren = New Warren(Variability, 38)
  208.                     Landscape(2, 8).Warren = New Warren(Variability, 80)
  209.                     Landscape(9, 7).Warren = New Warren(Variability, 20)
  210.                     Landscape(10, 3).Warren = New Warren(Variability, 52)
  211.                     Landscape(13, 4).Warren = New Warren(Variability, 67)
  212.                     WarrenCount = 5
  213.                     Landscape(2, 10).Fox = New Fox(Variability)
  214.                     Landscape(6, 1).Fox = New Fox(Variability)
  215.                     Landscape(8, 6).Fox = New Fox(Variability)
  216.                     Landscape(11, 13).Fox = New Fox(Variability)
  217.                     Landscape(12, 4).Fox = New Fox(Variability)
  218.                     FoxCount = 5
  219.                 Else
  220.                     For w = 0 To InitialWarrenCount - 1
  221.                         CreateNewWarren()
  222.                     Next
  223.                     For f = 0 To InitialFoxCount - 1
  224.                         CreateNewFox()
  225.                     Next
  226.                 End If
  227.             End Sub
  228.  
  229.             Private Sub CreateNewWarren()
  230.                 Dim x As Integer
  231.                 Dim y As Integer
  232.                 Do
  233.                     x = Rnd.Next(0, LandscapeSize)
  234.                     y = Rnd.Next(0, LandscapeSize)
  235.                 Loop While Not Landscape(x, y).Warren Is Nothing
  236.                 If ShowDetail Then
  237.                     Console.WriteLine("New Warren at (" & x & "," & y & ")")
  238.                 End If
  239.                 Landscape(x, y).Warren = New Warren(Variability)
  240.                 WarrenCount += 1
  241.             End Sub
  242.  
  243.             Private Sub CreateNewFox()
  244.                 Dim x As Integer
  245.                 Dim y As Integer
  246.                 Do
  247.                     x = Rnd.Next(0, LandscapeSize)
  248.                     y = Rnd.Next(0, LandscapeSize)
  249.                 Loop While Not Landscape(x, y).Fox Is Nothing
  250.                 If ShowDetail Then
  251.                     Console.WriteLine("  New Fox at (" & x & "," & y & ")")
  252.                 End If
  253.                 Landscape(x, y).Fox = New Fox(Variability)
  254.                 FoxCount += 1
  255.             End Sub
  256.  
  257.             Private Sub FoxesEatRabbitsInWarren(ByVal WarrenX As Integer, ByVal WarrenY As Integer)
  258.                 Dim FoodConsumed As Integer
  259.                 Dim PercentToEat As Integer
  260.                 Dim Dist As Double
  261.                 Dim RabbitsToEat As Integer
  262.                 Dim RabbitCountAtStartOfPeriod As Integer = Landscape(WarrenX, WarrenY).Warren.GetRabbitCount()
  263.                 For FoxX = 0 To LandscapeSize - 1
  264.                     For FoxY = 0 To LandscapeSize - 1
  265.                         If Not Landscape(FoxX, FoxY).Fox Is Nothing Then
  266.                             Dist = DistanceBetween(FoxX, FoxY, WarrenX, WarrenY)
  267.                             If Dist <= 3.5 Then
  268.                                 PercentToEat = 20
  269.                             ElseIf Dist <= 7 Then
  270.                                 PercentToEat = 10
  271.                             Else
  272.                                 PercentToEat = 0
  273.                             End If
  274.                             RabbitsToEat = CInt(Math.Round(CDbl(PercentToEat * RabbitCountAtStartOfPeriod / 100)))
  275.                             FoodConsumed = Landscape(WarrenX, WarrenY).Warren.EatRabbits(RabbitsToEat)
  276.                             Landscape(FoxX, FoxY).Fox.GiveFood(FoodConsumed)
  277.                             If ShowDetail Then
  278.                                 Console.WriteLine("  " & FoodConsumed & " rabbits eaten by fox at (" & FoxX & "," & FoxY & ").")
  279.                             End If
  280.                         End If
  281.                     Next
  282.                 Next
  283.             End Sub
  284.  
  285.             Private Function DistanceBetween(ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer) As Double
  286.                 Return Math.Sqrt(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2))
  287.             End Function
  288.  
  289.             Private Sub DrawLandscape()
  290.                 Console.WriteLine()
  291.                 Console.WriteLine("TIME PERIOD: " & TimePeriod)
  292.                 Console.WriteLine()
  293.                 Console.Write("    ")
  294.                 For x = 0 To LandscapeSize - 1
  295.                     If x < 10 Then
  296.                         Console.Write(" ")
  297.                     End If
  298.                     Console.Write(x & " |")
  299.                 Next
  300.                 Console.WriteLine()
  301.                 For x = 0 To LandscapeSize * 4 + 3
  302.                     Console.Write("-")
  303.                 Next
  304.                 Console.WriteLine()
  305.                 For y = 0 To LandscapeSize - 1
  306.                     If y < 10 Then
  307.                         Console.Write(" ")
  308.                     End If
  309.                     Console.Write(" " & y & "|")
  310.                     For x = 0 To LandscapeSize - 1
  311.                         If Not Landscape(x, y).Warren Is Nothing Then
  312.                             If Landscape(x, y).Warren.GetRabbitCount() < 10 Then
  313.                                 Console.Write(" ")
  314.                             End If
  315.                             Console.Write(Landscape(x, y).Warren.GetRabbitCount())
  316.                         Else
  317.                             Console.Write("  ")
  318.                         End If
  319.                         If Not Landscape(x, y).Fox Is Nothing Then
  320.                             Console.Write("F")
  321.                         Else
  322.                             Console.Write(" ")
  323.                         End If
  324.                         Console.Write("|")
  325.                     Next
  326.                     Console.WriteLine()
  327.                 Next
  328.             End Sub
  329.         End Class
  330.  
  331.         Class Warren
  332.             Private Const MaxRabbitsInWarren As Integer = 99
  333.             Private Rabbits() As Rabbit
  334.             Private RabbitCount As Integer = 0
  335.             Private PeriodsRun As Integer = 0
  336.             Private AlreadySpread As Boolean = False
  337.             Private Variability As Integer
  338.             Private Shared Rnd As New Random()
  339.  
  340.             Public Sub New(ByVal Variability As Integer)
  341.                 Me.Variability = Variability
  342.                 Rabbits = New Rabbit(MaxRabbitsInWarren) {}
  343.                 RabbitCount = CInt(CalculateRandomValue(CInt(MaxRabbitsInWarren / 4), Variability))
  344.                 For r = 0 To RabbitCount - 1
  345.                     Rabbits(r) = New Rabbit(Variability)
  346.                 Next
  347.             End Sub
  348.  
  349.             Public Sub New(ByVal Variability As Integer, ByVal RabbitCount As Integer)
  350.                 Me.Variability = Variability
  351.                 Me.RabbitCount = RabbitCount
  352.                 Rabbits = New Rabbit(MaxRabbitsInWarren) {}
  353.                 For r = 0 To RabbitCount - 1
  354.                     Rabbits(r) = New Rabbit(Variability)
  355.                 Next
  356.             End Sub
  357.  
  358.             Private Function CalculateRandomValue(ByVal BaseValue As Integer, ByVal Variability As Integer) As Double
  359.                 Return BaseValue - (BaseValue * Variability / 100) + (BaseValue * Rnd.Next(0, (Variability * 2) + 1) / 100)
  360.             End Function
  361.  
  362.             Public Function GetRabbitCount() As Integer
  363.                 Return RabbitCount
  364.             End Function
  365.  
  366.             Public Function NeedToCreateNewWarren() As Boolean
  367.                 If RabbitCount = MaxRabbitsInWarren And Not AlreadySpread Then
  368.                     AlreadySpread = True
  369.                     Return True
  370.                 Else
  371.                     Return False
  372.                 End If
  373.             End Function
  374.  
  375.             Public Function WarrenHasDiedOut() As Boolean
  376.                 If RabbitCount = 0 Then
  377.                     Return True
  378.                 Else
  379.                     Return False
  380.                 End If
  381.             End Function
  382.  
  383.             Public Sub AdvanceGeneration(ByVal ShowDetail As Boolean)
  384.                 PeriodsRun += 1
  385.                 If RabbitCount > 0 Then
  386.                     KillByOtherFactors(ShowDetail)
  387.                 End If
  388.             'Add this line so that the simulation will show the rabbits killed by the disease.......................................
  389.             If RabbitCount > 0 Then
  390.                 KillByDisease(ShowDetail)
  391.             End If
  392.  
  393.             If RabbitCount > 0 Then
  394.                 AgeRabbits(ShowDetail)
  395.             End If
  396.             If RabbitCount > 0 And RabbitCount <= MaxRabbitsInWarren Then
  397.                     If ContainsMales() Then
  398.                         MateRabbits(ShowDetail)
  399.                     End If
  400.                 End If
  401.                 If RabbitCount = 0 And ShowDetail Then
  402.                     Console.WriteLine("  All rabbits in warren are dead")
  403.                 End If
  404.             End Sub
  405.  
  406.             Public Function EatRabbits(ByVal RabbitsToEat As Integer) As Integer
  407.                 Dim DeathCount As Integer = 0
  408.                 Dim RabbitNumber As Integer
  409.                 If RabbitsToEat > RabbitCount Then
  410.                     RabbitsToEat = RabbitCount
  411.                 End If
  412.                 While DeathCount < RabbitsToEat
  413.                     RabbitNumber = Rnd.Next(0, RabbitCount)
  414.                     If Not Rabbits(RabbitNumber) Is Nothing Then
  415.                         Rabbits(RabbitNumber) = Nothing
  416.                         DeathCount += 1
  417.                     End If
  418.                 End While
  419.                 CompressRabbitList(DeathCount)
  420.                 Return RabbitsToEat
  421.             End Function
  422.  
  423.             Private Sub KillByOtherFactors(ByVal ShowDetail As Boolean)
  424.                 Dim DeathCount As Integer = 0
  425.                 For r = 0 To RabbitCount - 1
  426.                     If Rabbits(r).CheckIfKilledByOtherFactor() Then
  427.                         Rabbits(r) = Nothing
  428.                         DeathCount += 1
  429.                     End If
  430.                 Next
  431.                 CompressRabbitList(DeathCount)
  432.                 If ShowDetail Then
  433.                     Console.WriteLine("  " & DeathCount & " rabbits killed by other factors.")
  434.                 End If
  435.             End Sub
  436.         Private Sub KillByDisease(ByVal showdetail As Boolean)  'Improvements here..........................
  437.             Dim DeathCount As Integer = 0
  438.             For r = 0 To RabbitCount - 1
  439.                 If Rabbits(r).CheckIfKilledByDisease() Then
  440.                     Rabbits(r) = Nothing
  441.                     DeathCount += 1
  442.                 End If
  443.             Next
  444.             CompressRabbitList(DeathCount)
  445.             If showdetail Then
  446.                 Console.WriteLine("  " & DeathCount & " rabbits killed by disease.")
  447.             End If
  448.         End Sub
  449.         Private Sub AgeRabbits(ByVal ShowDetail As Boolean)
  450.                 Dim DeathCount As Integer = 0
  451.                 For r = 0 To RabbitCount - 1
  452.                     Rabbits(r).CalculateNewAge()
  453.                     If Rabbits(r).CheckIfDead() Then
  454.                         Rabbits(r) = Nothing
  455.                         DeathCount += 1
  456.                     End If
  457.                 Next
  458.                 CompressRabbitList(DeathCount)
  459.                 If ShowDetail Then
  460.                     Console.WriteLine("  " & DeathCount & " rabbits die of old age.")
  461.                 End If
  462.             End Sub
  463.  
  464.             Private Sub MateRabbits(ByVal ShowDetail As Boolean)
  465.                 Dim Mate As Integer = 0
  466.                 Dim Babies As Integer = 0
  467.                 Dim CombinedReproductionRate As Double
  468.                 For r = 0 To RabbitCount - 1
  469.                     If Rabbits(r).IsFemale() And RabbitCount + Babies < MaxRabbitsInWarren Then
  470.                         Do
  471.                             Mate = Rnd.Next(0, RabbitCount)
  472.                         Loop While Mate = r Or Rabbits(Mate).IsFemale()
  473.                         CombinedReproductionRate = (Rabbits(r).GetReproductionRate() + Rabbits(Mate).GetReproductionRate()) / 2
  474.                         If CombinedReproductionRate >= 1 Then
  475.                             Rabbits(RabbitCount + Babies) = New Rabbit(Variability, CombinedReproductionRate)
  476.                             Babies += 1
  477.                         End If
  478.                     End If
  479.                 Next
  480.                 RabbitCount = RabbitCount + Babies
  481.                 If ShowDetail Then
  482.                     Console.WriteLine("  " & Babies & " baby rabbits born.")
  483.                 End If
  484.             End Sub
  485.  
  486.             Private Sub CompressRabbitList(ByVal DeathCount As Integer)
  487.                 If DeathCount > 0 Then
  488.                     Dim ShiftTo As Integer = 0
  489.                     Dim ShiftFrom As Integer = 0
  490.                     While ShiftTo < RabbitCount - DeathCount
  491.                         While Rabbits(ShiftFrom) Is Nothing
  492.                             ShiftFrom += 1
  493.                         End While
  494.                         If ShiftTo <> ShiftFrom Then
  495.                             Rabbits(ShiftTo) = Rabbits(ShiftFrom)
  496.                         End If
  497.                         ShiftTo += 1
  498.                         ShiftFrom += 1
  499.                     End While
  500.                     RabbitCount = RabbitCount - DeathCount
  501.                 End If
  502.             End Sub
  503.  
  504.             Private Function ContainsMales() As Boolean
  505.                 Dim Males As Boolean = False
  506.                 For r = 0 To RabbitCount - 1
  507.                     If Not Rabbits(r).IsFemale() Then
  508.                         Males = True
  509.                     End If
  510.                 Next
  511.                 Return Males
  512.             End Function
  513.  
  514.             Public Sub Inspect()
  515.                 Console.WriteLine("Periods Run " & PeriodsRun & " Size " & RabbitCount)
  516.             End Sub
  517.  
  518.             Public Sub ListRabbits()
  519.                 If RabbitCount > 0 Then
  520.                     For r = 0 To RabbitCount - 1
  521.                         Rabbits(r).Inspect()
  522.                     Next
  523.                 End If
  524.             End Sub
  525.         End Class
  526.  
  527.         Class Animal
  528.             Protected NaturalLifespan As Double
  529.             Protected ID As Integer
  530.             Protected Shared NextID As Integer = 1
  531.             Protected Age As Integer = 0
  532.             Protected ProbabilityOfDeathOtherCauses As Double
  533.         Protected ProbabilityOfDeathDisease As Double
  534.         Protected IsAlive As Boolean
  535.         Protected Shared Rnd As New Random()
  536.  
  537.         Public Sub New(ByVal AvgLifespan As Integer, ByVal AvgProbabilityOfDeathOtherCauses As Double, ByVal Variability As Integer)
  538.             NaturalLifespan = AvgLifespan * CalculateRandomValue(100, Variability) / 100
  539.             ProbabilityOfDeathOtherCauses = AvgProbabilityOfDeathOtherCauses * CalculateRandomValue(100, Variability) / 100
  540.  
  541.             IsAlive = True
  542.             ID = NextID
  543.             NextID += 1
  544.         End Sub
  545.  
  546.         Public Overridable Sub CalculateNewAge()
  547.                 Age += 1
  548.                 If Age >= NaturalLifespan Then
  549.                     IsAlive = False
  550.                 End If
  551.             End Sub
  552.  
  553.             Public Overridable Function CheckIfDead() As Boolean
  554.                 Return Not IsAlive
  555.             End Function
  556.  
  557.             Public Overridable Sub Inspect()
  558.                 Console.Write("  ID " & ID & " ")
  559.                 Console.Write("Age " & Age & " ")
  560.                 Console.Write("LS " & NaturalLifespan & " ")
  561.                 Console.Write("Pr dth " & Math.Round(ProbabilityOfDeathOtherCauses, 2) & " ")
  562.             End Sub
  563.  
  564.             Public Overridable Function CheckIfKilledByOtherFactor() As Boolean
  565.                 If Rnd.Next(0, 100) < ProbabilityOfDeathOtherCauses * 100 Then
  566.                     IsAlive = False
  567.                     Return True
  568.                 Else
  569.                     Return False
  570.                 End If
  571.             End Function
  572.         'Add this function to make the rabbits die from disease...............................
  573.         Public Overridable Function CheckIfKilledByDisease() As Boolean
  574.             'Don't add a new ProbabilityOfDeathDisease becuase I tried it and it didn't work.
  575.             'Even if you keep the same variable for other factors it will give you different numbers
  576.             If Rnd.Next(0, 100) < ProbabilityOfDeathOtherCauses * 100 Then
  577.                 IsAlive = False
  578.                 Return True
  579.             Else
  580.                 Return False
  581.             End If
  582.         End Function
  583.         Protected Overridable Function CalculateRandomValue(ByVal BaseValue As Integer, ByVal Variability As Integer) As Double
  584.                 Return BaseValue - (BaseValue * Variability / 100) + (BaseValue * Rnd.Next(0, (Variability * 2) + 1) / 100)
  585.             End Function
  586.         End Class
  587.  
  588.         Class Fox
  589.             Inherits Animal
  590.             Private FoodUnitsNeeded As Integer = 10
  591.             Private FoodUnitsConsumedThisPeriod As Integer = 0
  592.             Private Const DefaultLifespan As Integer = 7
  593.             Private Const DefaultProbabilityDeathOtherCauses As Double = 0.1
  594.         Private Const DefaultProbabilityDeathDisease As Double = 0.05
  595.         Public Sub New(ByVal Variability As Integer)
  596.             MyBase.New(DefaultLifespan, DefaultProbabilityDeathOtherCauses, Variability)
  597.             FoodUnitsNeeded = CInt(10 * MyBase.CalculateRandomValue(100, Variability) / 100)
  598.             End Sub
  599.  
  600.             Public Sub AdvanceGeneration(ByVal ShowDetail As Boolean)
  601.                 If FoodUnitsConsumedThisPeriod = 0 Then
  602.                     IsAlive = False
  603.                     If ShowDetail Then
  604.                         Console.WriteLine("  Fox dies as has eaten no food this period.")
  605.                     End If
  606.                 Else
  607.                     If CheckIfKilledByOtherFactor() Then
  608.                         IsAlive = False
  609.                         If ShowDetail Then
  610.                             Console.WriteLine("  Fox killed by other factor.")
  611.                         End If
  612.                     Else
  613.                         If FoodUnitsConsumedThisPeriod < FoodUnitsNeeded Then
  614.                             CalculateNewAge()
  615.                             If ShowDetail Then
  616.                                 Console.WriteLine("  Fox ages further due to lack of food.")
  617.                             End If
  618.                         End If
  619.                         CalculateNewAge()
  620.                         If Not IsAlive Then
  621.                             If ShowDetail Then
  622.                                 Console.WriteLine("  Fox has died of old age.")
  623.                             End If
  624.                         End If
  625.                     End If
  626.                 End If
  627.             End Sub
  628.  
  629.             Public Sub ResetFoodConsumed()
  630.                 FoodUnitsConsumedThisPeriod = 0
  631.             End Sub
  632.  
  633.             Public Function ReproduceThisPeriod() As Boolean
  634.                 Const ReproductionProbability As Double = 0.25
  635.                 If Rnd.Next(0, 100) < ReproductionProbability * 100 Then
  636.                     Return True
  637.                 Else
  638.                     Return False
  639.                 End If
  640.             End Function
  641.  
  642.             Public Sub GiveFood(ByVal FoodUnits As Integer)
  643.                 FoodUnitsConsumedThisPeriod = FoodUnitsConsumedThisPeriod + FoodUnits
  644.             End Sub
  645.  
  646.             Public Overrides Sub Inspect()
  647.                 MyBase.Inspect()
  648.                 Console.Write("Food needed " & FoodUnitsNeeded & " ")
  649.                 Console.Write("Food eaten " & FoodUnitsConsumedThisPeriod & " ")
  650.                 Console.WriteLine()
  651.             End Sub
  652.         End Class
  653.  
  654.         Class Rabbit
  655.             Inherits Animal
  656.             Enum Genders
  657.                 Male
  658.                 Female
  659.             End Enum
  660.             Private ReproductionRate As Double
  661.             Private Const DefaultReproductionRate As Double = 1.2
  662.             Private Const DefaultLifespan As Integer = 4
  663.         Private Const DefaultProbabilityDeathOtherCauses As Double = 0.05
  664.         Private Const DefaultProbabilityDeathDisease As Double = 0.05
  665.         Private Gender As Genders
  666.  
  667.             Public Sub New(ByVal Variability As Integer)
  668.             MyBase.New(DefaultLifespan, DefaultProbabilityDeathOtherCauses, Variability)
  669.             ReproductionRate = DefaultReproductionRate * MyBase.CalculateRandomValue(100, Variability) / 100
  670.                 If Rnd.Next(0, 100) < 50 Then
  671.                     Gender = Genders.Male
  672.                 Else
  673.                     Gender = Genders.Female
  674.                 End If
  675.             End Sub
  676.  
  677.             Public Sub New(ByVal Variability As Integer, ByVal ParentsReproductionRate As Double)
  678.             MyBase.New(DefaultLifespan, DefaultProbabilityDeathOtherCauses, Variability)
  679.             ReproductionRate = ParentsReproductionRate * MyBase.CalculateRandomValue(100, Variability) / 100
  680.                 If Rnd.Next(0, 100) < 50 Then
  681.                     Gender = Genders.Male
  682.                 Else
  683.                     Gender = Genders.Female
  684.                 End If
  685.             End Sub
  686.  
  687.             Public Overrides Sub Inspect()
  688.                 MyBase.Inspect()
  689.                 Console.Write("Rep rate " & Math.Round(ReproductionRate, 1) & " ")
  690.                 If Gender = Genders.Female Then
  691.                     Console.WriteLine("Gender Female")
  692.                 Else
  693.                     Console.WriteLine("Gender Male")
  694.                 End If
  695.             End Sub
  696.  
  697.             Public Function IsFemale() As Boolean
  698.                 If Gender = Genders.Female Then
  699.                     Return True
  700.                 Else
  701.                     Return False
  702.                 End If
  703.             End Function
  704.  
  705.             Public Function GetReproductionRate() As Double
  706.                 Return ReproductionRate
  707.             End Function
  708.         End Class
  709.     End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement