Skip to content

Instantly share code, notes, and snippets.

@samuelnub
Created October 26, 2016 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuelnub/2e856d972f1a0f6bcd486de39b27f66e to your computer and use it in GitHub Desktop.
Save samuelnub/2e856d972f1a0f6bcd486de39b27f66e to your computer and use it in GitHub Desktop.
conway's game of life in c++
#include <iostream>
#include <vector>
#include <sstream>
#include <time.h>
#include <chrono>
#include <thread>
#include <random>
#include <climits>
class GameOfLife
{
public:
GameOfLife(uint32_t boardX = 80, uint32_t boardY = 80, uint64_t iterations = 256, std::chrono::milliseconds interval = std::chrono::milliseconds(40), uint64_t seed = time(0)) :
x(boardX),
y(boardY),
board(boardX, std::vector<bool>(boardY))
{
this->initBoard(seed);
for (uint64_t i = 0; i < iterations; i++)
{
this->tick();
std::this_thread::sleep_for(interval);
}
std::cout << "Conway's game of life implementation in C++\nBased on that WikiBooks article in Java!\nhttps://en.wikibooks.org/wiki/Algorithm_Implementation/Simulation/Conway%27s_Game_of_Life\n~Sam\n\n";
std::cin >> boardX;
}
~GameOfLife() {}
protected:
private:
uint32_t x;
uint32_t y;
std::vector<std::vector<bool>> board;
// Private member functions that should be encapsulated away
void initBoard(int64_t seed)
{
std::mt19937_64 module;
module.seed(seed);
for (uint32_t curX = 0; curX < this->x; curX++)
{
for (uint32_t curY = 0; curY < this->y; curY++)
{
auto gen = module();
if (gen <= (LLONG_MAX / 4))
{
this->board[curX][curY] = true;
}
}
}
}
void printBoard()
{
std::stringstream boardString;
for (uint32_t curX = 0; curX < this->x; curX++)
{
for (uint32_t curY = 0; curY < this->y; curY++)
{
if (this->board[curX][curY] == true)
{
boardString << "#";
}
else
{
boardString << " ";
}
}
boardString << "\n";
}
/*
for (const auto &curX : this->board)
{
boardString << "=";
}
*/
std::cout << boardString.rdbuf() << "\n";
}
uint32_t neighbourCount(uint32_t xCoord, uint32_t yCoord)
{
uint32_t count = 0;
if (this->board[(xCoord + 1) % this->x][yCoord])
{
count++;
}
if (this->board[(xCoord + 1) % this->x][(yCoord + 1) % this->y])
{
count++;
}
if (this->board[xCoord][(yCoord + 1) % this->y])
{
count++;
}
if (this->board[xCoord][(yCoord - 1) % this->y])
{
count++;
}
if (this->board[(xCoord + 1) % this->x][(yCoord - 1) % this->y])
{
count++;
}
if (this->board[(xCoord - 1) % this->x][yCoord])
{
count++;
}
if (this->board[(xCoord - 1) % this->x][(yCoord - 1) % this->y])
{
count++;
}
if (this->board[(xCoord - 1) % this->x][(yCoord + 1) % this->y])
{
count++;
}
return count;
}
bool getPoint(uint32_t xCoord, uint32_t yCoord)
{
return this->board[xCoord][yCoord] &&
this->neighbourCount(xCoord, yCoord) == 2 ||
this->neighbourCount(xCoord, yCoord) == 3;
}
void tick()
{
std::vector<std::vector<bool>> nextBoard = { this->x, std::vector<bool>(this->y) };
for (uint32_t curX = 0; curX < this->x; curX++)
{
for (uint32_t curY = 0; curY < this->y; curY++)
{
nextBoard[curX][curY] = this->getPoint(curX, curY);
}
}
this->printBoard();
this->board = nextBoard;
}
};
int main()
{
GameOfLife life = {};
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment