diff --git a/funcs/04_k-laenge.py b/funcs/04_k-laenge.py index e69de29..4c6adb3 100644 --- a/funcs/04_k-laenge.py +++ b/funcs/04_k-laenge.py @@ -0,0 +1,37 @@ + + +def k_length(words): + return sum(len(w) for w in words if w.startswith("K")) + +def test_k_length_summiert_laengen_der_k_woerter(): + # Arrange + words = ["Apfel", "Banane", "Kirsche", "Kiwi", "Melone"] + erwartet = 11 # 7 (Kirsche) + 4 (Kiwi) + + # Act + ergebnis = k_length(words) + + # Assert + assert ergebnis == erwartet + + +def test_k_length_ohne_k_woerter_ergibt_null(): + # Arrange + words = ["Apfel", "Banane"] + + # Act + ergebnis = k_length(words) + + # Assert + assert ergebnis == 0 + + +def test_k_length_leere_liste_ergibt_null(): + # Arrange + words = [] + + # Act + ergebnis = k_length(words) + + # Assert + assert ergebnis == 0 \ No newline at end of file diff --git a/funcs/06_undo.py b/funcs/06_undo.py index e69de29..51bd6ff 100644 --- a/funcs/06_undo.py +++ b/funcs/06_undo.py @@ -0,0 +1,99 @@ +from dataclasses import dataclass, field, replace + + +@dataclass(frozen=True) +class Particle: + position: tuple = (0, 0) + velocity: tuple = (0, 0) + acceleration: tuple = (0, 0) + history: tuple = () # Vorversionen, älteste zuerst + max_history: int = 5 # Begrenzung gegen Ausufern + + def update(self): + new_velocity = (self.velocity[0] + self.acceleration[0], + self.velocity[1] + self.acceleration[1]) + new_position = (self.position[0] + new_velocity[0], + self.position[1] + new_velocity[1]) + # Vorversion ohne eigene History merken, sonst entsteht eine Kette + previous = replace(self, history=()) + new_history = (self.history + (previous,))[-self.max_history:] + return replace(self, + position=new_position, + velocity=new_velocity, + history=new_history) + + def undo(self): + if not self.history: + return self + return replace(self.history[-1], history=self.history[:-1]) + + +# ---------- Tests ---------- + +def test_update_bewegt_particle_und_merkt_vorversion(): + # Arrange + p = Particle(position=(0, 0), velocity=(1, 0), acceleration=(1, 2)) + + # Act + p2 = p.update() + + # Assert + assert p2.velocity == (2, 2) + assert p2.position == (2, 2) + assert len(p2.history) == 1 + assert p2.history[-1].position == (0, 0) + + +def test_update_veraendert_original_nicht(): + # Arrange + p = Particle(velocity=(1, 1)) + + # Act + p.update() + + # Assert + assert p.position == (0, 0) + assert p.history == () + + +def test_undo_stellt_vorversion_wieder_her(): + # Arrange + p = Particle(velocity=(1, 0)) + p3 = p.update().update().update() # position = (3, 0) + + # Act + zurueck = p3.undo() + + # Assert + assert zurueck.position == (2, 0) + assert len(zurueck.history) == 2 + + +def test_undo_ohne_history_gibt_self_zurueck(): + # Arrange + p = Particle(position=(1, 1)) + + # Act + ergebnis = p.undo() + + # Assert + assert ergebnis == p + + +def test_history_ist_begrenzt(): + # Arrange + p = Particle(velocity=(1, 0), max_history=3) + + # Act + for _ in range(10): + p = p.update() + + # Assert + assert len(p.history) == 3 + assert p.history[0].position == (7, 0) # älteste gemerkte + +test_update_bewegt_particle_und_merkt_vorversion() +test_update_veraendert_original_nicht() +test_undo_stellt_vorversion_wieder_her() +test_undo_ohne_history_gibt_self_zurueck() +test_history_ist_begrenzt() \ No newline at end of file diff --git a/funcs/game.py b/funcs/game.py index cd99b94..70325b7 100644 --- a/funcs/game.py +++ b/funcs/game.py @@ -24,6 +24,7 @@ class Game: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False + self.handle_event(event) if running: running = self.update_game() self.draw_game() @@ -31,6 +32,9 @@ class Game: self.clock.tick(self.fps) pygame.quit() + def handle_event(self, event) -> None: + pass + def update_game(self) -> bool: raise NotImplementedError diff --git a/funcs/undo_doodle.py b/funcs/undo_doodle.py index e69de29..2527c25 100644 --- a/funcs/undo_doodle.py +++ b/funcs/undo_doodle.py @@ -0,0 +1,74 @@ +import pygame +from dataclasses import dataclass, replace + +from game import Game + +MAX_HISTORY = 5 + + +@dataclass(frozen=True) +class Doodle: + strokes: tuple = () # Tuple von Strichen, jeder Strich = Tuple von Punkten + drawing: bool = False # Stift gerade aufgesetzt? + history: tuple = () # alte Strich-Zustände, älteste zuerst + + def start_stroke(self, pos): + """Maustaste gedrückt: neuer Strich, alter Zustand wandert in die Historie.""" + history = (self.history + (self.strokes,))[-MAX_HISTORY:] + return replace(self, + strokes=self.strokes + ((pos,),), + drawing=True, + history=history) + + def add_point(self, pos): + """Mausbewegung: Punkt an den aktuellen Strich hängen (nur bei aufgesetztem Stift).""" + if not self.drawing: + return self + current = self.strokes[-1] + (pos,) + return replace(self, strokes=self.strokes[:-1] + (current,)) + + def end_stroke(self): + """Maustaste losgelassen: Stift abheben.""" + return replace(self, drawing=False) + + def undo(self): + """Backspace: letzten Zustand aus der Historie wiederherstellen.""" + if not self.history: + return self + return replace(self, + strokes=self.history[-1], + drawing=False, + history=self.history[:-1]) + + +class DoodleGame(Game): + def __init__(self): + super().__init__("Undo Doodle", 60, (640, 480)) + self.doodle = Doodle() + + def handle_event(self, event) -> None: + if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: + self.doodle = self.doodle.start_stroke(event.pos) + elif event.type == pygame.MOUSEMOTION: + self.doodle = self.doodle.add_point(event.pos) + elif event.type == pygame.MOUSEBUTTONUP and event.button == 1: + self.doodle = self.doodle.end_stroke() + elif event.type == pygame.KEYDOWN and event.key == pygame.K_BACKSPACE: + self.doodle = self.doodle.undo() + + def update_game(self) -> bool: + return True + + def draw_game(self) -> None: + self.screen.fill((255, 255, 255)) + for stroke in self.doodle.strokes: + if len(stroke) == 1: + pygame.draw.circle(self.screen, (0, 0, 0), stroke[0], 1) + else: + pygame.draw.lines(self.screen, (0, 0, 0), False, stroke, 2) + + + +if __name__ == "__main__": + game = DoodleGame() + game.run() \ No newline at end of file diff --git a/main.py b/main.py index e69de29..8e221f1 100644 --- a/main.py +++ b/main.py @@ -0,0 +1,5 @@ + + +if __name__ == "__main__": + game = DoodleGame() + game.run() \ No newline at end of file