From 2bc184528ff62f2ff6555fd45a3c3a2b041f367b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bertrand=20N=C3=A9ron?= <bneron@pasteur.fr> Date: Mon, 9 Sep 2019 17:18:12 +0200 Subject: [PATCH] add file to create labyrinth (from yoann code) --- Graph/graph/maze.py | 58 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Graph/graph/maze.py diff --git a/Graph/graph/maze.py b/Graph/graph/maze.py new file mode 100644 index 0000000..ba0f60a --- /dev/null +++ b/Graph/graph/maze.py @@ -0,0 +1,58 @@ +import random + +class Maze: + + def __init__(self, nb_rows, nb_cols): + self.nb_rows = nb_rows + self.nb_cols = nb_cols + + self.maze = [["."]*nb_cols for _ in range(nb_rows)] + + + """ Fill randomly the maze regarding the wall_proba + @param wall_proba Independent probability for each tile to be a wall + @param seed Random seed for reproducibility + """ + def fill_random(self, wall_proba=0.5, seed=None): + if seed: + random.seed(seed) + + for row in self.maze: + for idx in range(self.nb_cols): + row[idx] = "#" if random.random() < wall_proba else "." + + + """ Save a maze to a file """ + def save(self, filename): + with open(filename, "w") as out: + out.write(f"{self.nb_rows} {self.nb_cols}") + for row in self.maze: + out.write("".join(row) + "\n") + + + """ Load a maze from a file """ + def load(self, filename): + with open(filename) as input: + # Parse header + header = input.readline() + header = [int(x) for x in header.strip().split(" ")] + # Construct maze properties + self.nb_rows, self.nb_cols = header + self.maze = [["."] * self.nb_cols for _ in range(self.nb_rows)] + # Fill the maze + for row_idx in range(self.nb_rows): + line = input.readline() + for col_idx in range(self.nb_cols): + self.maze[row_idx][col_idx] = line[col_idx] + + + def __repr__(self): + rows_str = [] + for row in self.maze: + rows_str.append("".join(row)) + return "\n".join(rows_str) + + +if __name__ == "__main__": + m = Maze(10, 10) + print(m) -- GitLab