Advertisement
Guest User

Untitled

a guest
Apr 30th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include "stdafx.h"
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <time.h>
  6. #include <stdlib.h>
  7.  
  8. #include <windows.h>
  9.  
  10. #include <thread>
  11. #include <chrono>
  12.  
  13. std::vector<std::vector<bool>> GOL;
  14. std::vector<std::vector<bool>> GOLtmp;
  15.  
  16. long gen = 0;
  17.  
  18. void clear_screen() {
  19.     system("CLS");
  20. }
  21.  
  22. enum rendermodes
  23. {
  24.     RENDER_CONSOLE = 0,
  25.     TOTAL
  26. };
  27.  
  28. void GOL_Update()
  29. {
  30.     GOLtmp = GOL;
  31.  
  32.     for (int i = 0; i < GOLtmp.size(); i++)
  33.     {
  34.         for (int j = 0; j < GOLtmp[0].size(); j++)
  35.         {
  36.             // ITS UGLY AND I KNOOOW IIIT
  37.             if (i <= 1 || i >= GOLtmp.size() - 1 || j <= 1 || j >= GOLtmp[0].size() - 1)
  38.             {
  39.                 GOL[i][j] = false;
  40.             }
  41.             else
  42.             {
  43.  
  44.                 int nearbysum =
  45.                     GOLtmp[i - 1][j - 1] +
  46.                     GOLtmp[i - 1][j] +
  47.                     GOLtmp[i - 1][j + 1] +
  48.                     GOLtmp[i][j - 1] +
  49.                     GOLtmp[i][j + 1] +
  50.                     GOLtmp[i + 1][j - 1] +
  51.                     GOLtmp[i + 1][j] +
  52.                     GOLtmp[i + 1][j + 1];
  53.  
  54.                 if (GOLtmp[i][j] == false) // ded
  55.                 {
  56.                     if (nearbysum == 3)
  57.                     {
  58.                         GOL[i][j] = true;
  59.                     }
  60.                 }
  61.                 else
  62.                 {
  63.                     if (nearbysum != 2 && nearbysum != 3)
  64.                     {
  65.                         GOL[i][j] = false;
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.     }
  71.  
  72.     gen++;
  73. }
  74.  
  75. void GOL_Render(int mode)
  76. {
  77.     using namespace std;
  78.  
  79.     int alive = 0;
  80.  
  81.     COORD cp = { 0, 0 };
  82.     HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
  83.     SetConsoleCursorPosition(console, cp);
  84.  
  85.  
  86.     if (mode == rendermodes::RENDER_CONSOLE)
  87.     {
  88.         for (int i = 0; i < GOL.size(); i++)
  89.         {
  90.             for (int j = 0; j < GOL[0].size(); j++)
  91.             {
  92.                 if (GOL[i][j] == false)
  93.                 {
  94.                     cout << ' ';
  95.                 }
  96.                 else
  97.                 {
  98.                     cout << char(219);
  99.                     alive++;
  100.                 }
  101.             }
  102.  
  103.             cout << endl;
  104.         }
  105.     }
  106.  
  107.     cout << endl << char(178) << char(178) << " Life statistics" << endl <<
  108.                     char(178) << endl <<
  109.                     char(178) << " Gen " << gen << endl <<
  110.                     char(178) << " Alive : " << alive << endl <<
  111.                     char(178);
  112. }
  113.  
  114. int _tmain(int argc, _TCHAR* argv[])
  115. {
  116.     using namespace std;
  117.  
  118.     srand(time(NULL));
  119.  
  120.     cout << "Filling the GOL grid randomly" << endl;
  121.  
  122.     GOL.resize(30);
  123.     for (int i = 0; i < GOL.size(); i++)
  124.     {
  125.         GOL[i].resize(30);
  126.         for (int j = 0; j < GOL[i].size(); j++)
  127.         {
  128.             GOL[i][j] = rand() % 2;
  129.         }
  130.     }
  131.  
  132.     cout << "Available renderers : " << rendermodes::TOTAL << endl;
  133.  
  134.     while (true)
  135.     {
  136.         GOL_Update();
  137.         GOL_Render(rendermodes::RENDER_CONSOLE);
  138.  
  139.         //this_thread::sleep_for(chrono::milliseconds(500));
  140.     }
  141.  
  142.     system("PAUSE");
  143.  
  144.     return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement