47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
def getNode(name, l):
|
|
return next((i for i in l if i.name == name), -1)
|
|
|
|
|
|
def print_grid_direct(self, path=None):
|
|
"""
|
|
Gibt das Grid in der Konsole aus, direkt wie es im Array gespeichert ist.
|
|
Die y-Achse nimmt nach unten zu (0 ist oben), entsprechend der Array-Struktur.
|
|
Optionaler Parameter 'path' ist eine Liste von (x,y)-Koordinaten, die den Pfad darstellen.
|
|
"""
|
|
# Symbole für verschiedene Zustände
|
|
symbols = {
|
|
"free": ".",
|
|
"obstacale": "#", # Tippfehler aus Original-Code beibehalten
|
|
"start": "S",
|
|
"target": "G",
|
|
"path": "o"
|
|
}
|
|
|
|
# Ausgabe des Grids mit Koordinatenachsen
|
|
print("\n ", end="")
|
|
# Obere x-Achsen-Beschriftung
|
|
for x in range(self.cols):
|
|
print(f"{x:2d}", end=" ")
|
|
print("\n " + "-" * (self.cols * 3))
|
|
|
|
# Grid mit y-Achsen-Beschriftung
|
|
for y in range(self.rows):
|
|
print(f"{y:2d} |", end=" ")
|
|
for x in range(self.cols):
|
|
field = self.grid[x][y]
|
|
if path and (x, y) in path and field.state != "start" and field.state != "target":
|
|
print(f"{symbols['path']:2s}", end=" ")
|
|
else:
|
|
print(f"{symbols[field.state]:2s}", end=" ")
|
|
print() # Zeilenumbruch
|
|
|
|
print("\nLegende:")
|
|
print(" . = frei")
|
|
print(" # = Hindernis")
|
|
print(" S = Start")
|
|
print(" G = Ziel")
|
|
print(" o = Pfad")
|
|
|
|
# Zusätzliche Statistiken, falls ein Pfad vorhanden ist
|
|
if path:
|
|
print(f"Pfadlänge: {len(path)} Felder") |