0
\$\begingroup\$

For learning purpose I've started creating my implementation of Conway's Game of Life. I've used numpy to store big array, contating dead and alive cells, then I've apllied Conway's rules, to create mechanics of cells life. To manage grid, and graphics I used pygame module. After many reviews, and rewriting code in many ways, I can't find out what's wrong with it, so I decided to ask you. For example I've tried to make a glider, (as code shows), but he dies after 3 loop cycles. I'd be appreciate for help and tips. Can you help me find out why the cells aren't reproducing? Thanks in advance. Code:

import pygame
import numpy as np

BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
N = 195
WIDTH = 10
HEIGHT = 10

grid = np.zeros(shape=(N, N), dtype=np.int32)
glider = np.array([[0, 0, 1],
                       [1, 0, 1],
                       [0, 1, 1]])
grid[3:6, 3:6] = glider

pygame.init()

WINDOW_SIZE = [980, 980]
screen = pygame.display.set_mode(WINDOW_SIZE)

pygame.display.set_caption("GAME OF LIFE")

done = False

clock = pygame.time.Clock()

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    for row in range(N):
        for column in range(N):
            color = BLACK
            if grid[row][column] == 1:
                color = GREEN
            pygame.draw.rect(screen, color,
                             [WIDTH * column,
                              HEIGHT * row,
                              WIDTH,
                              HEIGHT])

    newGrid = grid.copy()
    for i in range(N):
        for j in range(N):
            total = grid[(i-1) % N:(i+1) % N, (j-1) % N:(j+1) % N].sum() - grid[i, j]
            if grid[i, j] == 1:
                if(total < 2) or (total > 3):
                    newGrid[i, j] = 0
                else:
                    if total == 3:
                        newGrid[i, j] = 1
    grid = newGrid

    clock.tick(60)
    pygame.display.flip()

pygame.quit()
\$\endgroup\$

closed as off-topic by πάντα ῥεῖ, Sᴀᴍ Onᴇᴌᴀ, Mast, Donald.McLean, Peilonrayz Feb 7 at 6:13

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – πάντα ῥεῖ, Sᴀᴍ Onᴇᴌᴀ, Mast, Donald.McLean, Peilonrayz
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Welcome to Code Review! Unfortunately this post is off-topic for this site. Please read What topics can I ask about here? - note that it states "If you are looking for feedback on a specific working piece of code...then you are in the right place!" Also, when posting your question, there should have been text on the side that read "Your question must contain code that is already working correctly..." When you have fixed the code, please edit your post to include the working code and it can be reviewed." \$\endgroup\$ – Sᴀᴍ Onᴇᴌᴀ Feb 6 at 22:50
  • \$\begingroup\$ Note: cross posted on SO \$\endgroup\$ – Sᴀᴍ Onᴇᴌᴀ Feb 6 at 23:04
0
\$\begingroup\$

I think you have a couple of subtle bugs in the way you implement Conway's rules. See my comments in the code for details:

for i in range(N):
    for j in range(N):
        # I changed the range of the extent of the sum, note +2 rather than +1 !
        total = grid[(i-1) % N:(i+2) % N, (j-1) % N:(j+2) % N].sum() - grid[i, j]
        if grid[i, j] == 1:
            if(total < 2) or (total > 3):
                newGrid[i, j] = 0
            # allow for survival in case of total = 2 or 3
            else:
                newGrid[i, j] = 1
        # allow for reproduction if cell is empty
        else:
            if total == 3:
                newGrid[i, j] = 1

With these edits the glider should glide :)

\$\endgroup\$
  • 4
    \$\begingroup\$ Welcome to Code Review! Please note, questions involving code that's not working as intended, are off-topic on this site. If OP edits their post to address the problem and make their post on-topic, they make your answer moot. It is advised not to answer such questions. Protip: There are tons of on-topic questions to answer; you'll make more reputation faster if you review code in questions that will get higher view counts for being on-topic. \$\endgroup\$ – Sᴀᴍ Onᴇᴌᴀ Feb 6 at 22:51
  • \$\begingroup\$ Thank you very much, really the first one only had influence on my program, now I know my mistake. Have a nice day! \$\endgroup\$ – dannyxn Feb 6 at 22:53
  • 1
    \$\begingroup\$ Sure @dannyxn! maybe consider deleting this question here as it looks off-topic. My answer is on StackOverflow anyway (I can't delete my answer as you have accepted it :) \$\endgroup\$ – Davide Fiocco Feb 6 at 23:07
  • 1
    \$\begingroup\$ Thank you for your understanding. Feel free to stick around for the plenty of on-topic questions we have that could still use an answer :-) \$\endgroup\$ – Mast Feb 7 at 2:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.