Skip to content
This repository has been archived by the owner on Mar 27, 2019. It is now read-only.

echebbi/game-of-life

Repository files navigation

A Java Game of Life

This is a Java 8 implementation of Conway's Game of Life. The Game is a popular exercise, commonly discovered while learning programming.

If you don't know it, you're invited to check Wikipedia.

Motivations

This project is originally a school project, but I improved it in order to introduce myself to Java 8 and to reactive programming, more specifically to the RxJava library.

Implementation

Disclaimer: this project has not been designed to be as simple as possible, but to produce a Game of Life easily configurable. If you're looking for a short and reactive Java 8 implementation, take a look at this gist:

The Generation class represents a set of cells, which is equivalent to one Game's generation. It provides several methods to launch the evolution (i.e. to generate next generations) with tailored arguments. Since the various parameters can confuse the code, the Evolution class provides a higher-level interface to configure a Game of Life.

The following code snippet shows how to:

  • Create a 6*6 game area,
  • Place a Glider at its center,
  • Launch the evolution with tailored options.
public class Main {
    
    private static final int WIDTH = 6;
    private static final int HEIGHT = 6;

    public static void main(String[] args) {
        
        new Evolution().size(WIDTH, HEIGHT)
                // There are several variants of Conway's game of life.
                // followRule allows to select the one to use (custom ones can be defined too)
                .followRule(Rule.GAME_OF_LIFE)
                // Populate the world with a glider located at its center
                .populateWith(Pattern.GLIDER.transformToCenter(WIDTH, HEIGHT))
                // Print each generation to the console
                .forEach(new PrintToStream())
                // Write each generation in an XML file
                .forEach(new WrittenOnCompleted("test.xml", new XMLRepresentation()))
                // Write each generation in an HTML file
                .forEach(new WrittenOnCompleted("test.html", new HTMLRepresentation()))
                // Stop the evolution if :
                // - a sequence is repeated
                // - the world becomes stable
                // - the world becomes empty
                .stop(new WhenRepeated().or(new WhenStable()).or(new WhenEmpty()))
                // Generate 100 generations
                .evolve(100);
    }
}

How to run

The code presented above corresponds to the class fr.kazejiyu.gameoflife.Main. It can be run easily by executing the following steps:

  • Clone the repository:
git clone https://github.com/echebbi/game-of-life
  • Build the sources:
cd game-of-life/
./gradlew build
  • Then execute the generated JAR:
cd /build/libs/
java -jar game-of-life.jar