X-Git-Url: https://www.fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=maze.py;h=d5662f0500e24dad299210a69efed402e3db2339;hb=21ed4aa91d0f1ac87ec684d8808e5ced552ad457;hp=8ac9fcef0495fbefc6c11f1652367fb44d916f40;hpb=8ea809c43242d3a2e063692105919a86c3f6fe6b;p=picoclvr.git diff --git a/maze.py b/maze.py index 8ac9fce..d5662f0 100755 --- a/maze.py +++ b/maze.py @@ -15,11 +15,11 @@ v_empty, v_wall, v_start, v_goal, v_path = 0, 1, 2, 3, 4 def create_maze(h=11, w=17, nb_walls=8): assert h % 2 == 1 and w % 2 == 1 - a, k = 0, 0 + nb_attempts, nb_added_walls = 0, 0 - while k < nb_walls: + while nb_added_walls < nb_walls: while True: - if a == 0: + if nb_attempts == 0: m = torch.zeros(h, w, dtype=torch.int64) m[0, :] = 1 m[-1, :] = 1 @@ -29,6 +29,7 @@ def create_maze(h=11, w=17, nb_walls=8): r = torch.rand(4) if r[0] <= 0.5: + # Add a vertical wall i1, i2, j = ( int((r[1] * h).item()), int((r[2] * h).item()), @@ -36,10 +37,14 @@ def create_maze(h=11, w=17, nb_walls=8): ) i1, i2, j = i1 - i1 % 2, i2 - i2 % 2, j - j % 2 i1, i2 = min(i1, i2), max(i1, i2) + + # If this wall does not hit another one, add it if i2 - i1 > 1 and i2 - i1 <= h / 2 and m[i1 : i2 + 1, j].sum() <= 1: m[i1 : i2 + 1, j] = 1 break + else: + # Add an horizontal wall i, j1, j2 = ( int((r[1] * h).item()), int((r[2] * w).item()), @@ -47,15 +52,18 @@ def create_maze(h=11, w=17, nb_walls=8): ) i, j1, j2 = i - i % 2, j1 - j1 % 2, j2 - j2 % 2 j1, j2 = min(j1, j2), max(j1, j2) + + # If this wall does not hit another one, add it if j2 - j1 > 1 and j2 - j1 <= w / 2 and m[i, j1 : j2 + 1].sum() <= 1: m[i, j1 : j2 + 1] = 1 break - a += 1 - if a > 10 * nb_walls: - a, k = 0, 0 + nb_attempts += 1 + + if nb_attempts > 10 * nb_walls: + nb_attempts, nb_added_walls = 0, 0 - k += 1 + nb_added_walls += 1 return m