How to generate forest

Asked by At

[Question Answered] Need to remove it.

2 Answers

Jurgen Strydom On

Not to solve the entire problem for you and take away all the fun, but I believe the missing piece is np.where:

import numpy as np
forest = np.random.randint(0, 2, (10,10))
x, y = np.where(forest == 1)
print(x)  # up down
print(y)  # left right

This function outputs x,y coordinates in the matrix that you can use to perform tests on, and take actions with in adjacent cells (coordinates).

You can combine these coordinate pairs like so to iterate over them:

coord_pairs = np.vstack((x,y))
coord_pairs.shape  # (2, 48)
coord_pairs[0,:]  # x
coord_pairs[1,:]  # y

for k in range(coord_pairs.shape[1]):
    print(f"x:{coord_pairs[0,k]}, y:{coord_pairs[1,k]}")

The state management as explained in this other answer on your question will also be required.

Christoph Burschka On

You're implementing a Cellular Automaton, and to simulate this you will need to create a copy of the array for your work. The basic algorithm would work as follows:

new_state = state.copy()
for i in range(height):
    for j in range(width):
        new_state[i,j] = calculate_new_cell(state, i, j)

state = new_state

(The calculate_new_cell(state, i, j) function is dependent on the rules of the cellular automaton.)