Artificial Life – Simple Math

The following code transforms the struggle for survival into a mathematical equation. A user is asked to enter a number of carnivores to create between 1 – 100. Once the number has been decided, the plants, herbivores and carnivores reproduce contingent on a specific ratio between plants:herbivores:carnivores or 16:6:2.

The program takes 3 separate life forms plants, herbivores and carnivores and increases or decreases their reproduction rate depedent on their ratio in comparison to the other life forms. A ratio of 2:1 was used for plants:animals and a ratio of 3:1 was used for herbivores:carnivores.

The program uses a while condition to verify the correct number was used for input and then uses a for counter to loop 100 times.

// artificial life psuedo code simple evolution math

import java.util.Scanner;

public class alpcsem

{
public static void main( String[] args )
{
Scanner input = new Scanner(System.in);

double p;
double h;
double c;

p = 20;
h = 8;

System.out.print(“Please enter the number of carnivores you would like to create(Between 1 – 100): “);
c = input.nextDouble();
while (c > 100 || c < 1)
{
System.out.print(“You must enter a number between 1 – 100 “);
System.out.print(“Please enter the number of carnivores you would like to create(Between 1 – 100): “);
c = input.nextDouble();
} // end while

System.out.printf(“Plants %.0f\n”, p);
System.out.printf(“Herbivores %.0f\n”, h);
System.out.printf(“Carinvores %.0f\n”, c);

for (int count = 1; count <= 100; count++) // counter
// while (p/(h+c) != 2 && h/c != 3 )
{
// ratio 2:1 for plants to animals = p/(h+c)
if (p/(h+c) > 2) // increase animal production
{
p = p + 16;
h = h + 12;
c = c + 4;
System.out.printf(“Plants %.0f\n”, p);
System.out.printf(“Herbivores %.0f\n”, h);
System.out.printf(“Carinvores %.0f\n”, c);
} // end while

if (p/(h+c) < 2) // reduce animal production
{
p = p + 16;
h = h + 3;
c = c + 1;
System.out.printf(“Plants %.0f\n”, p);
System.out.printf(“Herbivores %.0f\n”, h);
System.out.printf(“Carinvores %.0f\n”, c);
} // end while

if (h/c > 3) // increase carnivore production
{
p = p + 16;
h = h + 7;
c = c + 1;
} // end while

if (h/c < 3) // increase herbivore production
{
p = p + 16;
h = h + 5;
c = c + 3;
} // end if

} // end for

} // end method main
} // end class alpcsem

0
  Related Posts