Program ALifeSim

Main program of artificial life simulator

The Code

program ALifeSim;
{
    Copyright (c) 2014 Steven Binns

    Licensed under the Apache License, Version 2.0 (the "License"); you may not
    use this file except in compliance with the License, as described at
    http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/
}
uses
  Classes, SysUtils, Crt, globals_types_and_constants, ShellAPI, INIFiles, SDL;
var
  i : integer;
begin
  Randomize;
  new(screen);
  new(back);
  back^.x := 0;
  back^.y := 0;
  back^.w := 800;
  back^.h := 600;
  repeat
    clrscr;
    writeln('-- Root Menu --'#13#10);
    if HerbNeuralBase <> '' then
      writeln('Base Herbivore Neural Network: ' + HerbNeuralBase)
    else
      writeln('Base Herbivore Neural Network: <NIL>');
    if CarnNeuralBase <> '' then
      writeln('Base Carnivore Neural Network: ' + CarnNeuralBase)
    else
      writeln('Base Carnivore Neural Network: <NIL>');
    DisplayParameters;
    write(#13#10'Enter Command (networks n, parameters p, run, runnogui, end): ');
    readln(cmd);
    case cmd of
      'networks', 'n':
        repeat
          clrscr;
          writeln('-- Networks --'#13#10);
          if HerbNeuralBase <> '' then
            writeln('Base Herbivore Neural Network: ' + HerbNeuralBase)
          else
            writeln('Base Herbivore Neural Network: <NIL>');
          if CarnNeuralBase <> '' then
            writeln('Base Carnivore Neural Network: ' + CarnNeuralBase)
          else
            writeln('Base Carnivore Neural Network: <NIL>');
          write(#13#10'Enter Command (r to return to main menu, oh, oc, lh, lc):  ');
          readln(cmd);
          case cmd of
            'open herbivore folder', 'oh':
              begin
                ShellExecute(0, nil, 'explorer.exe', PChar(GetCurrentDir + '\Networks\Herbivores'), nil, 1);
              end;
            'open carnivore folder', 'oc':
              begin
                ShellExecute(0, nil, 'explorer.exe', PChar(GetCurrentDir + '\Networks\Carnivores'), nil, 1);
              end;
            'load carnivore', 'lc':
              begin
                write('Networks\Carnivores\');
                readln(cmd);
                if FileExists(GetCurrentDir + '\Networks\Carnivores\' + cmd + '.ini') then
                  begin
                    CarnNeuralBase := GetCurrentDir + '\Networks\Carnivores\' + cmd + '.ini';
                    savefile := TINIFile.Create(CarnNeuralBase);
                    for i := 0 to 11 do
                      CarnBaseWeights[0, i] := savefile.ReadInteger('Turn Left', IntToStr(i), 0);
                    for i := 0 to 11 do
                      CarnBaseWeights[1, i] := savefile.ReadInteger('Turn Right', IntToStr(i), 0);;
                    for i := 0 to 11 do
                      CarnBaseWeights[2, i] :=  savefile.ReadInteger('Move Forwards', IntToStr(i), 0);
                    for i := 0 to 11 do
                      CarnBaseWeights[3, i] := savefile.ReadInteger('Eat', IntToStr(i), 0);
                    savefile.Free;
                    writeln('File successfully loaded');
                  end
                else
                  begin
                    writeln('File at path ' + GetCurrentDir + '\Networks\Carnivores\' + cmd + '.ini not found');
                    CarnNeuralBase := '';
                  end;
                readln;
              end;
            'load herbivore', 'lh':
              begin
                write('Networks\Herbivores\');
                readln(cmd);
                if FileExists(GetCurrentDir + '\Networks\Herbivores\' + cmd + '.ini') then
                  begin
                    HerbNeuralBase :=
                      GetCurrentDir + '\Networks\Herbivores\' + cmd + '.ini';
                    savefile := TINIFile.Create(HerbNeuralBase);
                    for i := 0 to 11 do
                      HerbBaseWeights[0, i] := savefile.ReadInteger('Turn Left', IntToStr(i), 0);
                    for i := 0 to 11 do
                      HerbBaseWeights[1, i] := savefile.ReadInteger('Turn Right', IntToStr(i), 0);
                    for i := 0 to 11 do
                      HerbBaseWeights[2, i] := savefile.ReadInteger('Move Forwards', IntToStr(i), 0);
                    for i := 0 to 11 do
                      HerbBaseWeights[3, i] := savefile.ReadInteger('Eat', IntToStr(i), 0);
                    savefile.Free;
                    writeln('File successfully loaded');
                  end
                else
                  begin
                    writeln('File at path ' + GetCurrentDir +  '\Networks\Herbivores\' + cmd + '.ini not found');
                    HerbNeuralBase := '';
                  end;
              end;
          end;
        until (cmd = 'return') or (cmd = 'r');
      'parameters', 'p':
        repeat
          clrscr;
          DisplayParameters;
          write(#13#10'Enter Command (r to return to main menu): ');
          readln(cmd);
          clrscr;
          case cmd of
            'steps', 's':
              begin
                writeln('Steps: ', MAX_STEPS);
                write(#13#10'Enter Number of Steps: ');
                readln(MAX_STEPS);
              end;
            'fast steps', 'fs':
              begin
                writeln('Fast Steps: ', FAST_STEPS);
                write(#13#10'Enter Number of Fast Steps before Display: ');
                readln(MAX_STEPS);
              end;
            'delay', 'd':
              begin
               writeln('Delay in ms between frames: ', DELAY);
               write(#13#10'Enter the delay in ms: ');
               readln(DELAY);
              end;
            'plants', 'p':
              begin
                writeln('Max Plants: ', MAX_PLANTS);
                Write(#13#10'Enter Max Number of Plants: ');
                readln(MAX_PLANTS);
              end;
            'herbivores', 'h':
              begin
                writeln('Max Herbivores: ', MAX_HERBIVORES);
                write(#13#10'Enter Max Number of Herbivores: ');
                readln(MAX_HERBIVORES);
              end;
            'carnivores', 'c':
              begin
                writeln('Max Carnivores: ', MAX_CARNIVORES);
                write(#13#10'Enter Max Number of Carnivores: ');
                readln(MAX_CARNIVORES);
              end;
            'grid', 'g':
              begin
                writeln('Grid Size: ', MAX_GRID, 'x', MAX_GRID);
                write(#13#10'Enter Side Length of Grid: ');
                readln(cmd);
                if StrToInt(cmd) <= 1000 then
                  MAX_GRID := StrToInt(cmd)
                else
                  begin
                    writeln('Limit of 1000 x 1000 Grid');
                    readln;
                  end;
              end;
            'energy', 'e':
              begin
                writeln('Max Energy: ', MAX_ENERGY);
                Write(#13#10'Enter Max Energy: ');
                readln(MAX_ENERGY);
              end;
            'food energy', 'fe':
              begin
                writeln('Energy per Food Unit: ', FOOD_ENERGY);
                write(#13#10'Enter Food Energy: ');
                readln(FOOD_ENERGY);
              end;
            'reproduction energy', 're':
              begin
                writeln('Energy to Reproduce: ', REPRODUCTION_ENERGY);
                write(#10#10'Enter Reproduction Energy: ');
                readln(REPRODUCTION_ENERGY);
               end;
          end;
        until (cmd = 'return') or (cmd = 'r') ;
      'run':
          begin
            writeln('Simulating - Please wait about 10s per 100000 fast steps.');
            writeln('The display will appear soon.');
            SDL_INIT(SDL_INIT_VIDEO);
            SetUp;
            Simulate;
            sdl_quit;
            Save;
            Finish;
          end;
      'runnogui':
        begin
          writeln('Simulating - Please wait about 10s per 100000 steps.');
          SetUp;
          Simulate;
          Save;
          Finish;
        end;
    end;
  until cmd = 'end';
end.
Programming - a skill for life!

by Steven Binns: Y13 Age ~18