Advertisement
Guest User

SP_Fresh

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