HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Game of Life in Python

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
lifegamepython

Problem

I have this interpretation of John Conway's Game of Life in Python 3.3.1:

#JOHN CONWAY'S GAME OF LIFE
def countSurrounding(universe, a, b):
    count = 0
    surrounding = [[a - 1, b - 1],
                   [a - 1, b    ],
                   [a - 1, b + 1],
                   [a    , b - 1],
                   [a    , b + 1],
                   [a + 1, b - 1],
                   [a + 1, b    ],
                   [a + 1, b + 1]]
    for a in range(0, 8):
        try:
            if universe[surrounding[a][0]][surrounding[a][1]] == 1:
                count += 1
        except IndexError: a
    return count
def printUniverse(universe):
    for a in universe:
        print(a)
    print("\n")
nextUniverse = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 1, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 1, 0, 0, 0, 0],
                [0, 0, 1, 1, 1, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0]]
for gen in range(0, 10):
    universe = [a[:] for a in nextUniverse]
    printUniverse(universe)
    for a in range(0, 9):
        for b in range(0, 9):
            if universe[a][b] == 0 and countSurrounding(universe, a, b) == 3:
                nextUniverse[a][b] = 1
            elif universe[a][b] == 1 and countSurrounding(universe, a, b) not in (2, 3):
                nextUniverse[a][b] = 0


How can I improve/optimize my code?

EDIT: After the answers below, I have updated my code:

```
#JOHN CONWAY'S GAME OF LIFE
def countSurrounding(universe, a, b):
count = 0
surrounding = ((a - 1, b - 1),
(a - 1, b ),
(a - 1, b + 1),
(a , b - 1),
(a , b + 1),
(a + 1, b - 1),
(a + 1, b ),
(a + 1, b + 1))
for a, b in surrounding:

Solution

Overall, it seems really really good to me and I like the way you use IndexError to count surrounding cells in a concise way without bothering about edge cases.

Here are a few comments anyway :

Removing a useless check

This is not much but you can remove a check of universe[a][b] in

if universe[a][b] == 0 and countSurrounding(universe, a, b) == 3:
            nextUniverse[a][b] = 1
        elif universe[a][b] == 1 and countSurrounding(universe, a, b) not in (2, 3):
            nextUniverse[a][b] = 0


and it becomes

surr = countSurrounding(universe, a, b)
        if universe[a][b]:
            if surr not in (2, 3):
                nextUniverse[a][b] = 0
        else:
            if surr == 3:
                nextUniverse[a][b] = 1


(It looks slightly longer though) (I also took this chance to use an additional variable)

Use tuple instead of list if it's not something you plan to iterate over

Here, the surroundings are just pairs and that should be enough. Also, it makes the access of the different elements much more convenient through tuple unpacking.

Iterate in a pythonic way

You shouldn't use range to iterate over a structure when python provide such a convenient way to iterate over it.

For instance, you could do :

surrounding = [(a - 1, b - 1),
               (a - 1, b    ),
               (a - 1, b + 1),
               (a    , b - 1),
               (a    , b + 1),
               (a + 1, b - 1),
               (a + 1, b    ),
               (a + 1, b + 1),]
for a,b in surrounding:
    try:
        if universe[a][b]:
            count += 1
    except IndexError: a


Same kind of argument applies to for a in range(0, 9): for b in range(0, 9): even though you might want to introduce enumerate.

Code Snippets

if universe[a][b] == 0 and countSurrounding(universe, a, b) == 3:
            nextUniverse[a][b] = 1
        elif universe[a][b] == 1 and countSurrounding(universe, a, b) not in (2, 3):
            nextUniverse[a][b] = 0
surr = countSurrounding(universe, a, b)
        if universe[a][b]:
            if surr not in (2, 3):
                nextUniverse[a][b] = 0
        else:
            if surr == 3:
                nextUniverse[a][b] = 1
surrounding = [(a - 1, b - 1),
               (a - 1, b    ),
               (a - 1, b + 1),
               (a    , b - 1),
               (a    , b + 1),
               (a + 1, b - 1),
               (a + 1, b    ),
               (a + 1, b + 1),]
for a,b in surrounding:
    try:
        if universe[a][b]:
            count += 1
    except IndexError: a

Context

StackExchange Code Review Q#25772, answer score: 4

Revisions (0)

No revisions yet.