The Game of Life


[ Delphi Forum ] [ Delphi Tutorials -- by DelphiLand ]

Posted by Jessica on May 29, 2003 at 15:20:24:

Hello can anyone help me with writing a program to simulate the game of life ?

Here are the rules of one of my favorite theroys/observations/games

1. If at the last observation a cell was alive and had fewer than two living neighbours, it dies of loneliness.
2. If at the last observation a cell was alive and had four or more living neighbours, it dies of overcrowding.
3. If at the last observation a cell was alive and had two or three living neighbours, it survives.
4. If at the last observation a cell was dead and had exactly three living neighbours, it springs to life.
5. All other cells remain dead.


The program is to simulate the game of Life with a grid of 10 cells by 10 cells. The number of observations are three (3), plus the initial grid. The grid coordinates are to be located in a file named cells.txt, that contain the cells that are alive at the beginning.

One pair of coordinates are located in each row of the text file and they are all valid entries. Display the world indicated by the initial grid and then display the updated grids for the three transitions.
The symbol * denotes a living cell, a space a dead cell, and I want to place a border around the grid. And use two arrays, one to indicate the current status of each cell, and the other to count the number of live neighbours of each cell.
3. what the file cells.txt is:
1 4
1 6
1 7
1 9
1 10
2 1
etc.

The output for the above input file would produce the following:
Initially:-
-----------------------------------------
| | | | * | | * | * | | * | * |
+---+---+---+---+---+---+---+---+---+---+
| * | | * | * | | * | * | * | * | * |
+---+---+---+---+---+---+---+---+---+---+
| * | * | * | * | | * | * | * | | * |
+---+---+---+---+---+---+---+---+---+---+
| | | * | * | * | * | | * | | |
+---+---+---+---+---+---+---+---+---+---+
| | | * | * | * | * | | * | | |
+---+---+---+---+---+---+---+---+---+---+
| | | * | * | * | * | * | | * | * |
+---+---+---+---+---+---+---+---+---+---+
| | | | * | * | * | | | * | * |
+---+---+---+---+---+---+---+---+---+---+
| | * | | * | * | * | | | | |
+---+---+---+---+---+---+---+---+---+---+
| | * | * | | | | * | * | | * |
+---+---+---+---+---+---+---+---+---+---+
| * | | * | | * | | * | | | |
-----------------------------------------
Transition 1:-
-----------------------------------------
| | | * | * | | * | | | | * |
+---+---+---+---+---+---+---+---+---+---+
| * | | | | | | | | | |
+---+---+---+---+---+---+---+---+---+---+
| * | | | | | | | | | * |
+---+---+---+---+---+---+---+---+---+---+
| | | | | | | | * | | |
+---+---+---+---+---+---+---+---+---+---+
| | * | | | | | | * | | |
+---+---+---+---+---+---+---+---+---+---+
| | | | | | | | | | * |
+---+---+---+---+---+---+---+---+---+---+
| | | | | | | | * | * | * |
+---+---+---+---+---+---+---+---+---+---+
| | * | | | | | | * | | * |
+---+---+---+---+---+---+---+---+---+---+
| * | | | | | | * | * | | |
+---+---+---+---+---+---+---+---+---+---+
| | | * | * | | * | * | * | | |
-----------------------------------------

Transition 2:-
-----------------------------------------
| | | | | | | | | | |
+---+---+---+---+---+---+---+---+---+---+
| | * | | | | | | | | |
+---+---+---+---+---+---+---+---+---+---+
| | | | | | | | | | |
+---+---+---+---+---+---+---+---+---+---+
| | | | | | | | | * | |
+---+---+---+---+---+---+---+---+---+---+
| | | | | | | | | * | |
+---+---+---+---+---+---+---+---+---+---+
| | | | | | | | * | | * |
+---+---+---+---+---+---+---+---+---+---+
| | | | | | | | * | | * |
+---+---+---+---+---+---+---+---+---+---+
| | | | | | | | | | * |
+---+---+---+---+---+---+---+---+---+---+
| | * | * | | | * | | | | |
+---+---+---+---+---+---+---+---+---+---+
| | | | | | * | | * | | |
-----------------------------------------
Transition 3:-
-----------------------------------------
| | | | | | | | | | |
+---+---+---+---+---+---+---+---+---+---+
| | | | | | | | | | |
+---+---+---+---+---+---+---+---+---+---+
| | | | | | | | | | |
+---+---+---+---+---+---+---+---+---+---+
| | | | | | | | | | |
+---+---+---+---+---+---+---+---+---+---+
| | | | | | | | * | * | * |
+---+---+---+---+---+---+---+---+---+---+
| | | | | | | | * | | * |
+---+---+---+---+---+---+---+---+---+---+
| | | | | | | | | | * |
+---+---+---+---+---+---+---+---+---+---+
| | | | | | | | | * | |
+---+---+---+---+---+---+---+---+---+---+
| | | | | | | * | | | |
+---+---+---+---+---+---+---+---+---+---+
| | | | | | | * | | | |
-----------------------------------------

I know these are the arrays I have to use

const
GRID_SIZE = 10;
FILENAME = 'cells.txt';
type
LifeType = array[ 1..GRID_SIZE, 1..GRID_SIZE ] of boolean;
CountType = array[ 1..GRID_SIZE, 1..GRID_SIZE ] of integer;
·


After reading the text file, and populating the LifeType data structure, I have to close the file prior to manipulating the data structures further.

I hope someone can help me I am giving a speech on the mathematician J.H. Conway who invented the simulation game called Life.

And I want to give an example of his theroy in action.

Can anyone help?



[ Delphi Forum ]
[ DelphiLand: free Delphi source code, tips, tutorials ]