From f0d429e5d5d299059d8590c4e25b6a8d3879c6a9 Mon Sep 17 00:00:00 2001 From: darklithium Date: Thu, 4 Jun 2026 23:55:11 +0200 Subject: [PATCH] feat: Implement shuffle without repetition for fortune selection - Added Fisher-Yates shuffle with pointer mechanism - Guarantees no repeats until all fortunes have been shown - Global variables: _shuffled_fortunes, _shuffle_index Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe --- src/fortunecookie.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) 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():