Fishes and Sharks

Language : C++

Role : Programming

Group Size : Solo

Summary of Fishes and Sharks

Fishes and sharks is a version of John Conway's Game of life which uses cellular automata to simulate how fishes and sharks interact with each other by following a few simple rules. For each generation, all the cells in the simulation follow the same rules and get updated depending on the state of the surrounding cells.

The rules that the simulation uses are as follows:

Breeding Rules (For empty cells)

breeding age, and there are <4 of the other  species, then  create a species of that  type  

Shark Rules

Fish Rules

Purpose of the assignment

This assignment was designed to help us practice and improve our C++ skills as well as learn how to use threads and implement parallel programming.

Summary of Code

At the beginning of the code, a 2D array is initialised which is then populated with each cell being either a fish, a shark or empty. The way a cell represents being one of the three is by using positive and negative numbers, with fishes being represented by positive numbers, sharks being represented by negative numbers and an empty cell being represented as a zero. The actual number within the cell, whether it is positive or negative, shows the age of the fish or shark. 

 To determine what each cell would initially represent, the 2D array is looped through and for each cell, a random number is chosen between zero and nine. If the random number chosen is between zero and four, that cell is determined to be a fish, between five and eight is a shark and nine is an empty cell.

The ghost cells are then created which allows for the cells on opposite ends of the 2D array to interact with each other which helps improve the efficiency of the code. 

The main part of the code is the implementation of the rules for breeding and survival which is achieved by looping through the populated 2D array and for each cell, the value stored in the eight surrounding cells are stored in a temporary array. Depending on what the current cell is representing, the surrounding cells will determine what the new value will be. The updated value of the cell, after having the rules applied, is stored in a new separate 2D array to prevent the new value affecting updating of the other cells.

Once the whole of the 2D array is looped through, the fishes and sharks are displayed as rectangles before the rules are implemented, with fishes being yellow rectangles and sharks being red rectangles. The 2D array is then updated with the new values and the process repeats until the chosen number of generations the program is to run for is reached.