Skip to content

Instantly share code, notes, and snippets.

@James-Dolan
Last active August 29, 2015 14:16
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 James-Dolan/61c4172a23c8e145d15b to your computer and use it in GitHub Desktop.
Save James-Dolan/61c4172a23c8e145d15b to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include "cellGrid.hpp"
CellGrid::CellGrid() : row(100, 0), grid(100, row), dynamicRow(100, 0), dynamicGrid(100, dynamicRow){
for(int i = 0; i<100; i++){
for(int j = 0; j<100; j++){
this->setCell(i, j, false);
}
}
}
CellGrid::~CellGrid(){
}
void CellGrid::setCell(int i, int j, bool life){
this->grid[i][j] = life;
}
bool CellGrid::getCell(int i, int j){
return(this->grid[i][j]);
}
bool CellGrid::getDCell(int i, int j){
return(this->dynamicGrid[i][j]);
}
void CellGrid::setDCell(int i, int j, bool life){
this->dynamicGrid[i][j] = life;
}
void CellGrid::cycle(){
static int move[8][2] = {{-1,0}, {-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}};
int rLength = this->row.size();
int cLength = this->grid.size();
int neigh = 0;
for(int i = 0; i < rLength; i++){
for(int j = 0; j < cLength; j++){
for(int l = 0; l < 8; l++){
int ti = i + move[l][0];
int tj = j + move[l][1];
if(ti < 0 || ti >= rLength || tj < 0 || tj >= cLength){
continue;
}
if(this->getCell(ti, tj)){
neigh += 1;
}
}
if(this->getCell(i, j)){
if(neigh <= 1 || neigh >= 4){
this->setDCell(i, j, false);
}
}
else{
if(neigh == 3){
this->setDCell(i, j, true);
}
}
}
}
for(int i = 0; i < rLength; i++){
for(int j = 0; j < cLength; j++){
this->setCell(i, j, this->getDCell(i, j));
}
}
}
#ifndef CELLGRID_HPP_
#define CELLGRID_HPP_
#include <vector>
class CellGrid {
public:
CellGrid();
~CellGrid();
bool getCell(int i, int j);
void setCell(int i, int j, bool life);
bool getDCell(int i, int j);
void setDCell(int i, int j, bool life);
void cycle();
private:
std::vector<bool> row;
std::vector<std::vector<bool> > grid;
std::vector<bool> dynamicRow;
std::vector<std::vector<bool> > dynamicGrid;
};
#endif /* CELLGRID_HPP_ */
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <vector>
#include "cellGrid.hpp"
int main(){
sf::RenderWindow window(sf::VideoMode(1000, 1000), "Game of Life");
CellGrid mainGrid;
bool running = false;
sf::RectangleShape gGrid[100][100];
for(int i = 0; i < 100; i++){
for(int j = 0; j < 100; j++){
sf::RectangleShape gCell;
gCell.setSize(sf::Vector2f(10, 10));
gCell.setFillColor(sf::Color::Black);
gCell.setPosition(i*10, j*10);
gGrid[i][j] = gCell;
}
}
while(window.isOpen()){
sf::Event event;
while(window.pollEvent(event)){
if(event.type == sf::Event::Closed){
window.close();
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)){
//color the square && update mainGrid
float x = sf::Mouse::getPosition(window).x;
float y = sf::Mouse::getPosition(window).y;
for(int i = 0; i < 100; i++){
for(int j = 0; j<100; j++){
if(x >= gGrid[i][j].getPosition().x && x <= gGrid[i][j].getPosition().x+10 && y >= gGrid[i][j].getPosition().y && y <= gGrid[i][j].getPosition().y+10){
mainGrid.setCell(i, j, !mainGrid.getCell(i, j));
}
}
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::LShift)){
running = !running;
}
}
if(running){
window.clear(sf::Color::Black);
for(int i = 0; i < 100; i++){
for(int j = 0; j<100; j++){
if(mainGrid.getCell(i, j) == true){
gGrid[i][j].setFillColor(sf::Color(i+100,j+100, i+j+20));
window.draw(gGrid[i][j]);
}
else{
gGrid[i][j].setFillColor(sf::Color::Black);
window.draw(gGrid[i][j]);
}
}
}
window.display();
mainGrid.cycle();
}
else{
window.clear(sf::Color::Black);
for(int i = 0; i < 100; i++){
for(int j = 0; j<100; j++){
if(mainGrid.getCell(i, j) == true){
gGrid[i][j].setFillColor(sf::Color::Yellow);
window.draw(gGrid[i][j]);
}
else{
gGrid[i][j].setFillColor(sf::Color::Black);
window.draw(gGrid[i][j]);
}
}
}
window.display();
}
}
return 0;
}
CXX = g++
CXXFLAGS = -g -std=c++11
SFMLFLAGS = -lsfml-graphics -lsfml-window -lsfml-system
%.o : %.cpp %.hpp
$(CXX) -c $< $(CXXFLAGS)
GAME_OBJECTS = main.o cellGrid.o
game-of-life: $(GAME_OBJECTS)
$(CXX) -o $@ $(GAME_OBJECTS) $(CXXFLAGS) $(SFMLFLAGS)
clean:
$(RM) *.o game-of-life
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment