#!/usr/bin/env python3
"""
game_of_life.py
This program models John Conway's "Game of Life."
"""

__author__ = "Richard White"
__version__ = "2023-11-28"

import random
import os
import time


def create_empty_world(WIDTH, HEIGHT):
    """Creates a new world, a list of lists with
    WIDTH cols and HEIGHT rows.
    """
    world = []
    for row in range(HEIGHT):
        world.append([])
        for col in range(WIDTH):
            world[row].append(0)
    return world 

def fill_world(world):
    """Randomly fills the world with live cells,
    which are indicated by the value 1
    """
    for row in range(len(world)):
        for col in range(len(world[0])):
            if random.randrange(5) == 0:    # 1 in 3 possibility
                world[row][col] = 1         # 1 = live cell


def print_world(world):
    """Display a graphical representation of the random walker
    in the world.
    """
    os.system("clear")
    for row in range(len(world)):
        for col in range(len(world[0])):
            if world[row][col] == 0:
                print('.', end = "")
            else:
                print('0', end = "")
        print() # go down to the next line

def count_neighbors(world, row, col):
    """This returns the number of neighbors (out of a total
    of 8 possible) surrounding the cell at row, col
    """
    count = 0
    for r in range(row - 1, row + 2):
        for c in range(col - 1, col + 2):
            if r >= 0 and r < len(world) and \
               c >= 0 and c < len(world[0]) and \
               not(r == row and c == col) and \
                world[r][c] == 1:
                count += 1
    return count 




def main():
    WIDTH = 40     # screen width in characters
    HEIGHT = 12     # screen width in characters
    counter = 0
    world = create_empty_world(WIDTH, HEIGHT)
    print_world(world)
    fill_world(world)
    print_world(world)
    print(count_neighbors(world, 0, 0))


if __name__ == "__main__":
    main()


