Skip to content

guiiireg/tp-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

6 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Conway's Game of Life

A complete Python implementation of Conway's Game of Life cellular automaton with elegant Unicode visualization and interactive evolution.

Python License Status

๐ŸŽฏ What This Project Does

Conway's Game of Life is a Python implementation of the famous cellular automaton devised by mathematician John Conway. This project provides:

  • Random grid generation with customizable sizes (1ร—1 to 100ร—100)
  • Full cellular evolution following Conway's four rules
  • Interactive modes for both automatic and manual evolution
  • Professional Unicode display with box-drawing characters
  • Real-time statistics and pattern detection
  • Educational codebase with clear, modular architecture

โœจ Why This Project Is Useful

๐ŸŽ“ Educational Value

  • Perfect for learning cellular automata concepts
  • Demonstrates modular Python architecture and best practices
  • Shows algorithmic thinking with neighbor counting and rule application
  • Illustrates interactive programming with user input handling

๐Ÿ–ฅ๏ธ Technical Excellence

  • Zero dependencies - pure Python standard library
  • Type-safe implementation with comprehensive type hints
  • Clean terminal UI with Unicode box-drawing characters
  • Structured code following consistent commenting patterns

๐Ÿ”ฌ Scientific Exploration

  • Observe emergent patterns like oscillators, still lifes, and spaceships
  • Study complexity arising from simple rules
  • Experiment with different initial configurations
  • Analyze population dynamics and pattern stability

๐Ÿš€ Getting Started

Prerequisites

  • Python 3.6 or higher
  • Terminal with Unicode support (most modern terminals)
  • No external dependencies required

Installation

  1. Clone the repository:

    git clone https://github.com/guiiireg/tp-python.git
    cd tp-python
  2. Run the program:

    python main.py

Basic Usage

$ python main.py
=== GAME OF LIFE ===
Generating initial grid
Enter the grid size (n for an nร—n grid, max 100): 10

Current Grid:
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚โ–    โ–  โ–    โ–      โ–   โ”‚
โ”‚  โ–      โ–  โ–  โ–    โ–   โ”‚
โ”‚โ–  โ–  โ–    โ–    โ–       โ”‚
โ”‚    โ–  โ–    โ–  โ–  โ–     โ”‚
โ”‚โ–      โ–  โ–    โ–    โ–   โ”‚
โ”‚  โ–  โ–  โ–      โ–  โ–     โ”‚
โ”‚โ–    โ–    โ–  โ–    โ–  โ–   โ”‚
โ”‚  โ–  โ–      โ–  โ–       โ”‚
โ”‚โ–  โ–    โ–  โ–  โ–    โ–     โ”‚
โ”‚    โ–    โ–    โ–  โ–  โ–   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Grid 10ร—10 successfully generated!
Living cells: 49 (49.0%)
Dead cells: 51

Start automatic evolution? (y/n): y

๐ŸŽฎ Features

Core Functionality

  • ๐Ÿ“Š Grid Generation: Create random initial configurations
  • ๐Ÿ”„ Evolution Engine: Apply Conway's rules for cell transitions
  • ๐Ÿ‘๏ธ Visualization: Unicode terminal display with professional borders
  • ๐Ÿ“ˆ Statistics: Live tracking of cell populations and percentages
  • ๐ŸŽ›๏ธ Interactive Controls: Choose between automatic and manual evolution

Conway's Game of Life Rules

The evolution follows these four fundamental rules:

  1. Underpopulation: Live cell with < 2 neighbors dies
  2. Survival: Live cell with 2-3 neighbors survives
  3. Overpopulation: Live cell with > 3 neighbors dies
  4. Birth: Dead cell with exactly 3 neighbors becomes alive

Evolution Modes

  • ๐Ÿค– Automatic Mode: Continuous evolution with timed generations
  • ๐Ÿ‘ค Manual Mode: Step-by-step control with user input
  • ๐Ÿ›‘ Smart Detection: Automatic stopping for stable states or extinction

๐Ÿ“ Project Architecture

tp-python/
โ”œโ”€โ”€ main.py           # Core application logic and user interface
โ”œโ”€โ”€ config.py         # Configuration constants and symbols
โ”œโ”€โ”€ utils.py          # Utility functions and evolution algorithms
โ”œโ”€โ”€ game/             # Future modular extensions
โ””โ”€โ”€ .github/          # Project documentation and AI instructions
    โ”œโ”€โ”€ copilot-instructions.md
    โ””โ”€โ”€ prompts/

Key Components

File Purpose Key Functions
main.py Application entry point generate_grid(), display_grid(), run_evolution()
config.py Constants and symbols Unicode characters, evolution rules, UI messages
utils.py Core algorithms evolve_grid(), count_neighbors(), apply_evolution_rules()

๐Ÿ”ง Technical Implementation

Data Model

# Grid representation: 2D list of integers
grid: List[List[int]] = [
    [0, 1, 0],  # 0 = dead cell, 1 = alive cell
    [1, 1, 1],
    [0, 1, 0]
]

Algorithm Overview

def evolve_grid(grid):
    """Apply Conway's rules to create next generation"""
    for each cell:
        neighbor_count = count_neighbors(grid, row, col)
        next_state = apply_evolution_rules(current_state, neighbor_count)
    return new_grid

Code Style

The project follows a distinctive structured commenting pattern:

#------------------------------------------------------------------------------------
# Major section header (84 characters)
#------------------------------------------------------------------------------------
def function_name():
    # Subsection logic
    #--------------------------------------------------------------------------------------
    implementation_code()
    #--------------------------------------------------------------------------------------

๐ŸŽฏ Usage Examples

Quick Start

# Generate a small 5ร—5 grid and watch it evolve
python main.py
# Enter: 5
# Choose: y (automatic evolution)

Manual Control

# Generate a larger grid with manual stepping
python main.py
# Enter: 20
# Choose: n (manual evolution)
# Press Enter to advance each generation

Pattern Observation

Create interesting patterns by experimenting with different grid sizes:

  • Small grids (5-10): Good for observing oscillators and still lifes
  • Medium grids (15-30): Watch gliders and other moving patterns
  • Large grids (50-100): Complex population dynamics and emergent behavior

๐Ÿงฌ Scientific Patterns

Conway's Game of Life produces fascinating patterns:

  • ๐Ÿ”„ Oscillators: Patterns that repeat (blinkers, beacons)
  • ๐Ÿš€ Spaceships: Patterns that move across the grid (gliders)
  • ๐Ÿ  Still Lifes: Stable patterns that never change (blocks, beehives)
  • ๐ŸŒ€ Complex Dynamics: Chaotic behavior from simple rules

๐Ÿ“š Learning Resources

This implementation is perfect for understanding:

  • Cellular Automata Theory: How simple rules create complex behavior
  • Algorithm Design: Neighbor counting and state transitions
  • Python Programming: Modular design, type hints, and clean code
  • Terminal UI: Unicode characters and interactive programming

๐Ÿ› ๏ธ Development

Running the Code

# Main application
python main.py

# Code follows Python 3.6+ standards
# No external dependencies required
# Full type hints for IDE support

Code Organization

  • Modular design with clear separation of concerns
  • Pure functions for algorithms (no side effects)
  • Constants centralized in configuration module
  • Type safety with comprehensive type annotations

๐Ÿ“„ License

This project is developed for educational purposes as part of a Python programming course (TP = Travaux Pratiques).


Experience the fascinating world of cellular automata with Conway's Game of Life!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages