Home > Blockchain >  random generation of levels using 2D arrays Python
random generation of levels using 2D arrays Python

Time:12-17

I want to do random generation of levels using 2D arrays. 0 = emptiness, 1 = wall, how generate levels which have a passable route from a starting point to a finish(yellow circle, appears in a random free spot on the map) location?

pole = [[0] * 20 for i in range(20)]
for i in range(20):
    for j in range(20):
        pole[i][j] = random.randint(0, 1)

At the moment it just randomizes the ones and zeros and it comes out: enter image description here

CodePudding user response:

The best way to do this is to generate a random map just like you do now and them check with an algorithm if there is an open route between start and finish. If not, just generate a new map and check again.

A "flood fill" algorithm where you start filling from start and see if the fill reaches the finish spot could work. For algorithm help, see:

Need help implementing flood-fill algorithm

  • Related