diff --git a/src/fortunecookie.py b/src/fortunecookie.py index c8b08d1..74a237d 100644 --- a/src/fortunecookie.py +++ b/src/fortunecookie.py @@ -52,6 +52,8 @@ AVAILABLE_FORTUNE_LISTS = [ _current_fortune_list = "fortune" _current_fortune = "" _fortunes = {} +_shuffled_fortunes = [] +_shuffle_index = 0 _initialized = False @@ -112,8 +114,8 @@ def _load_all_fortune_lists(): def _get_random_fortune(): - """Gibt einen zufaelligen Spruch aus der aktuellen Liste zurueck.""" - global _current_fortune_list, _fortunes + """Gibt einen zufaelligen Spruch aus der aktuellen Liste zurueck (Shuffle ohne Wiederholung).""" + global _current_fortune_list, _fortunes, _shuffled_fortunes, _shuffle_index _init() @@ -123,7 +125,15 @@ def _get_random_fortune(): if not _fortunes.get("fortune"): return "Keine Sprueche verfguebar." - return random.choice(_fortunes[_current_fortune_list]) + # Neu shufflen wenn Liste leer oder am Ende + if not _shuffled_fortunes or _shuffle_index >= len(_shuffled_fortunes): + _shuffled_fortunes = _fortunes[_current_fortune_list].copy() + random.shuffle(_shuffled_fortunes) + _shuffle_index = 0 + + fortune = _shuffled_fortunes[_shuffle_index] + _shuffle_index += 1 + return fortune def get_initial_fortune():