Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Evolution type thing
#11
Sorry for double posting but can anyone tell me why my updated version makes an error?

Code:
'(C) copyright Jack Davies 2005
'Okay, that was unnessercery but it felt good.
'This is supposed to simulate evolution but will be a long project.
'I've never really commented code too extensively before but I've decided that
' it's time for a change so I'm going to over-comment.


'800x600 pixels
SCREEN 19

'for making random stuff, urm... random
RANDOMIZE TIMER

' This variable is used for REDIMing the cells array to the correct number of germs.
germs% = 1
' This is used every time a new germ is born to keep track of what generation it is from.
' See notes in the main loop.
generation% = 1

' Creates the 'germ' type.
TYPE germ
   ' "X" position on the screen (1 TO 800)
   locx AS INTEGER
   ' "Y" position on the screen (1 TO 600)
   locy AS INTEGER
   ' The maximum distance (in pixels) that the germ can travel per 'turn' for
   ' lack of a better word
   speed AS INTEGER
   ' 1 OR 2 (consider parralell to male OR female)
   gender AS INTEGER
   ' 1 TO 16 (number of possible colors) However this does not mean that this
   ' is the maximum number of combinations for germs as this combines with generation
   ' which will (usually) be stronger, faster and generally better than the last.
   breed AS INTEGER
   ' How much strength the germ has to kill other stuff so that he can eat it.
   strength AS INTEGER
   ' How old it is.
   age AS INTEGER
   ' How many parents it had. Important for deciding the method of reproduction.
   number_of_parents AS INTEGER
   ' What generation of its breed the germ belongs to.
   generation AS INTEGER
   ' How much food can be taken from that germ at that exact time.
   food_content AS INTEGER
   ' How much energy a germ has affects how much of its speed and strength it
   ' can use. The maximum is 100 for energy.
   energy AS INTEGER
   ' What its maximum food intake is. When energy is at 100 and the germ still
   ' eats then its fat goes up.
   max_food_intake AS INTEGER
   ' Whether or not the germ can move. If, for example, the germ is a 'plant germ'
   ' then it would not be able to move. Also, it may be extremely unlucky in its
   ' evolutionary mutations.
   capable_of_movement AS INTEGER
   ' Whether or not it's alive. Used mainly for the person writing the code to tell
   ' the program which germs to keep active, ie, which germs can eat, reproduce, be
   ' eaten, and most of the time, move.
   alive AS INTEGER
   ' The fat variable has the opposite effect of the energy variable meaning that
   ' the less fat there is, the better for the germ, ie, the germ would not move
   ' as quickly if it had lots of fat. However, while a germ contains fat it will
   ' not lose energy but when the germ was going to lose energy it would lose fat.
   fat AS INTEGER
   ' Useful for stuff like parentage.
   id AS INTEGER
   ' The number of the entry in the world-array that shares its co-ordinates
   ' with the germ.
   location AS INTEGER
END TYPE

' Creates the 'earth' type, there is one of these per pixel.
TYPE earth
   ' "X" position on the screen (1 TO 800)
   locx AS INTEGER
   ' "Y" position on the screen (1 TO 600)
   locy AS INTEGER
   ' How much food that pixel stores, starts off as a random variable and then
   ' is affected as time goes by depending on how much it is fed from and how
   ' many plant germs are there.
   food AS INTEGER
   ' How many nutrients that pixel contains, affected by how many germs die within
   ' a three pixel radius of that pixel.
   nutrients AS INTEGER
END TYPE

' Generate the world.
DIM world(1 TO 480000) AS earth

' These are the locx and locy things. See further on to get what I mean.
X% = 1
Y% = 1

' Z% is every pixel.
FOR Z% = 1 TO 480000
   ' The 'X' position of every single one.
   world(Z%).locx = X%
   ' The 'Y' position.
   world(Z%).locy = Y%
   ' How much food it contains.
   world(Z%).food = INT(RND * 10) + 1
   ' Starts with zero and goes up for every germ that dies on that pixel.
   world(Z%).nutrients = 0
   ' Increase the Y position by one every time.
   Y% = Y% + 1
   ' Moves along to the next column every time the previous one is filled.
   IF Y% = 600 THEN
      X% = X% + 1
      Y% = 1
   END IF
NEXT Z%

' DIM's the cells array, the cells array stores every single germ ever spawned,
' dead or alive.
DIM cells(1 TO germs%) AS germ

' Create the first germ.

' Sets the 'locx' variable of the first germ as a random number between 1 and 800
cells(1).locx = INT(RND * 800) + 1
' Sets the 'locy' variable of the first germ as a random number between 1 and 600
cells(1).locy = INT(RND * 600) + 1
' The first germ is a static, useless germ with only one perpose, to split and
' generate mutations every time it splits.
cells(1).speed = 0
' Set's the gender of the first germ.
cells(1).gender = INT(RND * 2) + 1
' Sets the breed of the first germ.
cells(1).breed = INT(RND * 16) + 1
' Sets the strength of the first germ.
cells(1).strength = 1
' The first instruction in the main loop is to up every living germs age by one,
' so, if I gave it an age of 1 to start with he'd be 2 years old the first time through.
cells(1).age = 0
' This has to equal one or more because otherwise he would not split in 2 and
' create little baby germs.
cells(1).number_of_parents = 1
' He's the first generation.
cells(1).generation = 1
' Why not? Threes a good number
cells(1).food_content = 3
' He's done nothing, so his energy is full.
cells(1).energy = 100
' He's the first generation, let's let him evolve to get better.
cells(1).max_food_intake = 1
' He can't move
cells(1).capable_of_movement = 0
' He's alive.
cells(1).alive = 1
' He's eaten nothing so why should he have any fat?
cells(1).fat = 0
' He's the first so his ID is 1. the seconds will be 2 etc, etc...
cells(1).id = 1
' Finds the first germs location.
FOR X% = 1 TO 480000 ' 480000 is the number of pixels on the screen.
   IF world(X%).locx = cells(Z%).locx AND world(X%).locy = cells(Z%).locy THEN
      cells(Z%).location = X%
      EXIT FOR
   END IF
NEXT X%

'main loop
DO
   ' Moves the generation up every time. This works as there are always germs that
   ' divide in two to reproduce, :. a new generation is brought around every loop.
   ' By the time that all the asexual germs have been killed off there will
   ' have had to be enough sex-having germs to have killed them off who will be
   ' producing at least one kid per loop. I know that isn't a very programmery,
   ' think of every possibility, way to look at it but heck, it's the way I'm gonna
   ' do it. For now.
   generation% = generation% + 1
   FOR z% = 1 TO germs%
      ' Make sure that the germ you're dealing with is alive so you don't get
      ' "Primordial Soup Of The Living Dead".
      IF cells(Z%).alive = 1 THEN
         ' The germ will only have changed position if it can move, so, if it can't
         ' move then we'll set its position at birth.
         IF cells(Z%).capable_of_movement = 1 THEN
            ' See information on the first cell for the next seven lines.
            FOR X% = 1 TO 480000
               IF world(X%).locx = cells(Z%).locx AND world(X%).locy = cells(Z%).locy THEN
                  cells(Z%).location = X%
                  EXIT FOR
               END IF
            NEXT X%
         END IF
         ' IF the current germ's position contains food, THEN
         IF world(cells(Z%).location).food > 0 THEN
            ' IF there is more food than the maximum the germ can eat, THEN
            IF world(cells(Z%).location).food > cells(Z%).max_food_intake THEN
               ' Eat it.
               world(cells(Z%).location).food = world(cells(Z%).location).food - cells(Z%).max_food_intake
               cells(Z%).energy = cells(Z%).energy + cells(Z%).max_food_intake
            ' Otherwise, if there isn't enough, THEN
            ELSEIF world(cells(Z%).location).food < cells(Z%).max_food_intake THEN
               ' Eat that pixel dry.
               cells(Z%).energy = cells(Z%).energy + world(cells(Z%).location).food
               world(cells(Z%).location).food = 0
            END IF
         END IF
         ' Up every living germs age by one.
         cells(Z%).age = cells(Z%).age + 1
         ' Show the germ on the screen
         PSET(cells(Z%).locx, cells(Z%).locy), cells(Z%).breed
      END IF
   NEXT Z%
   ' Wait one second.
   SLEEP 1000
   ' Clear the screen so that the germs can move next time round.
   CLS
LOOP

Thanks in advance.

Jack


EDIT:

No suggestions then? anyone?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)