Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <vector>
- #include <time.h>
- #include <stdlib.h>
- #include <windows.h>
- #include <thread>
- #include <chrono>
- std::vector<std::vector<bool>> GOL;
- std::vector<std::vector<bool>> GOLtmp;
- long gen = 0;
- void clear_screen() {
- system("CLS");
- }
- enum rendermodes
- {
- RENDER_CONSOLE = 0,
- TOTAL
- };
- void GOL_Update()
- {
- GOLtmp = GOL;
- for (int i = 0; i < GOLtmp.size(); i++)
- {
- for (int j = 0; j < GOLtmp[0].size(); j++)
- {
- // ITS UGLY AND I KNOOOW IIIT
- if (i <= 1 || i >= GOLtmp.size() - 1 || j <= 1 || j >= GOLtmp[0].size() - 1)
- {
- GOL[i][j] = false;
- }
- else
- {
- int nearbysum =
- GOLtmp[i - 1][j - 1] +
- GOLtmp[i - 1][j] +
- GOLtmp[i - 1][j + 1] +
- GOLtmp[i][j - 1] +
- GOLtmp[i][j + 1] +
- GOLtmp[i + 1][j - 1] +
- GOLtmp[i + 1][j] +
- GOLtmp[i + 1][j + 1];
- if (GOLtmp[i][j] == false) // ded
- {
- if (nearbysum == 3)
- {
- GOL[i][j] = true;
- }
- }
- else
- {
- if (nearbysum != 2 && nearbysum != 3)
- {
- GOL[i][j] = false;
- }
- }
- }
- }
- }
- gen++;
- }
- void GOL_Render(int mode)
- {
- using namespace std;
- int alive = 0;
- COORD cp = { 0, 0 };
- HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleCursorPosition(console, cp);
- if (mode == rendermodes::RENDER_CONSOLE)
- {
- for (int i = 0; i < GOL.size(); i++)
- {
- for (int j = 0; j < GOL[0].size(); j++)
- {
- if (GOL[i][j] == false)
- {
- cout << ' ';
- }
- else
- {
- cout << char(219);
- alive++;
- }
- }
- cout << endl;
- }
- }
- cout << endl << char(178) << char(178) << " Life statistics" << endl <<
- char(178) << endl <<
- char(178) << " Gen " << gen << endl <<
- char(178) << " Alive : " << alive << endl <<
- char(178);
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- using namespace std;
- srand(time(NULL));
- cout << "Filling the GOL grid randomly" << endl;
- GOL.resize(30);
- for (int i = 0; i < GOL.size(); i++)
- {
- GOL[i].resize(30);
- for (int j = 0; j < GOL[i].size(); j++)
- {
- GOL[i][j] = rand() % 2;
- }
- }
- cout << "Available renderers : " << rendermodes::TOTAL << endl;
- while (true)
- {
- GOL_Update();
- GOL_Render(rendermodes::RENDER_CONSOLE);
- //this_thread::sleep_for(chrono::milliseconds(500));
- }
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement