|
| 1 | +'''Conway's Game Of Life, Author Anurag Kumar(mailto:[email protected]) |
| 2 | +
|
| 3 | +Requirements: |
| 4 | + - numpy |
| 5 | + - random |
| 6 | + - time |
| 7 | + - matplotlib |
| 8 | +
|
| 9 | +Python: |
| 10 | + - 3.5 |
| 11 | +
|
| 12 | +Usage: |
| 13 | + - $python3 game_o_life <canvas_size:int> |
| 14 | +
|
| 15 | +Game-Of-Life Rules: |
| 16 | + |
| 17 | + 1. |
| 18 | + Any live cell with fewer than two live neighbours |
| 19 | + dies, as if caused by under-population. |
| 20 | + 2. |
| 21 | + Any live cell with two or three live neighbours lives |
| 22 | + on to the next generation. |
| 23 | + 3. |
| 24 | + Any live cell with more than three live neighbours |
| 25 | + dies, as if by over-population. |
| 26 | + 4. |
| 27 | + Any dead cell with exactly three live neighbours be- |
| 28 | + comes a live cell, as if by reproduction. |
| 29 | + ''' |
| 30 | +import numpy as np |
| 31 | +import random, time, sys |
| 32 | +from matplotlib import pyplot as plt |
| 33 | +import matplotlib.animation as animation |
| 34 | +from matplotlib.colors import ListedColormap |
| 35 | + |
| 36 | +usage_doc='Usage of script: script_nama <size_of_canvas:int>' |
| 37 | + |
| 38 | +choice = [0]*100 + [1]*10 |
| 39 | +random.shuffle(choice) |
| 40 | + |
| 41 | +def create_canvas(size): |
| 42 | + canvas = [ [False for i in range(size)] for j in range(size)] |
| 43 | + return canvas |
| 44 | + |
| 45 | +def seed(canvas): |
| 46 | + for i,row in enumerate(canvas): |
| 47 | + for j,_ in enumerate(row): |
| 48 | + canvas[i][j]=bool(random.getrandbits(1)) |
| 49 | + |
| 50 | +def run(canvas): |
| 51 | + ''' This function runs the rules of game through all points, and changes their status accordingly.(in the same canvas) |
| 52 | + @Args: |
| 53 | + -- |
| 54 | + canvas : canvas of population to run the rules on. |
| 55 | +
|
| 56 | + @returns: |
| 57 | + -- |
| 58 | + None |
| 59 | + ''' |
| 60 | + canvas = np.array(canvas) |
| 61 | + next_gen_canvas = np.array(create_canvas(canvas.shape[0])) |
| 62 | + for r, row in enumerate(canvas): |
| 63 | + for c, pt in enumerate(row): |
| 64 | + # print(r-1,r+2,c-1,c+2) |
| 65 | + next_gen_canvas[r][c] = __judge_point(pt,canvas[r-1:r+2,c-1:c+2]) |
| 66 | + |
| 67 | + canvas = next_gen_canvas |
| 68 | + del next_gen_canvas # cleaning memory as we move on. |
| 69 | + return canvas.tolist() |
| 70 | + |
| 71 | +def __judge_point(pt,neighbours): |
| 72 | + dead = 0 |
| 73 | + alive = 0 |
| 74 | + # finding dead or alive neighbours count. |
| 75 | + for i in neighbours: |
| 76 | + for status in i: |
| 77 | + if status: alive+=1 |
| 78 | + else: dead+=1 |
| 79 | + |
| 80 | + # handling duplicate entry for focus pt. |
| 81 | + if pt : alive-=1 |
| 82 | + else : dead-=1 |
| 83 | + |
| 84 | + # running the rules of game here. |
| 85 | + state = pt |
| 86 | + if pt: |
| 87 | + if alive<2: |
| 88 | + state=False |
| 89 | + elif alive==2 or alive==3: |
| 90 | + state=True |
| 91 | + elif alive>3: |
| 92 | + state=False |
| 93 | + else: |
| 94 | + if alive==3: |
| 95 | + state=True |
| 96 | + |
| 97 | + return state |
| 98 | + |
| 99 | + |
| 100 | +if __name__=='__main__': |
| 101 | + if len(sys.argv) != 2: raise Exception(usage_doc) |
| 102 | + |
| 103 | + canvas_size = int(sys.argv[1]) |
| 104 | + # main working structure of this module. |
| 105 | + c=create_canvas(canvas_size) |
| 106 | + seed(c) |
| 107 | + fig, ax = plt.subplots() |
| 108 | + fig.show() |
| 109 | + cmap = ListedColormap(['w','k']) |
| 110 | + try: |
| 111 | + while True: |
| 112 | + c = run(c) |
| 113 | + ax.matshow(c,cmap=cmap) |
| 114 | + fig.canvas.draw() |
| 115 | + ax.cla() |
| 116 | + except KeyboardInterrupt: |
| 117 | + # do nothing. |
| 118 | + pass |
0 commit comments