# Untitled

unknown

python

16 days ago

2.1 kB

4

Indexable

Never

^{}

def onBoard(i, j, image): ni = image.shape[0] # number of pixels (height) nj = image.shape[1] # number of pixels (width) if (i <= ni-1 and i >= 0) and (j <= nj-1 and j >= 0): # You need some more conditions here! # We've checked i, but what about j? return True else: return False #Finish this code def getNeighborValues(i,j, board): # The following list contains the indices of the neighbors for a pixel at (i.j) # But remember, we need to check if the neighbor is on the board when we try to get its value! neighborhood = [(i-1, j), (i, j-1), (i+1, j), (i, j+1)] neighbor_values = [] for neighbor in neighborhood: check = onBoard(neighbor[0], neighbor[1], board) if check == True: neighbor_values.append(board[neighbor]) else: neighbor_values.append(None) return neighbor_values def advance_board(board): ''' Advances the game board using the given rules. Input: the initial game board. Output: the advanced game board ''' # create a new array that's just like the original one, but initially set to all zeros (i.e., totally empty) new_board = np.zeros_like(board) # loop over each cell in the board and decide what to do. # You'll need two loops here, one nested inside the other. for i, ignore in enumerate(board[0]): for j, ignore in enumerate(board[1]): neighborhood = [(i-1, j), (i, j-1), (i+1, j), (i, j+1)] if board[i, j] == 1 and new_board[i, j] != 2: new_board[i, j] = 1 elif board[i,j] == 2: neighbor_array = getNeighborValues(i, j, board) new_board[i, j] = 0 for index, value in enumerate(neighbor_array): if value == 0.0: new_board[neighborhood[index][0],neighborhood[index][1]] = 0 elif value == 1.0: new_board[neighborhood[index][0],neighborhood[index][1]] = 2 return new_board

Leave a Comment