patterncppMinor
Implementing Conway's Game of Life
Viewed 0 times
lifegameimplementingconway
Problem
Here is an implementation of Conway's Game of Life. My code is working fine but I want you to review and give me your suggestions of how to improve it.
These are the steps of my code:
gameOfLife.h
implementation.cpp
These are the steps of my code:
- Initialize the grid cells with zeros (all cells are dead at the start)
- Then randomly change some of the grid cells to one (bring some cells to life)
- Draw the grid
- Update the grid, by checking which cells are going to live or die
- Go to step 3
gameOfLife.h
#ifndef GAME_OF_LIFE_H
#define GAME_OF_LIFE_H
#include
#include
class gameOfLife
{
public:
typedef std::vector> gridVector;
private:
int gridSize;
gridVector mainGrid;
gridVector *gridCopy;
public:
gameOfLife();
public:
void run();
void initGrid();
void drawGrid();
void updateGrid();
void copyGrid ();
int checkNeighbors(gridVector *grid, int x, int y);
void nextGeneration(int x, int y, int lod);
int randGenerate(int start, int end);
private:
std::random_device randDevice;
std::mt19937 gen;
};
#endifimplementation.cpp
#include
#include "gameOfLife.h";
gameOfLife::gameOfLife() : gridSize(15), gen(randDevice())
{
}
void gameOfLife::initGrid()
{
for(int i = 0; i genenNum(start, end - 1);
return genenNum(gen);
}
void gameOfLife::drawGrid()
{
char ch = 254;
for(int i = 1; i 3)
{
mainGrid[x][y] = 0;
}
}
void gameOfLife::run()
{
mainGrid.resize(gridSize);
initGrid();
while(true)
{
drawGrid();
updateGrid();
system("cls");
}
}
int main()
{
gameOfLife gol;
gol.run();
std::cin.get();
return 0;
}Solution
-
The reason this is incorrect is because this is a preprocessor directive, which do not follow the same syntax as compiled lines.
-
Instead of using a
you can now use C++11's type alias:
-
If you don't need the default constructor, you can either leave it out or make it explicitly defaulted:
-
There's really no need for multiple
-
I'm not sure why you have a
After removing this member, you should also remove
-
I'm not too familiar with GoL, but I don't think that
#includes should not end with a semicolon:#include "gameOfLife.h";The reason this is incorrect is because this is a preprocessor directive, which do not follow the same syntax as compiled lines.
-
Instead of using a
typedef:typedef std::vector> gridVector;you can now use C++11's type alias:
using std::vector> = gridVector;-
If you don't need the default constructor, you can either leave it out or make it explicitly defaulted:
gameOfLife() = default;-
There's really no need for multiple
public and private sections. If you prefer to organize or separate the members in some way, you can just add line breaks.-
I'm not sure why you have a
*gridCopy member. Not only would it be more natural to use a copy constructor in such cases, but having a raw pointer as a member would "force" you to have an overloaded copy constructor. This is because the default copy constructor performs shallow copies, which will copy the pointer instead of the value at that pointer.After removing this member, you should also remove
copyGrid().-
I'm not too familiar with GoL, but I don't think that
while loop in run() is supposed to run infinitely. This type of while loop will not stop unless a break or return is encountered. You may need to modify it in such a way that it can end.Code Snippets
#include "gameOfLife.h";typedef std::vector< std::vector<int>> gridVector;using std::vector<std::vector<int>> = gridVector;gameOfLife() = default;Context
StackExchange Code Review Q#63401, answer score: 4
Revisions (0)
No revisions yet.