|
| 1 | +# Conway's Game of Life Cellular Automata - Java |
| 2 | + |
| 3 | +This is a basic implementation of the cellular automaton John Conway's Game. |
| 4 | +I made it to practice using Object-Oriented Design principles when starting |
| 5 | +out with Java just experiment and play around a bit. It initialises a board |
| 6 | +with randomly placed cells and begins the simulation. |
| 7 | + |
| 8 | +<p align="center"><img src="imgs/screenshot.png" title="" alt="screenshot of program" data-align="center" width="70%"></p> |
| 9 | + |
| 10 | +## Rules |
| 11 | +The rules of the simulation are rather simple, below is an excerpt from this [Wikipedia page](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life#Rules). |
| 12 | +> The universe of the Game of Life is an infinite, two-dimensional orthogonal grid of square |
| 13 | +> cells, each of which is in one of two possible states, live or dead (or populated and unpopulated, |
| 14 | +> respectively). Every cell interacts with its eight neighbours, which are the cells that are |
| 15 | +> horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur: |
| 16 | +> 1. Any live cell with fewer than two live neighbours dies, as if by underpopulation. |
| 17 | +> 2. Any live cell with two or three live neighbours lives on to the next generation. |
| 18 | +> 3. Any live cell with more than three live neighbours dies, as if by overpopulation. |
| 19 | +> 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. |
| 20 | +> |
| 21 | +> These rules, which compare the behaviour of the automaton to real life, can be condensed into the following: |
| 22 | +> |
| 23 | +> 1. Any live cell with two or three live neighbours survives. |
| 24 | +> 2. Any dead cell with three live neighbours becomes a live cell. |
| 25 | +> 3. All other live cells die in the next generation. Similarly, all other dead cells stay dead. |
| 26 | +> |
| 27 | +> The initial pattern constitutes the seed of the system. The first generation is created by applying |
| 28 | +> the above rules simultaneously to every cell in the seed, live or dead; births and deaths occur simultaneously, |
| 29 | +> and the discrete moment at which this happens is sometimes called a tick. Each generation is a pure |
| 30 | +> function of the preceding one. The rules continue to be applied repeatedly to create further generations. |
| 31 | +
|
| 32 | +## Design |
| 33 | +This simulation was based around the principles of object-oriented programming, so functionality is separated into |
| 34 | +different classes as can be seen in the UML diagram below which was generated using PlantUML (source: `diagram.plantuml`). |
| 35 | + |
| 36 | +<p align="center"><img src="imgs/uml.png" title="" alt="uml diagram" data-align="center" width="70%"></p> |
| 37 | + |
| 38 | +### Configurable Parameters |
| 39 | +It is possible to change the pixel dimensions of the canvas, as well as the cell count of the grid. |
| 40 | +As can be seen in the UML diagram, the `Main.java` class contains 4 constants: `WINDOW_WIDTH`, `WINDOW_HEIGHT`, |
| 41 | +`GRID_WIDTH`, and `GRID_HEIGHT`. These can all be modified to change the appearance of the canvas. |
| 42 | +```java |
| 43 | +private final static int WINDOW_WIDTH = 1000; |
| 44 | +private final static int WINDOW_HEIGHT = 1000; |
| 45 | +private final static int GRID_WIDTH = 500; |
| 46 | +private final static int GRID_HEIGHT = 500; |
| 47 | +``` |
| 48 | + |
| 49 | +It is also possible to change the percentage of initial active cells in the `Grid.java` class, by modifying the |
| 50 | +`activeCellsPct` value in the constructor. The tick period can also be changed. |
| 51 | +This can be done in the `Main.java` class as shown: |
| 52 | +```java |
| 53 | +public static void main(String[] args) { |
| 54 | + Grid grid = new Grid(GRID_WIDTH, GRID_HEIGHT, 0.2); // <-- the percentage is here |
| 55 | + GFX gfx = new GFX(WINDOW_WIDTH, WINDOW_HEIGHT, grid); // (0.2 or 20% by default) |
| 56 | + while(true) { |
| 57 | + try { |
| 58 | + TimeUnit.MILLISECONDS.sleep(20); // <-- tick period (default 20ms) |
| 59 | + } catch(InterruptedException e) {} |
| 60 | + |
| 61 | + grid.update(); |
| 62 | + gfx.repaint(); |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | +### Note |
| 67 | +Using OOP for an automata is not exaclty the most efficient way, this program was just made to illustrate |
| 68 | +how OOP can be used for such a task. I made this as a side project as part of my IB Computer Science course |
| 69 | +in highschool. |
| 70 | + |
| 71 | +## Building and Running an Executable |
| 72 | +This project was made in the Eclipse IDE and used standard Java libraries so there should not be any problem with |
| 73 | +importing the files into an Eclipse project, building and running. |
0 commit comments