Advertisement
marwanpro

jeu de la vie mk1

Nov 28th, 2016
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.89 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <ctime>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <Grapic.h>
  6. #define CHMAX 255
  7.  
  8.  
  9. using namespace std;
  10. using namespace grapic;
  11.  
  12.  
  13. // Global Vars (EDITABLE)
  14. const int DIMX = 10;
  15. const int DIMY = 10;
  16. int ALIVE = 25;
  17. const bool DEBUG = true;
  18.  
  19.  
  20. // Tools Vars
  21. int glX = 0;
  22. int glY = 0;
  23. int randInt(int min, int max) { return min + (rand() % (int)(max - min + 1)); }
  24. clock_t startTime = clock();
  25.  
  26.  
  27. // Grapic Vars
  28. const char title[CHMAX] = "Jeu de la Vie";
  29. const int SIZE = 32;
  30. const int WINX = 20 * 2 + (DIMX - 2) * SIZE + DIMX;
  31. const int WINY = 80 + (DIMY - 2) * SIZE + DIMY;
  32. bool stop = false;
  33.  
  34.  
  35. struct mCase
  36. {
  37.     int x;
  38.     int y;
  39.     int nearAlive = 0;
  40.     bool isAlive = false;
  41.     bool isVisible = true;
  42.     bool isDisplayed = false;
  43.  
  44.  
  45.     void init()
  46.     {
  47.         x = glX;
  48.         y = glY;
  49.         nearAlive = 0;
  50.         if (x == 0 || y == 0 || x == DIMX || y == DIMY) isVisible = false;
  51.         isAlive = false;
  52.     }
  53. };
  54.  
  55.  
  56. void checkNear(mCase table[DIMX][DIMY], int glX, int glY)
  57. {
  58.     for (int x = glX - 1; x <= glX + 1; x++)
  59.     {
  60.         for (int y = glY - 1; y <= glY + 1; y++)
  61.         {
  62.             if (table[x][y].isAlive == true)
  63.             {
  64.                 table[glX][glY].isAlive++;
  65.             }
  66.  
  67.         }
  68.     }
  69. }
  70.  
  71.  
  72. // Init a double array of mCase
  73. void init(mCase table[DIMX][DIMY])
  74. {
  75.     // Just calling init
  76.     for (glX = 0; glX < DIMX; glX++)
  77.     {
  78.         for (glY = 0; glY < DIMY; glY++)
  79.         {
  80.             table[glX][glY].init();
  81.         }
  82.     }
  83.  
  84.     // Placing cells
  85.     for (int i = 0; i < ALIVE; i++)
  86.     {
  87.         int x = randInt(1, DIMX - 2);
  88.         int y = randInt(1, DIMY - 2);
  89.         if (!table[x][y].isAlive && table[x][y].isVisible) table[x][y].isAlive = true;
  90.         else i--;
  91.     }
  92.  
  93.     // Count near bombs
  94.     for (int x = 1; x < DIMX - 1; x++)
  95.     {
  96.         for (int y = 1; y < DIMY - 1; y++)
  97.         {
  98.             checkNear(table, x, y);
  99.         }
  100.     }
  101. }
  102.  
  103.  
  104. // Print in Console
  105. void mPrint(mCase table[DIMX][DIMY])
  106. {
  107.     std::cout << "To disable cheat, set DEBUG to false in Source Code" << std::endl << std::endl;
  108.     for (glX = 1; glX < DIMX - 1; glX++)
  109.     {
  110.         for (glY = 1; glY < DIMY - 1; glY++)
  111.         {
  112.             if (table[glX][glY].isAlive) std::cout << '*';
  113.             else std::cout << table[glX][glY].isAlive;
  114.             std::cout << " ";
  115.         }
  116.         std::cout << std::endl;
  117.     }
  118. }
  119.  
  120. // Grapic Init Void
  121. void grapicInit()
  122. {
  123.     backgroundColor(127, 127, 127);
  124. }
  125.  
  126.  
  127. // Draw black grid
  128. void gridDrawer()
  129. {
  130.     for (int vl = 20; vl < WINX - 20; vl += SIZE + 1)
  131.     {
  132.         color(0, 0, 0);
  133.         line(vl, 20, vl, WINY - 62);
  134.     }
  135.  
  136.     for (int hl = 20; hl < WINY - 60; hl += SIZE + 1)
  137.     {
  138.         color(0, 0, 0);
  139.         line(20, hl, WINX - 22, hl);
  140.     }
  141.  
  142.     // Coord Tester
  143.     if (DEBUG)
  144.     {
  145.         int x, y;
  146.         mousePos(x, y);
  147.         color(0);
  148.         if ((x - 20) % (SIZE + 1) == 0 || (y - 20) % (SIZE + 1) == 0) color(255);
  149.         if (x < 20 || x > WINX - 20 || y < 20 || y > WINY - 60)  color(255);
  150.         print(WINX - 35, WINY - 24, x);
  151.         print(WINX - 35, WINY - 40, y);
  152.     }
  153. }
  154.  
  155. // Draw HUD at top
  156. int mRemaining(mCase table[DIMX][DIMY])
  157. {
  158.     int remaining = 0;
  159.     for (int x = 1; x < DIMX - 1; x++)
  160.     {
  161.         for (int y = 1; y < DIMY - 1; y++)
  162.         {
  163.             if (table[x][y].isAlive) remaining++;
  164.         }
  165.     }
  166.     return remaining;
  167. }
  168.  
  169. // Draw HUD at top
  170. void hud(mCase table[DIMX][DIMY])
  171. {
  172.     int remaining = mRemaining(table);
  173.     double elapsedTime = double(clock() - startTime) / CLOCKS_PER_SEC;
  174.  
  175.     color(255, 255, 255);
  176.     print(20, WINY - 32, "Time:");
  177.     print(80, WINY - 32, int(elapsedTime));
  178.     print(20, WINY - 55, "Cells Alive:");
  179.     print(180, WINY - 55, remaining);
  180. }
  181.  
  182.  
  183. // Draw Void
  184. void draw(mCase table[DIMX][DIMY])
  185. {
  186.     gridDrawer();
  187.     for (glX = 1; glX < DIMX - 1; glX++)
  188.     {
  189.         for (glY = 1; glY < DIMY - 1; glY++)
  190.         {
  191.             //discover(table, glX, glY);
  192.         }
  193.     }
  194.     hud(table);
  195. }
  196.  
  197.  
  198. int main(int, char**)
  199. {
  200.     srand(time(NULL));
  201.     mCase table[DIMX][DIMY];
  202.     init(table);
  203.     mPrint(table);
  204.     winInit(title, WINX, WINY);
  205.     setKeyRepeatMode(true);
  206.     grapicInit();
  207.     while (!stop)
  208.     {
  209.         winClear();
  210.         draw(table);
  211.         stop = winDisplay();
  212.     }
  213.     winQuit();
  214.     system("pause");
  215.     return 0;
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement