Files
matrixmania/07_kloetzchen.py
T
2026-10-29 13:17:57 +01:00

41 lines
1.1 KiB
Python

# 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