This project is an exploration of building a Text User Interface (TUI) using the Bubble Tea framework, applied to the well-understood problem of Conway's Game of Life.
The Game of Life, devised by mathematician John Conway, is a cellular automaton simulation that demonstrates how complex patterns can emerge from simple rules. This implementation brings the classic simulation to life in your terminal using Go and the Bubble Tea framework.
- Bubble Tea TUI: Utilizes the Bubble Tea framework to create an interactive and visually appealing terminal interface.
- Game of Life Simulation: Implements the classic rules of Conway's Game of Life.
- Interactive Controls: Toggle the simulation on/off and quit the application with simple key commands.
- Colorful Display: Uses lipgloss for styling, making the game board visually distinct and easy to read.
- Flexible Initialization: Supports random generation or initialization from input files.
- Customizable Grid Size: Allows specifying custom grid dimensions via command-line flags.
The application combines the simplicity of the Game of Life rules with the power of Bubble Tea's event-driven architecture:
- Grid Initialization: The game starts with either a randomly populated grid or a predefined pattern from an input file.
- Simulation Loop: In each tick, the next generation of cells is calculated based on the current state.
- User Interface: Bubble Tea handles the rendering and user input, creating a responsive TUI.
- Game Logic: The core Game of Life rules are implemented in the
nextGeneration
function.
To run the Game of Life TUI:
- Ensure you have Go installed on your system.
- Clone this repository.
- Run
go mod tidy
to install dependencies. - Execute
go run main.go
to start the application with default settings.
There are several ways to run the program:
-
Default Random Grid:
go run main.go
-
Custom Grid Size:
go run main.go -width 60 -height 40
This will create a 60x40 grid with random initial state.
-
Using an Input File:
go run main.go -input examples/glider_gun.gol
This will initialize the grid based on the pattern in the
glider_gun.gol
file.
Input files should use the following format:
- Use
*
or1
to represent live cells - Use any other character (typically
.
) to represent dead cells - Ensure all rows have the same length
Example (glider pattern):
....
.*..
..*.
***
- Press
r
to toggle the simulation on/off - Press
q
orCtrl+C
to quit the application
Unit tests are provided to ensure the correctness of the Game of Life rules implementation. Run go test
to execute the tests.
You can create a GIF or video of the Game of Life TUI in action using VHS. Here's how:
-
Install VHS:
- On macOS, you can use Homebrew:
brew install vhs
- For other operating systems, follow the instructions on the VHS GitHub page.
- On macOS, you can use Homebrew:
-
Run the following command in your terminal:
vhs -o vhs/gol-tui.gif vhs/gol-tui.tape
We intend to implement many of the patterns found on https://conwaylife.com. Feel free to send pull requests.
This project demonstrates how a well-understood problem like the Game of Life can be reimagined as an interactive terminal application using modern Go libraries. It serves as both a learning tool and a starting point for more complex TUI applications.
Feel free to explore, modify, and expand upon this code to deepen your understanding of Go, Bubble Tea, and cellular automata!