Skip to content

Instantly share code, notes, and snippets.

@Spartan322
Created August 12, 2017 17:59
Show Gist options
  • Save Spartan322/37447ca3e670e8f3448a66df57c2b355 to your computer and use it in GitHub Desktop.
Save Spartan322/37447ca3e670e8f3448a66df57c2b355 to your computer and use it in GitHub Desktop.
#include <cstdlib>
#include <random>
#include <vector>
#include <iostream>
#include <SFML/Graphics.hpp>
class Grid {
const int MIN_SURROUND = 2;
const int MIN_SURROUND_SWITCH = 3;
const int MAX_SURROUND = 3;
const int MIN_OFFSET_CHECK = -1;
const int MAX_OFFSET_CHECK = 1;
typedef std::vector<std::vector<bool>> Matrix;
int m_width, m_height;
float m_distribRatio;
Matrix m_positionsMatrix;
Matrix m_tempMatrix;
public:
Grid(int width, int height, float liveDistribRatio = 0.1) :
m_width(width), m_height(height), m_distribRatio(liveDistribRatio)
{
GenerateGrid();
}
void GenerateGrid()
{
std::random_device rd;
std::mt19937 gen(rd());
// give "true" 1/4 of the time
// give "false" 3/4 of the time
std::bernoulli_distribution d(m_distribRatio);
TerminateGrid();
for(int x = 0; x < m_width; x++)
{
m_positionsMatrix.push_back(std::vector<bool>(m_height));
for(int y = 0; y < m_height; y++) m_positionsMatrix[x][y] = d(gen);
}
}
void TerminateGrid()
{
m_positionsMatrix.clear();
}
int CountNeighbors(int x, int y)
{
int popCount = 0;
for(int xOffset = -1; xOffset < 2; xOffset++)
for(int yOffset = -1; yOffset < 2; yOffset++)
{
if(xOffset == 0 && yOffset == 0) continue;
try {
if(m_tempMatrix.at(x+xOffset).at(y+yOffset))
{
popCount++;
std::cout << "neighbor count added: " << popCount << std::endl;
}
}
catch (std::out_of_range e) { std::cout << "check out of range" << std::endl; }
}
return popCount;
}
void TickGrid()
{
m_tempMatrix = Matrix(m_positionsMatrix);
for(int x = 0; x < m_positionsMatrix.size(); x++)
for(int y = 0; y < m_positionsMatrix[x].size(); y++)
{
int popCount = CountNeighbors(x, y);
switch(popCount)
{
case 2:
break;
case 3:
if(!m_tempMatrix[x][y]) m_positionsMatrix[x][y] = true;
break;
default:
if(m_tempMatrix[x][y]) m_positionsMatrix[x][y] = false;
break;
}
}
}
void DrawGrid(sf::RectangleShape liveBlock, sf::RectangleShape deadBlock, sf::RenderTarget& target)
{
for(int x = 0; x < m_positionsMatrix.size(); x++) {
for(int y = 0; y < m_positionsMatrix[x].size(); y++)
{
if(m_positionsMatrix[x][y])
{
liveBlock.setPosition(x*(liveBlock.getSize().x), y*(liveBlock.getSize().y));
target.draw(liveBlock);
}
else
{
deadBlock.setPosition(x*(deadBlock.getSize().x), y*(deadBlock.getSize().y));
target.draw(deadBlock);
}
}
}
}
};
int main()
{
Grid mainGrid(100, 100);
// Create the main window
sf::RenderWindow app(sf::VideoMode(600, 600), "Conways Game Of Life");
sf::RectangleShape liveBlock(sf::Vector2f(5,5));
liveBlock.setFillColor(sf::Color::White);
sf::RectangleShape deadBlock(sf::Vector2f(5,5));
deadBlock.setFillColor(sf::Color::Black);
app.setFramerateLimit(60);
// Start the game loop
while (app.isOpen())
{
// Process events
sf::Event event;
while (app.pollEvent(event))
{
// Close window : exit
if (event.type == sf::Event::Closed)
app.close();
if (event.type == sf::Event::KeyPressed)
mainGrid.TickGrid();
}
// Clear screen
app.clear(sf::Color::Black);
// Draw the sprite
mainGrid.DrawGrid(liveBlock, deadBlock, app);
// Update the window
app.display();
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment