I am stuck with a problem that I haven't been successful in finding the solution through other people's post. We are following standard rules for Conway's Game of Life.
So far I have been able to create the 2d array as well as populate the array with a random character of '-' for dead and 'O' for alive (The letter O not the number zero). I believe that I am going through and checking all of the neighbors correctly. * The reason I used try/catch is because it was the only way I could figure out how to avoid getting an out of bounds error message.
My main issue is that I'm not sure if I am going through the array correctly when checking/changing values. Also I am not sure how to go about printing the "updated" 2d array after applying the live/die rules to the entire array.
As of right now, the output prints the randomized array once, then prints the exact same array again, then asks if i want to continue ( Go through the while loop again). If I choose 'y', I get the error message
TypeError: list indices must be integers, not list
I'm beyond lost, any ideas will help as I am about 3 weeks into python.
Here is my code:
import random
numrows = 25
numcols = 25
def rnd():
rn = random.randint(0,1)
return rn
def initial():
grid = []
count = 0
for x in range(numrows):
grid.append([])
for y in range(numcols):
rand=random.randrange(1,3)
if(rand == 1):
grid[x].append('O')
else:
grid[x].append('-')
for x in grid:
print(*x, sep=' ',end="\n") #prints initial grid with random values
print("")# space for clarity sake
print("")# " "
answer = 'y'
count = 0
while(answer == 'y'):
#This is where I am checking each neighbor. Adding 1 to count for each
#live neighbor.
for r in range(0,numrows):
for c in range(0,numcols):
try:
if(grid[r-1][c-1] == 'O'):
count += 1
except:
pass
try:
if(grid[r-1][c] == 'O'):
count += 1
except:
pass
try:
if(grid[r-1][c+1] == 'O'):
count += 1
except:
pass
try:
if(grid[r][c-1] == 'O'):
count += 1
except:
pass
try:
if(grid[r][c+1] == 'O'):
count += 1
except:
pass
try:
if(grid[r+1][c-1] == 'O'):
count += 1
except:
pass
try:
if(grid[r+1][c] == 'O'):
count += 1
except:
pass
try:
if(grid[r+1][c+1] == 'O'):
count += 1
except:
pass
#this is where I am applying the rules of living or dying
#based on how many neighbors there are.
if(grid[r][c] == '-'):
if(count == 3):
grid[r][c].append('O')
if(grid[r][c] == 'O'):
if(count < 2):
grid[r][c].append('-')
if(grid[r][c] == 'O'):
if(count == 2 or count == 3):
grid[r][c].append('O')
if(grid[r][c] == 'O'):
if(count > 3):
grid[r][c].append('-')
for r in grid: #This is a problem area as i dont
print(*r, sep=' ',end="\n")#know how to print the grid
answer = input("Continue? y or n( lower case only): ")
if(answer != 'y'):
print(" Hope you had a great life! Goodbye!")
Here is the problem: the crash occurs at the if line:
if(grid[r][c] == '-'): # <=== here on this line
if(count == 3):
grid[r][c].append('O')
On that line, r
is no longer an integer, but it is a list. But, why? The answer occurs a few lines later:
for r in grid: #This is a problem area as i dont
print(*r, sep=' ',end="\n")#know how to print the grid
In this block, r was an int, but now you turned it into a list. I acknowledge your comment there, on not knowing how to print the grid, so may I suggest a quickie:
print('\n'.join(' '.join(str(cell) for cell in row) for row in grid))
This way, you are not overwriting the variable r
.