The Game of Life is a cellular automata created by John Horton Conway. Displayed within a grid, each cell is defined by its state, live or dead. The cells are updated according to the following rules:
- Any live cell with fewer than two live neighbours dies, as if by underpopulation.
- Any live cell with two or three live neighbours lives on to the next generation.
- Any live cell with more than three live neighbours dies, as if by overpopulation.
- Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
With just these simple rules, given certain simple enough looking initial conditions, great complexity can arise.
From a command prompt in the game_of_life
directory, run the following commands:
mkdir build
cd build
cmake ..
make all
./game_of_life
To build project using VS Code, navigate to the game_of_life
directory:
- From a command prompt, run
code .
- Press Ctrl + Shift + X to open the Extensions panel.
- Ensure that CMake Tools by Microsoft is installed. (ms-vscode.cmake-tools)
- Press Ctrl + Shift + P and type
cmake build
bool get_cell(int row, int col)
- returns true if there is a live cell at (row, col)
void set_cell(int row, int col, bool is_alive)
- sets the state of the cell at (row, col) to
is_alive
- sets the state of the cell at (row, col) to
void update()
- increments the grid by one tick and updates the board
void prev_state()
- restores the state of the grid to the previous tick
void clear()
- clears the board of any live cells
int count_neighbors(int row, col)
- returns the number cells that are alive and adjacent to (row, col)
bool in_bounds(int row, col)
- returns true if (row, col) is within the bounds of the grid
bool* bit_array
- pointer to the boolean array storing the values of the grid
std::vector<bool*> prev_states
- vector storing all previous states of the grid
-
enum Color
- An enum to allow easy referral to the three colors used in the program
-
select_color(Color color)
- Sets the current openGL color to the appropriate color as specified by the color paramater.
-
void fill_cell(int row, int col, Color color)
- Fill the cell at (row, col) with color.
-
void draw_lines()
- Draw the grid lines.
-
void display_grid()
- Display the grid.
-
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
- A function that is called by glfw every when it polls for events. This method implements the appropriate actions to be taken for each relevant key press.
-
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
- A function that is called by glfw every time there is a mouse button press event. This method toggles the state of the cell that is underneath the mouse.
- To use glfw and openGL we first must initialize glfw and make a current openGL context.
- We make the appropriate calls to glfw to set the callbacks with:
glfwSetKeyCallback
andglfwSetMouseButtonCallback
- In the event loop, we first create the next frame by calling
glfwPollEvents
and updating the grid ifplay == true
and atime_step
has elapsed. - Finally, we call
glfwSwapBuffers
to swap the back and front buffers, thereby displaying the frame we have just drawn on.
- Clicking on a square on the grid toggles its state
- space toggles the flow of time
- ↑ or K or W doubles the rate of time
- ↓ or J or S halfs the rate of time
- N increments time step by one
- P decrements time step by one
- C clears the grid of any live cells
- ESC exits the program
- Ths project was started using the template found at https://github.com/juliettef/GLFW-CMake-starter.