| 37 | return grid |
| 38 | |
| 39 | def play(grid): |
| 40 | noOfCellsAlive = 0 |
| 41 | nGrid = copy.deepcopy(grid) |
| 42 | dR = [1, 1, 1, -1, -1, -1, 0, 0] |
| 43 | dC = [1, 0, -1, -1, 0, 1, 1, -1] |
| 44 | |
| 45 | def isValid(r, c) -> bool: |
| 46 | return (r >= 0 and r < rows and c >= 0 and c < cols) |
| 47 | |
| 48 | for i in range(rows): |
| 49 | for j in range(cols): |
| 50 | count = 0 |
| 51 | for r1, c1 in zip(dR, dC): |
| 52 | r = r1+i |
| 53 | c = c1+j |
| 54 | |
| 55 | if isValid(r, c) and grid[r][c]: |
| 56 | count += 1 |
| 57 | |
| 58 | if grid[i][j] and (count < 2 or count > 3): |
| 59 | nGrid[i][j] = False |
| 60 | if grid[i][j] == False and count == 3: |
| 61 | nGrid[i][j] = True |
| 62 | |
| 63 | noOfCellsAlive += nGrid[i][j] |
| 64 | |
| 65 | return [nGrid, noOfCellsAlive] |
| 66 | |
| 67 | # Inception |
| 68 | grid = seed() |