added
This commit is contained in:
1 parent
b17db7bc39
commit
d327f5d7e6
7 files changed
+132
No files matched your search
@@ -0,0 +1,41 @@
|
||||
# Ausgangs- und Zielzustand
|
||||
state = {'A': 'B', 'B': '', 'C': ''}
|
||||
goal = {'A': 'C', 'B': 'A', 'C': ''}
|
||||
|
||||
|
||||
def is_clear(state, block):
|
||||
"""Prüft, ob ein Block frei ist (nichts liegt darauf)."""
|
||||
return all(v != block for v in state.values())
|
||||
|
||||
|
||||
def move(block, target):
|
||||
"""Gibt eine Action-Funktion zurück (Closure)."""
|
||||
|
||||
def action(current_state):
|
||||
if not is_clear(current_state, block):
|
||||
raise ValueError(f"Auf Block {block} liegt etwas!")
|
||||
if target != '' and not is_clear(current_state, target):
|
||||
raise ValueError(f"Block {target} ist nicht frei!")
|
||||
|
||||
new_state = current_state.copy()
|
||||
new_state[block] = target
|
||||
print(f"Moved {block} to {target}.")
|
||||
return new_state
|
||||
|
||||
return action
|
||||
|
||||
|
||||
def chain(f, g):
|
||||
"""Funktionale Komposition: Erst f, dann g ausführen."""
|
||||
return lambda current_state: g(f(current_state))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Erstellen des Ausführungsplans
|
||||
plan = chain(move('A', 'C'), move('B', 'A'))
|
||||
|
||||
# Ausführen des Plans auf dem Ausgangszustand
|
||||
final = plan(state)
|
||||
|
||||
print("Final state:", final)
|
||||
assert final == goal
|
||||
Reference in new issue
Block a user