Skip to content

Commit e132340

Browse files
Merge pull request steam-bell-92#1432 from ParidhiMis/fix-word-scramble-quit
fix: prevent game over screen when player quits
2 parents 3a3b78c + 617cc28 commit e132340

1 file changed

Lines changed: 139 additions & 28 deletions

File tree

games/Word-Scramble-Game/Word-Scramble-Game.py

Lines changed: 139 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -338,37 +338,148 @@ def main():
338338
score = 0
339339
lives = 3
340340
round_num = 1
341-
quit_game = False
342-
343-
# Build pool: all words from the chosen tier across every category
344-
pool: list[tuple[str, str]] = [] # (category, word)
345-
for cat, tiers in WORD_BANK.items():
346-
for w in tiers.get(tier, []):
347-
pool.append((cat, w))
348-
349-
random.shuffle(pool)
350-
pool_index = 0
351-
352-
while lives > 0 and not quit_game:
353-
# Cycle through words; reshuffle when exhausted
354-
if pool_index >= len(pool):
355-
random.shuffle(pool)
356-
pool_index = 0
357-
358-
category, word = pool[pool_index]
359-
pool_index += 1
341+
still_playing = True
342+
343+
while lives > 0 and still_playing:
344+
settings = DIFFICULTY_LEVELS[difficulty]
345+
valid_words = []
346+
for category, words in WORD_BANK.items():
347+
filtered_words = [w for w in words if settings["min_length"] <= len(
348+
w) <= settings["max_length"]]
349+
if filtered_words:
350+
valid_words.append((category, filtered_words))
351+
352+
category, words = random.choice(valid_words)
353+
word = random.choice(words)
354+
355+
letters = list(word)
356+
scrambled = ""
357+
for _ in range(100):
358+
random.shuffle(letters)
359+
scrambled = "".join(letters)
360+
if scrambled != word:
361+
break
362+
363+
fancy_scrambled = " ".join(scrambled.upper())
364+
365+
hint_used = False
366+
attempts = 0
367+
max_attempts = 3
368+
369+
while attempts < max_attempts:
370+
os.system("cls" if os.name == "nt" else "clear")
371+
print("╔══════════════════════════════════════════════╗")
372+
print("║ 🔤 W O R D S C R A M B L E 🔤 ║")
373+
print("╠══════════════════════════════════════════════╣")
374+
lives_str = "❤️ " * lives + "🖤 " * (3 - lives)
375+
print(
376+
f"║ Round: {round_num:<5} Score: {score:<6} Lives: {lives_str:<14}║")
377+
print(f"║ Difficulty: {difficulty.capitalize():<31}║")
378+
print("╚══════════════════════════════════════════════╝\n")
379+
380+
print(f" 🔀 Unscramble this word:\n")
381+
print(f" ✨ {fancy_scrambled}\n")
382+
print(
383+
f" Letters: {len(word)} | Attempts left this round: {max_attempts - attempts}")
384+
385+
if hint_used:
386+
print(f" 💡 Hint: {category}")
387+
388+
print("\n Commands: [answer] · 'hint' · 'skip' · 'quit'\n")
389+
390+
try:
391+
raw = input(" ➤ Your guess: ").strip().lower()
392+
except (EOFError, KeyboardInterrupt):
393+
still_playing = False
394+
break
395+
396+
if raw == "quit":
397+
still_playing = False
398+
break
399+
400+
if raw == "skip":
401+
print(f"\n ⏭️ Skipped! The word was: {word.upper()}")
402+
time.sleep(1.5)
403+
break
404+
405+
if raw == "hint":
406+
if not hint_used:
407+
hint_used = True
408+
print(f"\n 💡 Hint unlocked: {category}")
409+
else:
410+
print("\n 💡 You already used your hint!")
411+
time.sleep(1)
412+
continue
413+
414+
attempts += 1
415+
416+
if raw == word:
417+
base_points = DIFFICULTY_LEVELS[difficulty]["points"]
418+
bonus = base_points if not hint_used else base_points // 2
419+
score += bonus
420+
421+
os.system("cls" if os.name == "nt" else "clear")
422+
print("╔══════════════════════════════════════════════╗")
423+
print("║ 🔤 W O R D S C R A M B L E 🔤 ║")
424+
print("╠══════════════════════════════════════════════╣")
425+
print(
426+
f"║ Round: {round_num:<5} Score: {score:<6} Lives: {lives_str:<14}║")
427+
print(f"║ Difficulty: {difficulty.capitalize():<31}║")
428+
print("╚══════════════════════════════════════════════╝\n")
429+
430+
print(
431+
f"\n 🎉 Correct! +{bonus} points {'(hint used: half points)' if hint_used else ''}\n")
432+
time.sleep(1.5)
433+
break
434+
else:
435+
remaining = max_attempts - attempts
436+
if remaining > 0:
437+
print(
438+
f"\n ❌ Nope! Try again. ({remaining} attempt{'s' if remaining != 1 else ''} left)")
439+
time.sleep(1)
440+
else:
441+
lives -= 1
442+
os.system("cls" if os.name == "nt" else "clear")
443+
print("╔══════════════════════════════════════════════╗")
444+
print("║ 🔤 W O R D S C R A M B L E 🔤 ║")
445+
print("╠══════════════════════════════════════════════╣")
446+
lives_str = "❤️ " * lives + "🖤 " * (3 - lives)
447+
print(
448+
f"║ Round: {round_num:<5} Score: {score:<6} Lives: {lives_str:<14}║")
449+
print(f"║ Difficulty: {difficulty.capitalize():<31}║")
450+
print("╚══════════════════════════════════════════════╝\n")
451+
452+
print(
453+
f"\n 💔 Out of attempts! The word was: {word.upper()}\n")
454+
time.sleep(2)
455+
break
456+
457+
if still_playing:
458+
round_num += 1
360459

361-
earned, lost_life, quit_game = play_round(
362-
word, category, difficulty, round_num, score, lives
460+
if still_playing:
461+
os.system("cls" if os.name == "nt" else "clear")
462+
print("\n ╔══════════════════════════════════╗")
463+
print(" ║ 💀 GAME OVER 💀 ║")
464+
print(" ╚══════════════════════════════════╝\n")
465+
print(
466+
f" You survived {round_num} round{'s' if round_num != 1 else ''}.")
467+
print(f" Final score: {score} points\n")
468+
469+
grade = (
470+
"🏆 Wordsmith Supreme!" if score >= 80 else
471+
"🥇 Excellent!" if score >= 60 else
472+
"🥈 Good effort!" if score >= 40 else
473+
"🥉 Keep practising!" if score >= 20 else
474+
"📚 Hit the dictionary!"
363475
)
476+
print(f" {grade}\n")
477+
else:
478+
os.system("cls" if os.name == "nt" else "clear")
479+
print("\nThanks for playing! 👋\n")
364480

365-
score += earned
366-
if lost_life:
367-
lives -= 1
368-
if not quit_game:
369-
round_num += 1
370-
371-
game_over_screen(round_num - 1, score)
481+
if not still_playing:
482+
break
372483

373484
try:
374485
again = input(" Play again? (y/n): ").strip().lower()

0 commit comments

Comments
 (0)