Help with Python - Conway's Game of Life

How to check neighbours in matrix

Page 1 of 1

4 Replies - 4098 Views - Last Post: 29 April 2010 - 12:45 PM Rate Topic: -----

#1 Guest_Helix*


Reputation:

Help with Python - Conway's Game of Life

Posted 29 April 2010 - 09:44 AM

Hello!
I am a beginner in python programming, and I would be very thankful for some help with this problem. Most likely, it is something very simple I am doing wrong.

I have been given the task of programming a version of Conway's Game of Life. Most of it seems to be fairly straightforward, but I can't seem to get the neighbour count correct.

All cells in my grid have a value of 1 or 0. What I want to do is count how many 1's are around each cell, and then set a new value for the middle cell according to this total (1 stays 1 if surrounded by 2 or 3, 0 becomes 1 if surrounded by 3, any other value sets it to 0). I am probably making a very basic mistake here, but I have been staring myself blind at the code. Here is the entire method for calculating new values for each cell:


def generation(cell):
    cell_new = cell
    p = win.getMouse()
    for x in range(30):
        for y in range(30):
            neighbour_count = 0
            for n in (x-1, x, x+1):
                for m in (y-1, y, y+1):
                    try:
                        if m == 1:
                            neighbour_count = neighbour_count + 1
                    except IndexError, e:
                        pass
            neighbour_count = neighbour_count - cell[x][y] #Takes the middle cell out of the calculation
            if neighbour_count == 3:
                cell_new[x][y] = 1
            elif neighbour_count == 2 and cell[x][y] == 1:
                cell_new[x][y] = 1
            elif neighbour_count != 2 and neighbour_count != 3:
                cell_new[x][y] = 0
    return cell_new




Is This A Good Question/Topic? 0

Replies To: Help with Python - Conway's Game of Life

#2 Guest_Helix*


Reputation:

Re: Help with Python - Conway's Game of Life

Posted 29 April 2010 - 10:59 AM

if m == 1:

is of course

if cell[n][m] == 1:



Still does not work as intended.
Was This Post Helpful? 0

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Help with Python - Conway's Game of Life

Posted 29 April 2010 - 12:09 PM

You're assuming that "cell_new = cell " is making a copy. It's not. It's just pointing.

e.g.
>>> a = [1,2,3]
>>> b = a
>>> a[0]=9
>>> b
[9, 2, 3]
>>>



You want to make a copy into cell_new. Actually, considering you're just filling in, don't even bother copying. Just grow it:

e.g.
cell_new = []
for x in range(30):
	cell_new.append([])
	for y in range(30):
		cell_new[x].append(0)



Also, you count-1 for the middle cell is a fallacy. What if the middle cell is empty?

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Helix*


Reputation:

Re: Help with Python - Conway's Game of Life

Posted 29 April 2010 - 12:25 PM

Thank you, thank you! I knew it had to be something like that. You just saved me. =D

The removing of the middle cell from the calculation is on purpose. Either the cell is zero, and does not need to be subtracted, or it is one, and does. Either way, the calculation will be correct. If it is empty, that would be a problem, but further up my code I generate the entire grid out of zeros, and then fill in random 1's. So the middle cell it will never be empty as long as the program functions correctly (cells outside the grid, however, will be. But they are never in the middle of a test like this).

Thank you again. This was most helpful.
Was This Post Helpful? 0

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Help with Python - Conway's Game of Life

Posted 29 April 2010 - 12:45 PM

Ah, I see, you're taking the value of the cell out. That will work. Sorry, my bad.

Don't be afraid of functions. I'd probably implement something like "def getNeighborCount(cell, x, y)" just to make the loop look simple.

Good luck.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1