Skip to content

claby2/gol

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gol (Game of Life)

gol is a Game of Life library for C++.

With gol, you can easily simulate Conway's Game of Life cellular automaton in C++. gol is a single-file include which means you only need one header file to use it.

Features

  • A board class to iterate through and store a Conway's Game of Life simulation
  • Custom rule strings, wrapping, and neighborhood types
  • File parsing and saving (including support for run-length encoding)
  • Documentation

Installation

  1. Download the latest single header version.
  2. Either put the header file in a central location (with a specified path) or directly in your project tree.

Examples

To compile all the examples:

$ make example-compile

Blinker Oscillator

Here is an implementation that prints a blinker oscillator and steps 4 times:

// Implements a blinker oscillator and steps 4 times
// blinker oscillators have a period of 2 

#include <gol/gol.hpp>

#include <iostream>

gol::Board board(10, 10);

// Prints the board and then updates with next step
void printAndStep() {
    std::cout << "\n";
    for(int i = 0; i < board.height(); i++) {
        for(int j = 0; j < board.width(); j++) {
            std::cout << board[i][j] << " ";
        }
        std::cout << "\n";
    }
    board.nextStep(); // Update with next step with rule_string B3/S23
}

int main() {
    for(int i = 0; i < board.height(); i++) {
        for(int j = 0; j < board.width(); j++) {
            board[i][j] = false;
        }
    }

    // Make blinker oscillator
    board[2][2] = true;
    board[2][3] = true;
    board[2][4] = true;

    for(int i = 0; i < 4; i++) {
        printAndStep();
    }
}

This will print the following twice:

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 
0 0 1 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 

0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 

Preview

Previewing requires SDL2.

Refer to the /preview directory to see how gol can be used with SDL2 to preview simulations graphically.

Compile the previewer with:

$ make preview-compile

Run the previewer with:

$ make preview

Testing

Testing requires Catch2.

Compile with:

$ make test-compile

Run the tests with:

$ make test

Reference

Documentation

Functions

Function Use
initRandom Initialize random seed
getRandomBool Return a random bool
getBirthValues Return birth values from B/S notation rule string
getSurvivalValues Return survival values from B/S notation rule string
isValidRuleString Test whether rule string is valid

Board Member Functions

Function Use
(constructor) Construct Game of Life board
operator[] Access element
height Return the number of rows
width Return the number of columns
countNeighborsMoore Return number of true cells in Moore neighborhood
countNeighborsNeumann Return number of true cells in von Neumann neighborhood
nextStep Iterate to next time step
setWrap Set to toggle wrapping for counting neighbors
getWrap Return the current wrap state
getNeighborhoodType Return current neighborhood type
setFromFile Set values of board from given file
getLiveCount Return the number of true elements
getDeadCount Return the number of false elements
setFromRLEFile Set values of board from given RLE file
setRuleString Set rule string
getRuleString Return current rule string
saveAsFile Save board as file
saveAsRLEFile Save board as RLE file