Ich bin ziemlich neu zu Python und versuche, einen Labyrinthlöser zu bauen. Ich habe bereits einen Algorithmus erstellt, der eine einzelne Route navigieren und deren Route drucken kann (N/E/S/W), aber ich möchte diese auf mehrere Routen verschieben. Welche Art von Architektur sollte ich verwenden, um mehrere Routen zu verwalten?Python Maze Solving - Welche Architektur zu verwenden?
Ich dachte daran, Klassen und Unterklassen zu verwenden, um die verschiedenen Routen zu modellieren, so dass Unterklassen ihre "Stubs" erben, aber jetzt frage ich mich, ob dies ein wenig zu kompliziert sein könnte. Ich habe Beiträge über Rekursion gesehen, aber ich habe keine Ahnung, wo ich mit so etwas anfangen soll.
Hat jemand irgendwelche Vorschläge? Mein Code ist unten für einzelne Route, wenn jemand interessiert ist:
# =============================================================================
# MAZE
# =============================================================================
# Edit this grid to change the maze. Codes are:
# 0 = Walkable
# 1 = Wall
# 2 = Start
# 3 = Finish
maze = [[1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
# =============================================================================
# CLASSES AND METHODS
# =============================================================================
# Class which will hold all the routes
class Route():
used = []
def __init__(self):
self.directions = []
self.tracks = []
self.status = 'Pending'
# Class which stores position of 'marker' and 'start' in maze
class Pos():
def __init__(self, x, y):
self.x = x
self.y = y
self.coords = [] # Coordinates updated UPDATE_POS method
self.N = []
self.E = []
self.S = []
self.W = []
self.update_pos(x, y)
# When called, it updates values manually
def update_pos(self, x, y):
self.coords = [x, y] # Coordinates updated from x and y
self.N = [x, y-1, 'N'] # Directions define adjacents
self.E = [x+1, y, 'E']
self.S = [x, y+1, 'S']
self.W = [x-1, y, 'W']
# Find start of maze and set position
def find_start(self, maze):
for row in maze:
if 2 not in row:
continue
if 2 in row:
self.y = maze.index(row)
self.x = maze[maze.index(row)].index(2)
self.update_pos(self.x, self.y)
# Check if the index is within the maze
def valid_space(self, maze, index): # index in the form [x,y]
size = len(maze)
if index[0] < size and index[1] < size:
return True
else:
return False
# Looks for walkable spaces and moves the position to that space
def look_around(self, maze):
directions = [self.N, self.E, self.S, self.W]
for direction in directions:
# Eliminate invalid moves, used or outside maze
if self.valid_space(maze, direction):
if direction[0:2] in Route.used:
continue
# Look for walkable space
if maze[direction[1]][direction[0]] == 0:
self.x = direction[0]
self.y = direction[1]
self.update_pos(self.x, self.y)
route1.directions.append(direction[2])
Route.used.append(self.coords)
break
# Look for finish
if maze[direction[1]][direction[0]] == 3:
self.x = direction[0]
self.y = direction[1]
self.update_pos(self.x, self.y)
route1.directions.append(direction[2])
Route.used.append(self.coords)
route1.status = 'Finished'
else:
continue
# =============================================================================
# MAIN
# =============================================================================
# Initialise position and route
position = Pos(0, 0)
start_pos = Pos(0, 0)
route1 = Route()
# Change position to start of maze and print
start_pos.find_start(maze)
position.find_start(maze) # Set position to start
print('Solving...')
while route1.status != 'Finished':
position.look_around(maze)
print('''The directions to the finish are: ''')
print(route1.directions)
Willkommen bei StackOverflow. Bitte lesen und befolgen Sie die Buchungsrichtlinien in der Hilfe. [zum Thema] (http://stackoverflow.com/help/on-topic) und [how to ask] (http://stackoverflow.com/help/how-to-ask) gilt hier. StackOverflow ist kein Design-, Codierungs-, Recherche- oder Lernprogramm. – Prune
["Kann mir jemand helfen?" ist keine gültige SO Frage] (https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question). Dies bedeutet in der Regel, dass Sie eine halbe Stunde mit einem lokalen Tutor oder einem Lernprogramm statt Stack Overflow benötigen. – Prune
@Carcigenicate: Danke. Überraschend, dass niemand (einschließlich mir) dies vorher bemerkt hat. – Prune