From 9d715836b64e997a5b560cf0de82628adec8c4e8 Mon Sep 17 00:00:00 2001 From: Atharv-AC <235652593+Atharv-AC@users.noreply.github.com> Date: Tue, 23 Jun 2026 23:17:37 +0530 Subject: [PATCH 1/3] Add reverse hangman game --- games/logic/reverse_hangman/game.py | 95 +++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 games/logic/reverse_hangman/game.py diff --git a/games/logic/reverse_hangman/game.py b/games/logic/reverse_hangman/game.py new file mode 100644 index 0000000..1186371 --- /dev/null +++ b/games/logic/reverse_hangman/game.py @@ -0,0 +1,95 @@ +from collections import Counter + +AUTHOR = "Atharv-AC" + +WORDS = [ + "python", + "script", + "terminal", + "program", + "variable", + "function", + "computer", + "keyboard", + "monitor", + "internet", + "database", + "network", + "binary", + "system", + "memory", + "rocket", + "planet", + "garden", + "castle", + "bridge", + "forest", +] + + +def get_best_letter(candidates, guessed): + counter = Counter() + + for word in candidates: + for letter in set(word): + if letter not in guessed: + counter[letter] += 1 + + if not counter: + return None + + return counter.most_common(1)[0][0] + + +def run(): + print(f"Game by {AUTHOR}") + print("=== Reverse Hangman ===") + print("Think of a secret word.") + print("Answer only with y or n.\n") + + while True: + try: + length = int(input("Enter the length of your word: ")) + break + except ValueError: + print("Please enter a valid number.") + + candidates = [word for word in WORDS if len(word) == length] + + if not candidates: + print("Sorry, I don't know any words of that length.") + return + + guessed = set() + + while len(candidates) > 1: + letter = get_best_letter(candidates, guessed) + + if letter is None: + break + + guessed.add(letter) + + answer = input(f"Is the letter '{letter}' in your word? (y/n): ").strip().lower() + + while answer not in ("y", "n"): + answer = input("Please enter y or n: ").strip().lower() + + if answer == "y": + candidates = [w for w in candidates if letter in w] + else: + candidates = [w for w in candidates if letter not in w] + + print(f"Possible words remaining: {len(candidates)}") + + print() + + if len(candidates) == 1: + print(f"My guess is: {candidates[0]}") + elif len(candidates) > 1: + print("I couldn't narrow it down to one word.") + print("Possible words:") + for word in candidates[:10]: + print("-", word) + else: + print("No matching word found.") \ No newline at end of file From 65c1781e2c0b1a97b7fb653c2f96005ab659308b Mon Sep 17 00:00:00 2001 From: Atharv-AC <235652593+Atharv-AC@users.noreply.github.com> Date: Wed, 24 Jun 2026 13:40:54 +0530 Subject: [PATCH 2/3] Add letter position support to reverse hangman --- games/logic/reverse_hangman/game.py | 56 ++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 5 deletions(-) diff --git a/games/logic/reverse_hangman/game.py b/games/logic/reverse_hangman/game.py index 1186371..8ebd0a6 100644 --- a/games/logic/reverse_hangman/game.py +++ b/games/logic/reverse_hangman/game.py @@ -41,11 +41,46 @@ def get_best_letter(candidates, guessed): return counter.most_common(1)[0][0] +def get_letter_positions(letter, length): + while True: + raw = input( + f"Enter positions for '{letter}' (1-{length}, comma-separated): " + ).strip() + + try: + positions = [int(value.strip()) - 1 for value in raw.split(",")] + + if not positions: + raise ValueError + + if len(set(positions)) != len(positions): + print("Please do not enter the same position twice.") + continue + + if any(position < 0 or position >= length for position in positions): + print(f"Positions must be between 1 and {length}.") + continue + + return set(positions) + + except ValueError: + print("Please enter positions like: 1,3,5") + + +def matches_positions(word, letter, positions): + actual_positions = { + index for index, character in enumerate(word) + if character == letter + } + return actual_positions == positions + + def run(): print(f"Game by {AUTHOR}") print("=== Reverse Hangman ===") print("Think of a secret word.") - print("Answer only with y or n.\n") + print("When a letter is present, enter all of its positions.") + print("Use 1-based positions, for example: 1,3,5\n") while True: try: @@ -70,15 +105,22 @@ def run(): guessed.add(letter) - answer = input(f"Is the letter '{letter}' in your word? (y/n): ").strip().lower() + answer = input( + f"Is the letter '{letter}' in your word? (y/n): " + ).strip().lower() while answer not in ("y", "n"): answer = input("Please enter y or n: ").strip().lower() if answer == "y": - candidates = [w for w in candidates if letter in w] + positions = get_letter_positions(letter, length) + candidates = [ + word + for word in candidates + if matches_positions(word, letter, positions) + ] else: - candidates = [w for w in candidates if letter not in w] + candidates = [word for word in candidates if letter not in word] print(f"Possible words remaining: {len(candidates)}") @@ -92,4 +134,8 @@ def run(): for word in candidates[:10]: print("-", word) else: - print("No matching word found.") \ No newline at end of file + print("No matching word found.") + + +if __name__ == "__main__": + run() \ No newline at end of file From bcc97545f0690f4198914889ace062ad3c8f76d8 Mon Sep 17 00:00:00 2001 From: Atharv-AC <235652593+Atharv-AC@users.noreply.github.com> Date: Wed, 24 Jun 2026 15:25:33 +0530 Subject: [PATCH 3/3] Add letter position support to reverse hangman --- games/logic/reverse_hangman/game.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/games/logic/reverse_hangman/game.py b/games/logic/reverse_hangman/game.py index 8ebd0a6..f16a006 100644 --- a/games/logic/reverse_hangman/game.py +++ b/games/logic/reverse_hangman/game.py @@ -137,5 +137,3 @@ def run(): print("No matching word found.") -if __name__ == "__main__": - run() \ No newline at end of file