Problem/Question: the grid is not getting advanced is it getting printed twice, when I only want it to be printed once. Also, this is homework and it is due ASAP, although late work is accepted for less credit in my class.
I am making Conway's game of life with these six functions:
Function 1: create a blank grid input: nothing return: a blank grid
Function 2: print a given grid input: a grid return: nothing
Function 3: load a pattern input: a file name, a grid return: nothing
Function 4: advance a grid one generation input: a grid return: a new grid advanced by one generation
Function 5: advance a cell one generation input: a row, a column, a grid return: whether the cell is alive or not (True or False)
Function 6: determine the number of living neighbors of a cell input: a row, a column, a grid return: the number of living neighbors of the cell
So far, I have made 1, 2, 3, 5, and 6, and have tried function 4. When I try to use it however I don't get an error, but actually, something that I believe is worse: simply the wrong output.
Here is a condensed version of my code so far:
living_cell = "O"
dead_cell = "-"
def create_blank_grid():
line = []
for i in range(60):
line.append(dead_cell)
grid = []
for j in range(30):
grid.append(line[:])
return grid
# print(create_blank_grid())
def print_grid(grid):
for i in grid:
for j in i:
print(j, end="")
print()
return grid
# print_grid(create_blank_grid())
def load_design(file_name, grid):
myFile = open(file_name, "r")
coordinates = []
for line in myFile:
real_line = line.split()
row, col = real_line[0], real_line[1]
coordinates.append((row, col))
myFile.close()
# turn on certain cells here
for row, col in coordinates:
row, col = int(row), int(col)
# print(row, col)
grid[row][col] = living_cell
# print_grid(grid)
return print_grid(grid)
# load_design("square.in", create_blank_grid())
# square just has coordinates in it like "12 35" on each line
def num_living_neighbors(row, col, grid):
# just to make sure
row = int(row)
col = int(col)
living_neighbors_count = 0
if (col + 1) < len(grid[row]) and grid[row][col + 1] == living_cell:
living_neighbors_count += 1
if (col - 1) >= 0 and grid[row][col - 1] == living_cell:
living_neighbors_count += 1
if (row + 1) < len(grid) and grid[row + 1][col] == living_cell:
living_neighbors_count += 1
if (row - 1) >= 0 and grid[row - 1][col] == living_cell:
living_neighbors_count += 1
if (((col + 1) < len(grid[row])) and (row + 1) < len(grid)) and grid[row + 1][col + 1] == living_cell:
living_neighbors_count += 1
if (((row + 1) < len(grid)) and (col - 1) >= 0) and grid[row + 1][col - 1] == living_cell:
living_neighbors_count += 1
if (((row - 1) >= 0) and (col + 1) < len(grid[row])) and grid[row - 1][col + 1] == living_cell:
living_neighbors_count += 1
if (((row - 1) >= 0) and (col - 1) >= 0) and grid[row - 1][col - 1] == living_cell:
living_neighbors_count += 1
return living_neighbors_count
def adv_cell_one_gen(row, col, grid):
# is alive, less than 2 alive neighbors
if grid[row][col] == living_cell and num_living_neighbors(row, col, grid) < 2:
return False
# is alive, 2 or 3 alive neighbors
if grid[row][col] == living_cell and (num_living_neighbors(row, col, grid) == 2 or num_living_neighbors(row, col, grid) == 3)
return True
# is alive, more than 4 alive neighbors
if grid[row][col] == living_cell and num_living_neighbors(row, col, grid) > 3:
return False
# is dead, has 3 alive neighbors
if grid[row][col] == dead_cell and num_living_neighbors(row, col, grid) == 3:
return True
def adv_grid_one_gen(grid):
for i in range(len(grid)):
for j in range(i):
adv_cell_one_gen(i, j, grid)
return print_grid(grid)
adv_grid_one_gen(load_design("f-pentomino.in", create_blank_grid()))
My question is, when I called adv_grid_one_gen in te last line, I get an out put where 2 mistakes are made: 1. the printed grid is not advanced at all, and 2. the console shows that it got printed twice.
So overall, a grid that is not advanced is getting printed twice, when I only want it to be printed once
If anyone could offer any help, or just comment something I am doing wrong, I would appreciate it fully, but my main question is above. Thanks a lot! I really appreciate it!