An implementation of John Conway's Game of Life in C++ using SDL2 for rendering.
The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970. It is a zero-player game, meaning that its evolution is determined by its initial state, requiring no further input. One interacts with the Game of Life by creating an initial configuration and observing how it evolves. It is Turing complete and can simulate a universal constructor or any other Turing machine.
The game works on the following simple rules :-
- Any live cell with two or three live neighbours survives.
- Any dead cell with three live neighbours becomes a live cell- .
- All other live cells die in the next generation. Similarly-ll other dead cells stay dead.
- Random generation of initial patterns.
- Visualization of each generation using SDL2.
- Customizable screen dimensions (SCREEN_HEIGHT and SCREEN_WIDTH constants in the code).
Static Shapes | Oscillators | Spaceships |
---|---|---|
Block | Blinker | Glider |
Bee-Hive | Toad | Light Weight Sapceship |
Loaf | Beacon | Medium Weight Sapceship |
Boat | Pulsar | Heavy Weight Sapceship |
Tub | Pentadecathlon |
- Clone the repository:
git clone https://github.com/your-username/GameOfLife.git
- Compile the code using your preferred C++ compiler.
g++ -std=c++20 <your-file-name>.cpp -o <your-output-file> -lSDL2
- Run the executable.
./<your-output-file>
SDL2: Simple DirectMedia Layer is a cross-platform development library designed to provide low-level access to audio, keyboard, mouse, and display.
- Optimize code for better cache utilization.
- Implement a terminal version of the Game of Life.
To install SDL2, you'll need to follow platform-specific instructions, as the installation process can vary depending on your operating system. Here are instructions for common platforms:
-
Download SDL2 Development Libraries:
- Go to the SDL2 download page: https://www.libsdl.org/download-2.0.php.
- Under "Development Libraries," download the "Visual C++" version (e.g.,
SDL2-devel-2.x.x-VC.zip
). - Extract the contents of the downloaded ZIP file to a location on your computer.
-
Set Up Visual Studio (If Using Visual Studio):
- If you're using Visual Studio, create a new C++ project.
- Go to "Project Properties" > "Configuration Properties" > "VC++ Directories."
- Add the path to the
include
directory inside the extracted SDL2 folder to "Include Directories." - Add the path to the
lib
directory inside the extracted SDL2 folder to "Library Directories."
-
Link SDL2 Library:
- Under "Configuration Properties" > "VC++ Directories," add
SDL2.lib
andSDL2main.lib
to "Additional Dependencies" under "Linker" > "Input."
- Under "Configuration Properties" > "VC++ Directories," add
-
Copy SDL2.dll:
- From the extracted SDL2 folder, copy
SDL2.dll
to your project's executable directory (where your.exe
file is located).
- From the extracted SDL2 folder, copy
-
Using Homebrew (Recommended):
-
Open Terminal.
-
Install Homebrew if you haven't already: https://brew.sh/
-
Run the following command to install SDL2:
brew install sdl2
-
-
Using MacPorts (Alternative):
-
Open Terminal.
-
Install MacPorts if you haven't already: https://www.macports.org/install.php
-
Run the following command to install SDL2:
sudo port install libsdl2
-
-
Using APT:
-
Open Terminal.
-
Update your package list:
sudo apt-get update
-
Install SDL2 development libraries:
sudo apt-get install libsdl2-dev
-
-
Using Snap (Alternative):
-
Open Terminal.
-
Install SDL2 via Snap:
sudo snap install sdl2
-
You can typically install SDL2 via your distribution's package manager. Use the appropriate package manager and search for the SDL2 development libraries.
After installing SDL2, you can compile and run your SDL2-based C++ programs by including the appropriate SDL2 headers and linking against the SDL2 library during compilation.
This project is licensed under the MIT License.