Advertisement
marwanpro

Untitled

Jan 3rd, 2017
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.08 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 = 20;
  15. const int DIMY = 20;
  16. int ALIVE = 75;
  17. const bool DEBUG = true;
  18.  
  19.  
  20. // Tools Vars
  21. int glX = 0;
  22. int glY = 0;
  23. int turn = 0;
  24. int randInt(int min, int max) { return min + (rand() % (int)(max - min + 1)); }
  25. clock_t startTime = clock();
  26.  
  27.  
  28. // Grapic Vars
  29. const char title[CHMAX] = "Jeu de la Vie";
  30. const int SIZE = 32;
  31. const int WINX = 20 * 2 + (DIMX - 2) * SIZE + DIMX;
  32. const int WINY = 80 + (DIMY - 2) * SIZE + DIMY;
  33. bool stop = false;
  34.  
  35.  
  36. struct mCase
  37. {
  38.     int x;
  39.     int y;
  40.     int nearAlive = 0;
  41.     bool isAlive = false;
  42.     bool isVisible = true;
  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.     table[glX][glY].nearAlive = 0;
  59.     for (int x = glX - 1; x <= glX + 1; x++)
  60.     {
  61.         for (int y = glY - 1; y <= glY + 1; y++)
  62.         {
  63.             if (table[x][y].isAlive == true)
  64.             {
  65.                 table[glX][glY].nearAlive++;
  66.             }
  67.  
  68.         }
  69.     }
  70.     if (table[glX][glY].isAlive) table[glX][glY].nearAlive--;
  71. }
  72.  
  73.  
  74. // Init a double array of mCase
  75. void init(mCase table[DIMX][DIMY])
  76. {
  77.     // Just calling init
  78.     for (glX = 0; glX < DIMX; glX++)
  79.     {
  80.         for (glY = 0; glY < DIMY; glY++)
  81.         {
  82.             table[glX][glY].init();
  83.         }
  84.     }
  85.  
  86.     // Placing cells
  87.     for (int i = 0; i < ALIVE; i++)
  88.     {
  89.         int x = randInt(1, DIMX - 2);
  90.         int y = randInt(1, DIMY - 2);
  91.         if (!table[x][y].isAlive && table[x][y].isVisible) table[x][y].isAlive = true;
  92.         else i--;
  93.     }
  94.  
  95.     // Count near bombs
  96.     for (int x = 1; x < DIMX - 1; x++)
  97.     {
  98.         for (int y = 1; y < DIMY - 1; y++)
  99.         {
  100.             checkNear(table, x, y);
  101.         }
  102.     }
  103. }
  104.  
  105.  
  106. // Print in Console
  107. void mPrint(mCase table[DIMX][DIMY])
  108. {
  109.     //system("cls");
  110.     std::cout << "To disable cheat, set DEBUG to false in Source Code" << std::endl << std::endl;
  111.     for (glX = 1; glX < DIMX - 1; glX++)
  112.     {
  113.         for (glY = 1; glY < DIMY - 1; glY++)
  114.         {
  115.             if (table[glX][glY].isAlive) std::cout << '*';
  116.             else std::cout << table[glX][glY].nearAlive;
  117.             std::cout << " ";
  118.         }
  119.         std::cout << std::endl;
  120.     }
  121.  
  122.  
  123. }
  124.  
  125. // Grapic Init Void
  126. void grapicInit()
  127. {
  128.     backgroundColor(127, 127, 127);
  129. }
  130.  
  131.  
  132. // Draw black grid
  133. void gridDrawer()
  134. {
  135.     for (int vl = 20; vl < WINX - 20; vl += SIZE + 1)
  136.     {
  137.         color(0, 0, 0);
  138.         line(vl, 20, vl, WINY - 62);
  139.     }
  140.  
  141.     for (int hl = 20; hl < WINY - 60; hl += SIZE + 1)
  142.     {
  143.         color(0, 0, 0);
  144.         line(20, hl, WINX - 22, hl);
  145.     }
  146.  
  147.     // Coord Tester
  148.     if (DEBUG)
  149.     {
  150.         int x, y;
  151.         mousePos(x, y);
  152.         color(0);
  153.         if ((x - 20) % (SIZE + 1) == 0 || (y - 20) % (SIZE + 1) == 0) color(255);
  154.         if (x < 20 || x > WINX - 20 || y < 20 || y > WINY - 60)  color(255);
  155.         print(WINX - 35, WINY - 24, x);
  156.         print(WINX - 35, WINY - 40, y);
  157.     }
  158. }
  159.  
  160. // Draw HUD at top
  161. int mRemaining(mCase table[DIMX][DIMY])
  162. {
  163.     int remaining = 0;
  164.     for (int x = 1; x < DIMX - 1; x++)
  165.     {
  166.         for (int y = 1; y < DIMY - 1; y++)
  167.         {
  168.             if (table[x][y].isAlive) remaining++;
  169.         }
  170.     }
  171.     return remaining;
  172. }
  173.  
  174.  
  175. // Draw HUD at top
  176. void hud(mCase table[DIMX][DIMY])
  177. {
  178.     int remaining = mRemaining(table);
  179.     double elapsedTime = double(clock() - startTime) / CLOCKS_PER_SEC;
  180.  
  181.     color(255, 255, 255);
  182.     print(20, WINY - 32, "Time:");
  183.     print(80, WINY - 32, int(elapsedTime));
  184.     print(20, WINY - 55, "Cells Alive:");
  185.     print(180, WINY - 55, remaining);
  186.     print(150, WINY - 32, "Step:");
  187.     print(230, WINY - 32, turn);
  188.  
  189.     color(0, 0, 0);
  190.     line(0, 20, 20, 20);
  191.     line(20, 0, 20, 20);
  192.     print(3, -4, "R");
  193. }
  194.  
  195.  
  196. // Display Cells
  197. void discover(mCase table[DIMX][DIMY], int x, int y)
  198. {
  199.     if (!table[x][y].isAlive) return;
  200.  
  201.     int wX = x + SIZE * x;
  202.     int wY = 30 + -y + SIZE * (DIMY - 2 - y)+8;
  203.  
  204.     color(250, 0, 0);
  205.     print(wX, wY, "O");
  206. }
  207.  
  208.  
  209. // Reset Void
  210. void reset(mCase table[DIMX][DIMY])
  211. {
  212.     system("cls");
  213.     init(table);
  214.     startTime = clock();
  215.     turn = 0;
  216. }
  217.  
  218.  
  219. // Finish Detector 3000
  220. bool isFinished(mCase table[DIMX][DIMY], mCase previous[2][DIMX][DIMY])
  221. {
  222.     if (table == previous[0] || table == previous[1] || table == previous[2]) return true;
  223.     else
  224.     {
  225.         cout << "P0";
  226.         mPrint(previous[0]);
  227.         cout << "P1";
  228.         mPrint(previous[1]);
  229.         cout << "P2";
  230.         mPrint(previous[2]);
  231.  
  232.         memcpy(previous[2], previous[1], DIMX*DIMY);
  233.         memcpy(previous[1], previous[0], DIMX*DIMY);
  234.         memcpy(previous[0], table, DIMX*DIMY);
  235.         return false;
  236.     }
  237. }
  238.  
  239.  
  240. // Draw Void
  241. void draw(mCase table[DIMX][DIMY])
  242. {
  243.     gridDrawer();
  244.     for (glX = 1; glX < DIMX - 1; glX++)
  245.     {
  246.         for (glY = 1; glY < DIMY - 1; glY++)
  247.         {
  248.             discover(table, glX, glY);
  249.         }
  250.     }
  251.     hud(table);
  252. }
  253.  
  254.  
  255. // Update Void
  256. void update(mCase table[DIMX][DIMY], mCase previous[2][DIMX][DIMY])
  257. {
  258.     delay(150);
  259.     system("cls");
  260.    
  261.     int x, y;
  262.     mousePos(x, y);
  263.     if (x < 20 && y < 20)
  264.     {
  265.         reset(table);
  266.         return;
  267.     }
  268.    
  269.     turn++;
  270.     for (int x = 1; x < DIMX - 1; x++)
  271.     {
  272.         for (int y = 1; y < DIMY - 1; y++)
  273.         {
  274.             checkNear(table, x, y);
  275.         }
  276.     }
  277.  
  278.     for (int x = 1; x < DIMX - 1; x++)
  279.     {
  280.         for (int y = 1; y < DIMY - 1; y++)
  281.         {
  282.             // if (table[x][y].isAlive && (table[x][y].nearAlive == 2 || table[x][y].nearAlive == 3)) // Stance
  283.             if (table[x][y].isAlive && table[x][y].nearAlive >= 4) table[x][y].isAlive = false; // Mort
  284.             else if (table[x][y].isAlive && table[x][y].nearAlive <= 1) table[x][y].isAlive = false;
  285.             else if (!table[x][y].isAlive && table[x][y].nearAlive == 3) table[x][y].isAlive = true;
  286.         }
  287.     }
  288.  
  289.     for (int x = 1; x < DIMX - 1; x++)
  290.     {
  291.         for (int y = 1; y < DIMY - 1; y++)
  292.         {
  293.             checkNear(table, x, y);
  294.         }
  295.     }
  296.  
  297.     mPrint(table);
  298.     if (isFinished(table, previous)) cout << "Finished";
  299. }
  300.  
  301.  
  302. int main(int, char**)
  303. {
  304.     srand(time(NULL));
  305.     mCase table[DIMX][DIMY];
  306.     mCase previous[2][DIMX][DIMY];
  307.     init(table);
  308.     mPrint(table);
  309.     winInit(title, WINX, WINY);
  310.     setKeyRepeatMode(true);
  311.     grapicInit();
  312.     while (!stop)
  313.     {
  314.         winClear();
  315.         draw(table);
  316.         if (isMousePressed(SDL_BUTTON_LEFT)) update(table, previous);
  317.         stop = winDisplay();
  318.     }
  319.     winQuit();
  320.     system("pause");
  321.     return 0;
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement