I am using Python 3.6. I have had a quick look on google, and on stack overflow, but I am yet to see one with my problem. The problem is that my function that should find the number of live neighbours, is returning the wrong value (but only sometimes).
This is the code:
def neighbourCount(i, j):
neighbours = 0
try:
if grid[i-1][j-1] == "1":
neighbours += 1
except:
pass
try:
if grid[i-1][j] == "1":
neighbours += 1
except:
pass
try:
if grid[i-1][j+1] == "1":
neighbours +=1
except:
pass
try:
if grid[i][j-1] == "1":
neighbours += 1
except:
pass
try:
if grid[i][j+1] == "1":
neighbours += 1
except:
pass
try:
if grid[i+1][j-1] == "1":
neighbours += 1
except:
pass
try:
if grid[i+1][j] == "1":
neighbours += 1
except:
pass
try:
if grid[i+1][j+1] == "1":
neighbours +=1
except:
pass
return(neighbours)
grid = [
["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","0"],
["0","0","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","0","0","0","0","0","0","0","0"],
["0","0","0","0","0","0","0","0","0","0"]
]
newGrid = [
["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","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","0","0"]
]
game = 1
while game == 1:
for i in range(0,10):
print(*grid[i], sep=' ')
numNeigh = []
for i in range(0,10):
row = []
for j in range(0,10):
neighbours = neighbourCount(i, j)
if grid[i][j] == "1" and ( neighbours>1 and neighbours<4 ):
newGrid[i][j] = "1"
if grid[i][j] == "1" and ( neighbours<2 or neighbours>3 ):
newGrid[i][j] = "0"
if grid[i][j] == "0" and neighbours == 3:
newGrid[i][j] = "1"
row.append(neighbours)
numNeigh.append(row)
for i in numNeigh:
print(i)
for i in newGrid:
print(i)
print("\n\n")
grid = newGrid
input()
This is the output (first chunk: grid array)(second chunk: neighbours that each cell has)(third chunk: newGrid array):
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 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 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]
[1, 2, 3, 3, 2, 0, 0, 0, 0, 0]
[1, 2, 4, 2, 2, 0, 0, 0, 0, 0]
[1, 1, 2, 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, 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', '1', '1', '0', '0', '0', '0', '0', '0']
['0', '1', '0', '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', '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']
(Note for above: I also have a 2D array for "newGrid" that is the same as "grid" but with only "0")
The middle chunk shows the amount of neighbours each position has, but rather than showing "1, 1, 2, 1, 1, 0, 0...." on the third row down, it is showing "1,2,4,2,2,0,0..."
Your issue probably lies in the line where you create newGrid
. Unless you properly create a copy of grid
(deep copy), then the elements will be pointing to the same lists (shallow copy). So when you change newGrid
, grid
is being altered as well. To properly create a copy of grid, change your newGrid
line from:
newGrid = grid.copy()
to:
newGrid = [x[:] for x in grid]
copy
method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.