  GNU nano 7.2                                                                                                                                                                                                                           game11.py
#!/usr/bin/env python3
"""
Each cell is an object.
"""

import random
import time
import os

# ---- Rules / Configuration ----
ROWS = 100
COLS = 100
ALIVE = "O"
DEAD = " "

# ---- Cell class ----
class Cell:
    """Represents one cell in the grid."""
    def __init__(self, alive=False):
        self.alive = alive

# ---- Game class ----
class GameOfLife:
    """Represents the Game of Life grid and rules."""
    def __init__(self, rows, cols, p_alive):
        self.rows = rows
        self.cols = cols
        # Create a grid of Cell objects with random alive/dead
        self.grid = [[Cell(random.random() < p_alive) for _ in range(cols)] for _ in range(rows)]

    def count_neighbors(self, r, c):
        """Count how many alive neighbors a cell has."""
        count = 0
        for dr in [-1, 0, 1]:
            for dc in [-1, 0, 1]:
                if dr == 0 and dc == 0:
                    continue  # skip self
                nr, nc = r + dr, c + dc
                if 0 <= nr < self.rows and 0 <= nc < self.cols:
                    if self.grid[nr][nc].alive:
                        count += 1
        return count

    def step(self):
        """Compute the next generation based on Game of Life rules."""
        # Create a new grid of dead cells
        new_grid = [[Cell(False) for _ in range(self.cols)] for _ in range(self.rows)]

        for r in range(self.rows):
            for c in range(self.cols):
                n = self.count_neighbors(r, c)
                if self.grid[r][c].alive:
                    # Alive cell survives if 2 or 3 neighbors
                    if n == 2 or n == 3:
                        new_grid[r][c].alive = True
                    else:
                        new_grid[r][c].alive = False  # dies otherwise
                else:
                    # Dead cell becomes alive if exactly 3 neighbors
                    if n == 3:
                        new_grid[r][c].alive = True
                    else:
                        new_grid[r][c].alive = False  # stays dead

        # Update the grid
        self.grid = new_grid

    def print_grid(self):
        """Print the grid in the terminal."""
        for row in self.grid:
            print("".join(ALIVE if cell.alive else DEAD for cell in row))

# ---- Helper function to clear terminal ----
def clear():
    os.system("cls" if os.name == "nt" else "clear")

# ---- Main program ----
def main():
    print("Welcome to Conway's Game of Life!")
    print("Rules:")
    print("1. Live cell with 2 or 3 neighbors survives.")
    print("2. Dead cell with exactly 3 neighbors becomes alive.")
    print("3. All other cells die or stay dead.")

    try:
        perc = float(input("Enter percentage of live cells (0-100): "))
    except:
        perc = 20.0
    p_alive = perc / 100.0

    try:
        max_gen = int(input("Enter number of generations to run: "))
    except:
        max_gen = 100

    game = GameOfLife(ROWS, COLS, p_alive)

    for gen in range(max_gen):
        clear()
        print(f"Generation {gen+1}/{max_gen}")
        game.print_grid()
        game.step()
        time.sleep(0.1)

    print("Simulation finished!")

if __name__ == "__main__":
    main()



