Skip to content

dadoPuccio/Game-of-Life

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Conway's Game-of-Life

Conway’s Game of Life - HCI 2021-2022 Programming Assignment

Overview

The Game of Life was invented in 1970 by the British mathematician John Horton Conway. He developed an interest in a problem which was made evident in the 1940’s by mathematician John von Neumann, who aimed to find a hypothetical machine that had the ability to create copies of itself and was successful when he discovered a mathematical model for such a machine with very complicated rules on a rectangular grid. Thus, the Game of Life was Conway’s way of simplifying von Neumann’s ideas. It is the best-known example of a cellular automaton which is any system in which rules are applied to cells and their neighbors in a regular grid. Martin Gardner popularized the Game of Life by writing two articles for his column “Mathematical Games” in the journal Scientific American in 1970 and 1971.

Rules of the Game

The game is played on a two-dimensional grid (or board). Each grid location is either empty or populated by a single cell. A location’s neighbors are any cells in the surrounding eight adjacent locations. The simulation of starts from an initial state of populated locations and then progresses through time. The evolution of the board state is governed by a few simple rules:

  1. Each populated location with one or zero neighbors dies (from loneliness).
  2. Each populated location with four or more neighbors dies (from overpopulation).
  3. Each populated location with two or three neighbors survives.
  4. Each unpopulated location that becomes populated if it has exactly three populated neighbors.
  5. All updates are performed simultaneously in parallel.

This figure illustrates the rules for cell death, survival, and birth: GoL Rules

Implementation

The implementation is done in Python, making use of the MVC architectural pattern together with Observer. PyQt5 is used for the GUI realization whilst Numpy and Scipy are used to perform the update computations on the game board. Pickle is used to load and save the state of the game to raw-binary files.

The Model

The Model is implemented in the CheckboardModel class: there we hold the state of the game, the methods to manage the state, the logic to update the View, the color personalization methods and the load/save functionalities. In order to keep this class not too complex two other classes were employed: Cell and GameColors.

The current state is represented as a dictionary where the keys are tuples (i,j) and the values are Cell: if the key (i,j) exists then such position is occupied by a Cell of a certain state (either "Alive", "Dead" or "Born").
In order to track the status of the game through time we make use of a list named boardHistory, which holds all the state dictionaries that were built, so that we're able to navigate the steps that accurred.

The game loop is managed as well in the CheckboardModel class: through the use of a QTimer we periodically call the .next() method, which computes the next board based on the current state, then notifies the View. We can interact with the timer through the dedicated methods, which are controlled by the speed slider.

The GUI

The class App is in charge to create both the Model and the GUI. The GUI consists of 4 components:

  1. Toolbar: contains load/save/help functions (on top).
  2. ConfigPanel: interface to manage the cell size and interface colors.
  3. GameGrid: it's the grid where the Game of Life is displayed (it's the View in our MVC).
  4. SimulationPanel: contains the controllers for the game: play/pause/back/next/speed/reset.

Each component is coupled to the Model, and presents the user a full control over the Game. The updates of the Model are notified to these components through Observer design pattern.

Application Features

The main window looks like this: Main Window

Interact with the State

The user can interact with the current state drawing new cells using a left click whilst with a right click he can remove the selected cell. We have this same effect when dragging the mouse while clicking.

Start/Pause/Reset

The game simulation starts clicking the play button and can be paused with the pause button on the left. We can reset the state with the specific button on the right.

Variable Framerate

Through the speed slider we're able to change the speed of the simulation, from a minimum of 2 fps up tp 30 fps. The initial value is 10 fps.

Save/Load Game

It is possible to save the current game to a .gol file through the specific option in the File menù on top. The created file is a raw-binary file generated by pickle package, writing to file the boardHistory attribute of the Model. Such games can then be loaded with the specific load option, which reads the pickle file and loads it to the boardHistory.

Save and Load example

Navigate Game History

Through the arrows down on the left it is possible to navigate the previous states of the board as shown below:

Navigation of the state

Custom Colors

It is possible to customize the colors of the 3 types of cells. This can help to visualize how the board state is evolving, as shown below:

Custom Colors

Responsive Layout

The layout is responsive to window resize:

Responsive Layout

Available Games

In the ./Games folder some well known Game of Life configurations are available:

  • Gosper Glider Gun
  • Copperhead Spaceship
  • Spiral

Game Example

Game Example

Requirements

Package Version Required
Python Tested on v3.8.5 Yes
PyQt5 >= 5.6 Yes
Numpy Tested on v1.19.2 Yes
Scipy Tested on v1.6.2 Yes
Pickle Tested on v4.0 Yes

Other versions of these package were not tested. With any or few chagings the code should run just fine.

Credits

About

Conway’s Game of Life - HCI 2021-2022 Programming Assignment

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages