home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / CTECHAPP.ZIP / SIMULATN.ZIP / ECOSYS.CPP < prev    next >
C/C++ Source or Header  |  1990-02-15  |  3KB  |  173 lines

  1. //  Program:    SimEco   (Simulated Ecosystem)
  2. //  Version:    2.00
  3. //
  4. //  Language:   C++ 2.0
  5. //  Environ:    MS-DOS, Zortech C++ v2.0x
  6. //
  7. //  Purpose:    This program simulates a primitive ecosystem.
  8. //
  9. //  Written by: Scott Robert Ladd
  10.  
  11. // include standard C libraries
  12. extern "C"
  13.     {
  14.     #include "conio.h"
  15.     #include "time.h"
  16.     #include "stdio.h"
  17.     #include "stdlib.h"
  18.     #include "graphvga.h"
  19.     }
  20.  
  21. // this macro returns a random number between 1 and n
  22. #define RandVal(n) ((rand() % n) + 1)
  23.  
  24. // include C++ classes
  25. #include "Creature.hpp"
  26. #include "Grazer.hpp"
  27. #include "BitGrid.hpp"
  28.  
  29. // These definitions change how food is distributed
  30. // #define GARDEN 1
  31. #define NORMAL_FOOD 1
  32. #define NEW_FOOD 2
  33.  
  34. // constants which define the size of the grid array
  35. const int MaxX = 630;
  36. const int MaxY = 450;
  37.  
  38. // global data items
  39. BitGrid * GrazerFood;
  40.  
  41. // function prototypes
  42. int  Initialize();
  43. void LifeCycle();
  44. void cdecl Finish();
  45. int  main();
  46.  
  47. int Initialize()
  48.     {
  49.     GraphInit();
  50.  
  51.     // set function to be called at exit
  52.     if (atexit(Finish))
  53.         return 1;
  54.  
  55.     srand(unsigned(time(NULL)));
  56.  
  57.     GrazerFood = new BitGrid (630, 450);
  58.  
  59.     Grazer::SetRegion(GrazerFood, MaxX, MaxY);
  60.  
  61.     Grazer * G;
  62.     int i, x, y;
  63.  
  64.     for (i = 0; i < 40; ++i)
  65.         {
  66.         x = RandVal(MaxX);
  67.         y = RandVal(MaxY);
  68.  
  69.         G = new Grazer(x,y);
  70.  
  71.         if (G == NULL)
  72.             {
  73.             printf("Error: Init Grazers\n");
  74.             return 2;
  75.             }
  76.         }
  77.  
  78.     for (i = 0; i < 4000; ++i)
  79.         {
  80.         do  {
  81.             x = RandVal(MaxX);
  82.             y = RandVal(MaxY);
  83.             }
  84.         while (GrazerFood->IsSet(x,y));
  85.  
  86.         GrazerFood->Include(x,y);
  87.  
  88.         PlotPixel(x,y,14);
  89.         }
  90.  
  91.     return 0;
  92.     }
  93.  
  94. void LifeCycle()
  95.     {
  96.     Creature * C;
  97.     unsigned long move;
  98.     char stat_line[20];
  99.     int x, y, i;
  100.  
  101.     move = 0L;
  102.  
  103.     while ((Grazer::Population() > 0) && !kbhit())
  104.         {
  105.         Creature::CList.GoToHead();
  106.  
  107.         while (1)
  108.             {
  109.             C = Creature::CList.Examine();
  110.  
  111.             if (NULL == C)
  112.                 break;
  113.  
  114.             Creature::CList.GoNext();
  115.  
  116.             if (C->Move())
  117.                 delete (Grazer *)C;
  118.             }
  119.  
  120.         for (i = 0; i < NEW_FOOD; ++i)
  121.             {
  122.             #if defined(GARDEN)
  123.                 do  {
  124.                     x = RandVal(100) + 270;
  125.                     y = RandVal(100) + 175;
  126.                     }
  127.                 while (GrazerFood->IsSet(x,y));
  128.  
  129.                 GrazerFood->Include(x,y);
  130.  
  131.                 PlotPixel(x,y,14);
  132.             #endif
  133.  
  134.             #if defined(NORMAL_FOOD)
  135.                 do  {
  136.                     x = RandVal(MaxX);
  137.                     y = RandVal(MaxY);
  138.                     }
  139.                 while (GrazerFood->IsSet(x,y));
  140.  
  141.                 GrazerFood->Include(x,y);
  142.  
  143.                 PlotPixel(x,y,14);
  144.             #endif
  145.             }
  146.  
  147.         ++move;
  148.  
  149.         sprintf(stat_line,"M: %6ld, C: %3d",move,Grazer::Population());
  150.  
  151.         PutGraphString(465,10,stat_line,15,0);
  152.         }
  153.     }
  154.  
  155. void cdecl Finish()
  156.     {
  157.     while (!kbhit()) ;
  158.  
  159.     if (!getch()) getch();
  160.  
  161.     GraphDone();
  162.     }
  163.  
  164. int main()
  165.     {
  166.     if (Initialize())
  167.         return 1;
  168.  
  169.     LifeCycle();
  170.  
  171.     return 0;
  172.     }
  173.