-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaze.py
More file actions
48 lines (37 loc) · 1.13 KB
/
maze.py
File metadata and controls
48 lines (37 loc) · 1.13 KB
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
42
43
44
45
46
47
48
import random
from dataclasses import dataclass
from enum import Enum
@dataclass
class MazeLocation:
row: int
column: int
class Cell(str, Enum):
EMPTY = ' '
BLOCKED = 'X'
START = 'S'
GOAL = 'G'
PATH = '*'
class Maze:
def __init__(self, cols=10, rows=10, start=MazeLocation(0, 0), goal=MazeLocation(9, 9),
sparseness=0.2):
self._cols = cols
self._rows = rows
self._start = start
self._goal = goal
self._sparseness = sparseness
self._maze = [[Cell.EMPTY for _ in range(self._rows)] for _ in range(self._cols)]
self.fill_randomly()
self._maze[start.row][start.column] = Cell.START
self._maze[goal.row][goal.column] = Cell.GOAL
def fill_randomly(self):
for r in range(self._rows):
for c in range(self._cols):
if random.uniform(0, 1.0) <= 0.2:
self._maze[r][c] = Cell.BLOCKED
def __repr__(self):
output = ''
for row in self._maze:
output = '\n'.join([output, (' '.join(c.value for c in row))])
return output
maze = Maze()
print(maze)