Skip to content

Latest commit

 

History

9 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Conway's Game of Life Implementation

🎮 How the Game Works

Conway's Game of Life is a cellular automaton that evolves based on simple rules:

Rules of the Game:

  • Any live cell with 2 or 3 live neighbors survives to the next generation
  • Any dead cell with exactly 3 live neighbors becomes a live cell (reproduction)
  • All other cells die or remain dead in the next generation

The simulation runs on an infinite two-dimensional grid where each cell interacts with its 8 neighbors (horizontal, vertical, and diagonal).

🕹️ How to Use the Application

Controls Overview:

🎯 Random Button

  • Purpose: Populates the grid with random live cells (approximately 30% density)
  • When to use: Click before starting to create an initial random configuration
  • Effect: Stops any running simulation and generates a new random pattern

▶️ Start Button

  • Purpose: Begins the continuous simulation
  • When to use: After setting up your initial pattern (via Random or manual drawing)
  • Note: Becomes disabled while simulation is running

⏹️ Stop Button

  • Purpose: Pauses the running simulation
  • When to use: To pause and examine the current generation
  • Note: Only enabled when simulation is active

⏭️ Step Button

  • Purpose: Advances the simulation by exactly one generation
  • When to use: For precise control and observation of each generation
  • Note: Only available when simulation is paused

🧹 Clear Button

  • Purpose: Resets the entire grid to all dead cells
  • When to use: To start fresh with a clean grid
  • Effect: Stops simulation and clears all cells

🎚️ Speed Slider

  • Purpose: Controls the simulation speed
  • Range: From Fast (left) to Slow (right)
  • Real-time adjustment: Can be changed during simulation

🖱️ Manual Cell Editing:

  • Click on any cell to toggle its state (alive/dead)
  • Click and drag to draw multiple cells
  • Works even while simulation is running for interactive experimentation

🎨 Predefined Patterns

The application includes several famous Game of Life patterns:

Glider

  • A spaceship that moves diagonally across the grid
  • Select from Patterns → Glider menu

Blinker

  • An oscillator that alternates between horizontal and vertical states
  • Period 2 oscillator

Pulsar

  • A complex period 3 oscillator with fascinating behavior
  • One of the most well-known patterns in Game of Life

📁 Project Structure

GameOfLife/
├── src/
│   └── com/
│       └── studely/
│           └── gameoflife/
│               ├── GameOfLife.java     # Main application class and simulation controller
│               ├── Grid.java           # Core grid logic and game rules implementation
│               ├── CellState.java      # Enum defining cell states (ALIVE/DEAD)
│               └── GameOfLifeUI.java   # Swing-based graphical user interface
├── bin/                               # After Compiling project files (generated)
├── README.md                          # Project documentation
└── .gitignore                         # Git ignore rules

🏗️ Architecture Overview

Core Components:

1. CellState.java - Data Model

public enum CellState {
    ALIVE,   // Represents a living cell
    DEAD     // Represents a dead/empty cell
}
  • Purpose: Simple enum defining the two possible states of a cell
  • Role: Foundation for the entire simulation's data representation

2. Grid.java - Business Logic Layer

public class Grid {
}
  • Responsibilities:
    • Implements Conway's Game of Life rules
    • Manages cell states and neighbor calculations
    • Handles toroidal boundary conditions (wrapping edges)
    • Provides pattern generation (glider, blinker, pulsar)

3. GameOfLife.java - Application Controller

public class GameOfLife {
}
  • Responsibilities:
    • Main application entry point
    • Simulation thread management
    • Generation timing and pacing
    • Coordination between grid and UI

4. GameOfLifeUI.java - Presentation Layer

public class GameOfLifeUI extends JFrame {
}
  • Responsibilities:
    • Graphical rendering of the grid
    • User interaction handling (clicks, drags, buttons)
    • Control panel management
    • Real-time visualization updates

🔄 Data Flow

  1. User InteractionGameOfLifeUI handles input events
  2. UI CommandsGameOfLife controller processes requests
  3. Logic ExecutionGrid performs calculations and updates state
  4. Visual FeedbackGameOfLifeUI repaints the updated grid

🎯 Design Patterns Used

Model-View-Controller (MVC) Pattern:

  • Model: Grid + CellState (data and business logic)
  • View: GameOfLifeUI (presentation layer)
  • Controller: GameOfLife (mediates between model and view)

Observer Pattern:

  • UI components observe simulation state changes
  • Real-time updates through repaint triggers

Strategy Pattern:

  • Different pattern generators (glider, blinker, pulsar)
  • Extensible for additional patterns

🚀 How to Run the Application

Prerequisites:

  • Java JDK 8 or higher
  • Git (for cloning the repository)

Compilation:

# Navigate to project root
cd GameOfLife

# Compile all source files
javac -d bin src/com/studely/gameoflife/*.java

Execution:

# Run the application
java -cp bin com.studely.gameoflife.GameOfLife

Running from Source:

# Clone the repository
git clone https://github.com/your-username/game-of-life.git

# Navigate to project directory
cd game-of-life

# Compile the project
javac -d bin src/com/studely/gameoflife/*.java

# Run the application
java -cp bin com.studely.gameoflife.GameOfLife

Running the JAR file:

java -jar game-of-life.jar

Creating JAR:

# Create executable JAR
jar cfe game-of-life.jar com.studely.gameoflife.GameOfLife -C bin .

⭐ Standout Features

1. Professional Architecture

  • Clean MVC-like separation between simulation logic and UI
  • Well-documented code with JavaDoc comments
  • Proper package structure and encapsulation

2. Advanced Visualization

  • Smooth grid rendering with adjustable cell size
  • Real-time updates during simulation
  • Visual feedback for user interactions

3. Performance Optimized

  • Efficient neighbor counting with toroidal boundary handling
  • Double buffering technique for smooth generation transitions
  • Separate simulation thread to prevent UI freezing

4. Interactive Features

  • Real-time drawing: Modify cells during simulation
  • Adjustable speed: Dynamic simulation speed control
  • Pattern library: Built-in famous Game of Life patterns

5. User Experience

  • Intuitive button layout matching the expected workflow
  • Clear visual indicators (generation counter, speed settings)
  • Responsive design that works on different screen sizes

6. Educational Value

  • Perfect for learning about cellular automata
  • Great visualization of emergent behavior from simple rules
  • Excellent example of object-oriented programming principles

🎯 Technical Highlights

  • Toroidal Universe: The grid wraps around edges, creating an infinite universe
  • Thread-Safe Design: Proper synchronization for thread-safe operations
  • Extensible Architecture: Easy to add new patterns or modify rules
  • Memory Efficient: Uses enum for cell states and optimized data structures

📊 Usage Tips

  1. Start with Random: Click "Random" to see interesting emergent behavior
  2. Adjust Speed: Use the slider to find a comfortable simulation pace
  3. Experiment: Try drawing your own patterns and see how they evolve
  4. Use Patterns: Load predefined patterns to see classic Game of Life behaviors
  5. Step Through: Use "Step" to carefully observe complex pattern evolution

🛠️ Development Features

Modularity:

  • Clean separation of concerns
  • Easy to extend or modify individual components
  • Independent testing capabilities

Extensibility:

  • Add new patterns by extending Grid class
  • Modify rendering by updating GameOfLifeUI
  • Change simulation rules in Grid.nextGeneration()

Maintainability:

  • Comprehensive JavaDoc documentation
  • Consistent coding style
  • Clear method and variable naming

🔧 Technical Stack

  • Language: Java 8+
  • GUI Framework: Java Swing
  • Concurrency: Java Threads for simulation pacing
  • Build Tool: Standard Java compiler (javac)
  • Version Control: Git with proper .gitignore

🎓 Learning Opportunities

This implementation demonstrates:

  • Conway's Game of Life rules and cellular automata concepts
  • Java Swing GUI development
  • Multithreading and synchronization
  • Object-oriented design principles
  • Algorithm optimization techniques

Perfect for both entertainment and educational purposes!

About

A simple Java console implementation of Conway’s Game of Life using a 5x5 grid.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages