1

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

Ilya
  • 254
  • 2
  • 11
  • 1
    The requirement "how to make emptiness (0) not completely covered by walls (1)" is not clear. I am guessing you want to generate levels which have a passable route from a starting point to a finish location? – vaizki Dec 17 '21 at 11:56
  • @vaizki Yes, that's what I meant – Ilya Dec 17 '21 at 11:58
  • @MichaelSzczesny no, I don't need a labyrinth, I want a lot of ways connected to each other – Ilya Dec 17 '21 at 12:06
  • @Ilya - *I want a lot of ways connected to each other*: That sounds a lot like a maze. – Michael Szczesny Dec 17 '21 at 12:12

1 Answers1

1

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

vaizki
  • 1,678
  • 1
  • 9
  • 12