# L-15 MCS 507 Mon 25 Sep 2023 : run_game_better_show.py

"""
The script uses scitools.std to run the game of life
applying the rules of John Conway.
"""

import numpy as np
from scipy import sparse
import matplotlib
import matplotlib.pyplot as plt
from run_game_better import update

def main():
    """
    Generates a random matrix and applies
    the rules for Conway's game of life.
    """
    ratio = 0.2  # ratio of nonzeroes
    dim = 200  # dimension of the matrix
    alive = np.random.rand(dim, dim)
    alive = np.matrix(alive < ratio, np.uint8)
    plt.ion()
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.set_xlim(-1, dim+1)
    ax.set_ylim(-1, dim+1)
    spm = sparse.coo_matrix(alive)
    dots, = ax.plot(spm.row, spm.col, 'b.')
    strtitle = 'game of life'
    ax.set_title(strtitle)
    fig.canvas.draw()
    plt.pause(0.00001)
    for i in range(5*dim):
        spm = sparse.coo_matrix(alive)
        dots.set_xdata(spm.row)
        dots.set_ydata(spm.col)
        strtitle = 'game of life at step %d' % (i+1)
        ax.set_title(strtitle)
        fig.canvas.draw()
        plt.pause(0.00001)
        alive = update(alive)

main()
