-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.py
41 lines (35 loc) · 1.15 KB
/
maze.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from enum import Enum
from typing import NamedTuple
import random
from math import sqrt
class Cell(str, Enum):
EMPTY = " "
BLOCKED = "X"
START = "S"
GOAL = "G"
PATH = "*"
class MazeLocation(NamedTuple):
row: int
column: int
class Maze:
def __init__(self, rows = 10, columns = 10, sparseness = .2, start = MazeLocation(0,0), goal = MazeLocation(9,9)):
self._rows = rows
self._columns = columns
self.start = start
self.goal = goal
self._grid = [[Cell.EMPTY for c in range(columns)] for r in range(rows)]
self._randomly_fill(rows, columns, sparseness)
self._grid[start.row][start.column] = Cell.START
self._grid[goal.row][goal.column] = Cell.GOAL
def _randomly_fill(self, rows, columns, sparseness):
for row in range(rows):
for column in range(columns):
if random.uniform(0, 1.0) < sparseness:
self._grid[row][column] = Cell.BLOCKED
def __str__(self):
output = ""
for row in self._grid:
output += "".join([c.value for c in row]) + "\n"
return output
maze = Maze()
print(maze)