RTL.ConwaysLifeFMX Sample
ConwaysLifeFMX is a sample application that implements a parallelized Conways Game of Life. This game is a zero-player game that evolves from an initial state with no further input.
Contents
Location
You can find the ConwaysLifeFMX project at:
- Start | Programs | Embarcadero RAD Studio 10.2 Tokyo | Samples and then navigate to either of the following folders:
CPP\RTL\Parallel Library\FMX
CPP\Multi-Device Samples\Parallel Library\FMX
Object Pascal\RTL\Parallel Library\FMX
Object Pascal\Multi-Device Samples\Parallel Library\FMX
- Subversion Repository:
Description
This sample is an implementation of the Conways Game of Life that shows the performance of the TParallel.For versus the serial running.
In the application interface you have the following controls:
- Parallel checkbox lets you switch from the serial version (not checked) to the parallel version (checked).
- Start button runs the life generation, Stop resumes the game.
- Clear button cleans the board of cells.
- Re Center focuses the grid board centering both scrollbars.
- Load button allows you to load
.LIF
files. These files are different starting points for the simulation. They are contained in theLifs
folder in the project.
How to Use the Sample
- Navigate to the location given above and open ConwaysLifeFMX.dproj or ConwaysLifeFMXCpp.cbproj.
- Select Run > Run to run the application.
- On the application interface:
- Click the Start button (with the Parallel checkbox unchecked).
- Look at the number of
Generations per seconds
displayed in the interface (these are values of the serial version of the game implementation). - Click the Stop button.
- Now check the Parallel option and click the Start button again.
- Look again at the number of
Generations per seconds
displayed and see how this value has now increased.Note: You can check the CPU performance on the task manager in your computer to see the use that the parallel version does.
- Click again the Stop button.
- Click the Load button and select any
.LIF
file from the \RTL\Parallel Library\Lifs\ folder, for exampleACORN.LIF
. - Click again the Start button to see the life generation.
Tip: To visualize the evolution of the game you might Stop, then Re Center and finally Start again the application.
Files
File | Contains |
---|---|
ConwaysLifeFMX |
The ConwaysLife FireMonkey project. |
LifeUnitFMX.fmx |
The main form that is the user interface for the Conways Game of Life sample. |
LifeUnitFMX.pas |
The container holding source code for the methods used in the ConwaysLifeFMX sample. |
\RTL\Parallel Library\Shared_Code\LifeEngine.pas |
The container holding source code for the life engine of the Conways Game of Life. |
\RTL\Parallel Library\Lifs\*.LIF |
The |
Classes
Each application defines a single class that implements the main form of the application.
- TLifeForm is the main form that handles all controls.
- TLifeEngine implements the different threads for updating generations.
- ELifeEngine implements exceptions for the life engine.
Implementation
- A
ProcessCells
function will run in the serial version of the application (Parallel option unchecked). It will process on one generation at the time. - The TParallel.For method is implemented to increase the efficiency of the application. It runs having the Parallel option checked. TParallel.For processrs a lot of cells of the board simultaneously.
Uses
LifeEngine