jhylands

The evaluation of the boxes

Jan 3rd, 2012
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.59 KB | None | 0 0
  1. Module Module1
  2.     Dim forms() As life
  3.     Dim RandomFeed As Int32 = 3
  4.     Public Structure cordinate
  5.         Dim x As Int16
  6.         Dim y As Int16
  7.     End Structure
  8.     Public Structure life
  9.         Dim energy As Int64
  10.         Dim cord As cordinate
  11.         Dim startenergy As Int16
  12.         Dim breadsize As Int16
  13.         Dim genetics As String
  14.     End Structure
  15.     Sub Main()
  16.         For i = 0 To Second(Now) + 5
  17.             Random(10)
  18.         Next
  19.         ReDim forms(0)
  20.         CreateAnimal(forms(0), ToLife(Random(100), Random(100), 0, 10000, 2000, "A")) 'animal , startsize = 01000, beedsize = 020000
  21.         Console.WriteLine(forms(0).energy)
  22.         Console.WriteLine(forms(0).cord.x & "," & forms(0).cord.y)
  23.         Console.ReadLine()
  24.         Do
  25.             ReDim Preserve forms(forms.GetLength(0) + 2)
  26.             CreatePlant(forms(forms.GetLength(0) - 3))
  27.             CreatePlant(forms(forms.GetLength(0) - 2))
  28.             CreatePlant(forms(forms.GetLength(0) - 1))
  29.             For i = 1 To 100
  30.                 MoveAnimals(forms)
  31.                 check()
  32.             Next
  33.             Console.WriteLine("Number of creatures: " & forms.GetLength(0))
  34.             Console.WriteLine("Energy: " & forms(0).energy)
  35.             Console.WriteLine("Mother energy: " & (forms(forms.GetLength(0) - 1).breadsize) - (forms(forms.GetLength(0) - 1).startenergy))
  36.             Console.ReadLine()
  37.         Loop
  38.  
  39.         Console.Read()
  40.     End Sub
  41.     Public Function Random(ByVal maxnum As Integer)
  42.         RandomFeed = RandomFeed ^ 2 Mod (97 * 99989)
  43.         Random = Int(1 + (RandomFeed / 10000000.0) * maxnum)
  44.     End Function
  45.     Sub CreatePlant(ByRef plant As life)
  46.         plant.cord.x = Random(100)
  47.         plant.cord.y = Random(100)
  48.         plant.energy = 1000
  49.         plant.genetics = "P05000" 'plant, energycontent 05000
  50.     End Sub
  51.     Function ToLife(ByVal x As Int16, ByVal y As Int16, ByVal energy As Int64, ByVal startsize As Int16, ByVal breadsize As Int16, ByVal genetics As String) As life
  52.         ToLife.cord.x = x
  53.         ToLife.cord.y = y
  54.         ToLife.energy = energy
  55.         ToLife.startenergy = startsize
  56.         ToLife.breadsize = breadsize
  57.         ToLife.genetics = genetics
  58.     End Function
  59.     Sub CreateAnimal(ByRef animal As life, ByVal parent As life)
  60.         animal.cord = parent.cord
  61.         animal.energy = parent.startenergy
  62.         animal.genetics = parent.genetics
  63.         animal.startenergy = parent.startenergy + Random(100) - 50
  64.         animal.breadsize = parent.breadsize + Random(100) - 50
  65.         If animal.breadsize <= animal.startenergy Then
  66.             animal.breadsize = animal.startenergy
  67.         End If
  68.     End Sub
  69.     Public Function MapAdd(ByVal Number1, ByVal number2)
  70.         MapAdd = Number1 + number2
  71.         If MapAdd - 100 > 0 Then
  72.             MapAdd = MapAdd - 100
  73.         ElseIf MapAdd + 100 < 100 Then
  74.             MapAdd = MapAdd + 100
  75.         End If
  76.     End Function
  77.     Sub MoveAnimals(ByRef lifelist() As life)
  78.         Dim list2() As life
  79.         Dim i As Int16 = 0
  80.         Dim thismax As Integer = lifelist.GetLength(0) - 1
  81.         Do
  82.             If i > thismax Then
  83.                 Exit Do
  84.             End If
  85.             If lifelist(i).genetics = "A" Then
  86.                 If lifelist(i).energy > 0 Then
  87.                     'move the animal
  88.                     MoveAnimal(lifelist(i))
  89.                 End If
  90.             End If
  91.             If forms(i).energy <= 0 Then
  92.                 Console.WriteLine("A death: " & i)
  93.                 Console.WriteLine("Type: " & lifelist(i).genetics)
  94.                 'the animal is dead
  95.                 'get a second array ready
  96.                 ReDim list2(lifelist.GetLength(0) - 2)
  97.                 'move the entries before
  98.                 For n = 0 To i - 1
  99.                     list2(n) = lifelist(n)
  100.                 Next
  101.                 'move the entires after
  102.                 For n = i + 1 To lifelist.GetLength(0) - 1
  103.                     list2(n - 1) = lifelist(n)
  104.                 Next
  105.                 'make the first array smaller
  106.                 ReDim lifelist(list2.GetLength(0) - 1)
  107.                 'transphure the entries back across
  108.                 lifelist = list2
  109.                 thismax = thismax - 1
  110.             End If
  111.             i = i + 1
  112.         Loop
  113.  
  114.     End Sub
  115.     Sub MoveAnimal(ByRef animal As life)
  116.         'defining the cariables that hold the movement direction
  117.         'and settig them to a random number from -1 to 1
  118.         Dim xMove As Int16 = Random(3) - 2
  119.         Dim yMove As Int16 = Random(3) - 2
  120.         animal.cord.x = MapAdd(animal.cord.x, xMove)
  121.         animal.cord.y = MapAdd(animal.cord.y, yMove)
  122.         animal.energy = animal.energy - 1
  123.         If animal.energy < 0 Then
  124.             'die
  125.         End If
  126.     End Sub
  127.     Sub EatPlant(ByRef Animal As life, ByRef Plant As life)
  128.         Animal.energy = Animal.energy + Plant.energy
  129.         Plant.energy = 0
  130.         Plant.cord.x = Random(100)
  131.         Plant.cord.y = Random(100)
  132.     End Sub
  133.     Sub check()
  134.         For n = 0 To forms.GetLength(0) - 1
  135.             'if this is an animal
  136.             If Mid(forms(n).genetics, 1, 1) = "A" Then
  137.                 'check for food--------------------------------------------------------------------------------------
  138.                 For i = 0 To forms.GetLength(0) - 1
  139.                     If i <> n Then
  140.                         'if the animal is close to the plant in the x
  141.                         If forms(n).cord.x < forms(i).cord.x + 10 And forms(n).cord.x > forms(i).cord.x - 10 Then
  142.                             'if the animal close to the plant in the y
  143.                             If forms(n).cord.y < forms(i).cord.y + 10 And forms(n).cord.y > forms(i).cord.y - 10 Then
  144.                                 'eat the plant
  145.                                 EatPlant(forms(n), forms(i))
  146.                                 Console.WriteLine("Animal:" & n & "," & forms(n).energy)
  147.                             End If
  148.                         End If
  149.                     End If
  150.                 Next
  151.                 'check weather it can bread------------------------------------------------------------------------------------------
  152.                 If forms(n).energy > forms(n).breadsize Then
  153.                     ReDim Preserve forms(forms.GetLength(0))
  154.                     CreateAnimal(forms(forms.GetLength(0) - 1), forms(n))
  155.                     forms(n).energy = forms(n).energy - forms(n).startenergy
  156.                     Console.WriteLine("A Birth: " & forms(forms.GetLength(0) - 1).breadsize)
  157.                 End If
  158.             End If
  159.  
  160.         Next
  161.     End Sub
  162. End Module
Advertisement
Add Comment
Please, Sign In to add comment