Generate paths on n*n grid

algorithm, multidimensional-array

Solution

Obviously, you're trying to generate "black path" shapes on a write grid.

So let's just do it.

- Start with a white grid.

- Randomly position some turtles on it.

- Then, while your grid doesn't meet a proper white/black cell ratio, do the following

- Move each turtle one cell in a random direction and paint it black unless doing so break the "no more than three black neighbors" rule.

Problem

I have `n*n` grid, where for example `n=10`. I have to fill it with black and white elements. Every black element has to have one, two or three black neighbors. It is not allowed to contain black elements with four or zero neighbors. How should I build this kind of grid ? Edit: To be more specific, it is two-dimensional array built for example with two `for` loops: ``` n = 10 array = [][]; for ( x = 0; x < n; x++ ) { for ( y = 0; y < n; y++ ) { array[x][y] = rand(black/white) } } ``` This pseudo code builds somethind like: And what I expect is:

Original source