[2] add main.py and mazes.txt
This commit is contained in:
parent
8b568c2b8c
commit
4a1fdb4388
102
meosyam/docs/data/2/main.py
Normal file
102
meosyam/docs/data/2/main.py
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
import sys
|
||||
|
||||
class Cell:
|
||||
def __init__(self, x, y, is_wall=False, is_start=False, is_exit=False):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.is_wall = is_wall
|
||||
self.is_start = is_start
|
||||
self.is_exit = is_exit
|
||||
|
||||
def is_passable(self):
|
||||
return not self.is_wall
|
||||
|
||||
|
||||
class Maze:
|
||||
def __init__(self, width, height, cells, start=None, exit=None):
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.cells = cells
|
||||
self.start = start
|
||||
self.exit = exit
|
||||
|
||||
def get_cell(self, x, y):
|
||||
if 0 <= x < self.width and 0 <= y < self.height:
|
||||
return self.cells[y][x]
|
||||
return None
|
||||
|
||||
def get_neighbors(self, cell):
|
||||
neighbors = []
|
||||
for dx, dy in ((0, -1), (0, 1), (-1, 0), (1, 0)):
|
||||
nx, ny = cell.x + dx, cell.y + dy
|
||||
neighbor = self.get_cell(nx, ny)
|
||||
if neighbor and neighbor.is_passable():
|
||||
neighbors.append(neighbor)
|
||||
return neighbors
|
||||
|
||||
|
||||
class MazeBuilder:
|
||||
@staticmethod
|
||||
def build_from_file(filename):
|
||||
with open(filename, 'r') as f:
|
||||
lines = [line.rstrip('\n') for line in f]
|
||||
if not lines:
|
||||
raise ValueError("Empty file")
|
||||
height = len(lines)
|
||||
width = max(len(line) for line in lines)
|
||||
cells = []
|
||||
start = None
|
||||
exit_cell = None
|
||||
for y, line in enumerate(lines):
|
||||
row = []
|
||||
for x in range(width):
|
||||
ch = line[x] if x < len(line) else ' '
|
||||
is_wall = (ch == '#')
|
||||
is_start = (ch == 'S')
|
||||
is_exit = (ch == 'E')
|
||||
if is_start:
|
||||
start = Cell(x, y, False, True, False)
|
||||
row.append(start)
|
||||
elif is_exit:
|
||||
exit_cell = Cell(x, y, False, False, True)
|
||||
row.append(exit_cell)
|
||||
else:
|
||||
row.append(Cell(x, y, is_wall, False, False))
|
||||
cells.append(row)
|
||||
if start is None:
|
||||
raise ValueError("No start cell (S) found")
|
||||
if exit_cell is None:
|
||||
raise ValueError("No exit cell (E) found")
|
||||
return Maze(width, height, cells, start, exit_cell)
|
||||
|
||||
|
||||
def print_maze(maze):
|
||||
for y in range(maze.height):
|
||||
row = []
|
||||
for x in range(maze.width):
|
||||
cell = maze.get_cell(x, y)
|
||||
if cell.is_start:
|
||||
row.append('S')
|
||||
elif cell.is_exit:
|
||||
row.append('E')
|
||||
elif cell.is_wall:
|
||||
row.append('#')
|
||||
else:
|
||||
row.append(' ')
|
||||
print(''.join(row))
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) > 1:
|
||||
filename = sys.argv[1]
|
||||
else:
|
||||
filename = 'maze1.txt'
|
||||
try:
|
||||
maze = MazeBuilder.build_from_file(filename)
|
||||
print(f"Maze loaded ({maze.width}x{maze.height})")
|
||||
print_maze(maze)
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
10
meosyam/docs/data/2/maze1.txt
Normal file
10
meosyam/docs/data/2/maze1.txt
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
##########
|
||||
#S #
|
||||
# #### #
|
||||
# # #
|
||||
# # # #
|
||||
# # # #
|
||||
# # # #
|
||||
# # # #
|
||||
# # E#
|
||||
##########
|
||||
10
meosyam/docs/data/2/maze10x10.txt
Normal file
10
meosyam/docs/data/2/maze10x10.txt
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
##########
|
||||
#S # # #
|
||||
# # # #
|
||||
# ## ## ##
|
||||
# # #
|
||||
# # # # #
|
||||
# # # # #
|
||||
# # #
|
||||
## ## ##E
|
||||
##########
|
||||
20
meosyam/docs/data/2/maze20x20.txt
Normal file
20
meosyam/docs/data/2/maze20x20.txt
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
####################
|
||||
#S # # # # #
|
||||
# # # # # # # # ###
|
||||
# # # # # # # # #
|
||||
# # # # # # # # ###
|
||||
# # # # # # # # #
|
||||
# # # # # # # # ###
|
||||
# # # # # # # # #
|
||||
# # # # # # # # ###
|
||||
# # # # # # # # #
|
||||
# # # # # # # # ###
|
||||
# # # # # # # # #
|
||||
# # # # # # # # ###
|
||||
# # # # # # # # #
|
||||
# # # # # # # # ###
|
||||
# # # # # # # # #
|
||||
# # # # # # # # ###
|
||||
# # # # # # # # #
|
||||
# # # # # # # # E#
|
||||
####################
|
||||
10
meosyam/docs/data/2/maze_empty.txt
Normal file
10
meosyam/docs/data/2/maze_empty.txt
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
##########
|
||||
#S #
|
||||
# #
|
||||
# #
|
||||
# #
|
||||
# #
|
||||
# #
|
||||
# #
|
||||
# E#
|
||||
##########
|
||||
Loading…
Reference in New Issue
Block a user