adds the hangman multi level spoilers

This commit is contained in:
waldek 2022-04-26 22:41:09 +02:00
parent 1b48d3f0e0
commit 6d5d75a88b
1 changed files with 656 additions and 61 deletions

View File

@ -3137,6 +3137,601 @@ When looking at the game above we can break down the game logic as follows.
7. A winning banner is shown
8. The player is asked to play a new game or not.
### A non object orientated solution
You can try to create the hangman game in multiple stages.
Below you can find multi level hints towards a solution.
<details>
<summary>Spoiler warning</summary>
<details>
<summary>First hint</summary>
```python
import random
def get_random_word():
pass
def ask_for_letter():
pass
def is_letter_in_word():
pass
def show_hidden_word():
pass
def show_end_game(status):
pass
def game():
pass
def ask_new_game():
pass
def main():
pass
if __name__ == "__main__":
main()
```
</details>
<details>
<summary>Second hint</summary>
```python
import random
def get_random_word():
pass
def ask_for_letter():
pass
def is_letter_in_word(letter, word):
pass
def show_hidden_word(word, letters_found):
pass
def show_end_game(status):
pass
def game():
pass
def ask_new_game():
pass
def main():
while True:
status = game()
show_end_game(status)
status = ask_new_game()
if not status:
break
print("bye bye!")
if __name__ == "__main__":
main()
```
</details>
<details>
<summary>Possible solution</summary>
```python
import random
def get_random_word():
words = ["hello", "world", "apple", "banana", "hippopotamus"]
return random.choice(words)
def ask_for_letter():
while True:
letter = input("your letter please: ")
if len(letter) != 1:
print("only one letter! no cheating!")
continue
if not letter.isalpha():
print("letters please, no numbers nor spaces...")
continue
else:
break
return letter.lower()
def is_letter_in_word(letter, word):
if letter in word:
return True
return False
def show_hidden_word(word, letters_found):
hidden = ""
for letter in word:
if letter in letters_found:
hidden += letter
else:
hidden += "*"
print("word to find: {}".format(hidden))
if "*" in hidden:
return False
return True
def show_end_game(status):
if status:
print("good job!")
else:
print("better luck next time...")
def game():
word_to_guess = get_random_word()
letters_found = []
difficulty = len(word_to_guess) * 3
while len(letters_found) <= difficulty:
left = difficulty - len(letters_found)
print("you have {} tries left".format(left))
status = show_hidden_word(word_to_guess, letters_found)
if status:
return True
letter = ask_for_letter()
letters_found.append(letter)
return False
def ask_new_game():
while True:
result = input("do you want to play a new game?").lower()
if result.startswith("y"):
status = True
break
elif result.startswith("n"):
status = False
break
else:
print("please answer y(es) or n(o)")
return status
def main():
while True:
status = game()
show_end_game(status)
status = ask_new_game()
if not status:
break
print("bye bye!")
if __name__ == "__main__":
main()
```
</details>
</details>
### An object orientated solution
You can try to create the hangman game in multiple stages.
Below you can find multi level hints towards a solution.
<details>
<summary>Spoiler warning</summary>
<details>
<summary>First hint</summary>
```python
import random
class Word(object):
def __init__(self):
pass
def __iter__(self):
pass
def __len__(self):
pass
def is_letter_in_word(self):
pass
class Game(object):
def __init__(self):
pass
def play_turn(self):
pass
def get_hidden_word(self):
pass
def is_word_found(self):
pass
class Controller(object):
def __init__(self):
pass
def set_view(self):
pass
def submit_letter(self):
pass
def request_hidden_word(self):
pass
def request_new_game(self):
pass
class CommandLineInterface(object):
def __init__(self):
pass
def ask_for_letter(self):
pass
def ask_for_bool(self):
pass
def show_message(self):
pass
def run(self):
pass
if __name__ == "__main__":
pass
```
</details>
<details>
<summary>Second hint</summary>
```python
import random
class Word(object):
def __init__(self):
pass
def __iter__(self):
pass
def __len__(self):
pass
def is_letter_in_word(self, letter):
pass
class Game(object):
def __init__(self):
pass
def play_turn(self, letter):
pass
def get_hidden_word(self):
pass
def is_word_found(self):
pass
class Controller(object):
def __init__(self, model):
pass
def set_view(self, view):
pass
def submit_letter(self, letter):
pass
def request_hidden_word(self):
pass
def request_new_game(self):
pass
class CommandLineInterface(object):
def __init__(self, controller):
pass
def ask_for_letter(self):
pass
def ask_for_bool(self, msg):
pass
def show_message(self, msg):
pass
def run(self):
pass
if __name__ == "__main__":
model = Game()
controller = Controller(model)
view = CommandLineInterface(controller)
controller.set_view(view)
view.run()
```
</details>
<details>
<summary>Third hint</summary>
```python
import random
class Word(object):
def __init__(self):
pass
def __iter__(self):
pass
def __len__(self):
pass
def is_letter_in_word(self, letter):
pass
class Game(object):
def __init__(self):
pass
def play_turn(self, letter):
pass
def get_hidden_word(self):
pass
def is_word_found(self):
pass
class Controller(object):
def __init__(self, model):
pass
def set_view(self, view):
pass
def submit_letter(self, letter):
pass
def request_hidden_word(self):
pass
def request_new_game(self):
pass
class CommandLineInterface(object):
def __init__(self, controller):
pass
def ask_for_letter(self):
pass
def ask_for_bool(self, msg):
pass
def show_message(self, msg):
pass
def run(self):
while True:
self.controller.request_hidden_word()
letter = self.ask_for_letter()
status = self.controller.submit_letter(letter)
if status:
status = self.ask_for_bool("do you want to play a new game?")
if status:
self.controller.request_new_game()
else:
break
self.show_message("bye bye!")
if __name__ == "__main__":
model = Game()
controller = Controller(model)
view = CommandLineInterface(controller)
controller.set_view(view)
view.run()
```
</details>
<details>
<summary>Possible solution</summary>
```python
import random
DB = ["hello", "world", "apple", "banana", "hippopotamus"]
class Word(object):
def __init__(self):
self.data = list(random.choice(DB))
def __iter__(self):
for element in self.data:
yield element
def __len__(self):
return len(self.data)
def is_letter_in_word(self, letter):
if letter in self.data:
return True
else:
return False
class Game(object):
def __init__(self):
self.word = Word()
self.tries = len(self.word) * 3
self.letters_found = []
def play_turn(self, letter):
if self.tries == 0:
return False
self.letters_found.append(letter)
if self.word.is_letter_in_word(letter):
if self.is_word_found():
return True
self.tries -= 1
return None
def get_hidden_word(self):
hidden = ""
for letter in self.word:
if letter in self.letters_found:
hidden += letter
else:
hidden += "*"
return hidden
def is_word_found(self):
word = self.get_hidden_word()
if "*" in word:
return False
else:
return True
class Controller(object):
def __init__(self, model):
if isinstance(model, Game):
self.model = model
def set_view(self, view):
if isinstance(view, CommandLineInterface):
self.view = view
def submit_letter(self, letter):
status = self.model.play_turn(letter)
if status:
self.view.show_message("congragulations! you found it!")
return True
elif status == False:
self.view.show_message("better luck next time...")
return True
else:
self.view.show_message("you have {} tries left".format(self.model.tries))
def request_hidden_word(self):
self.view.show_message(self.model.get_hidden_word())
def request_new_game(self):
self.model = Game()
class CommandLineInterface(object):
def __init__(self, controller):
self.controller = controller
def ask_for_letter(self):
while True:
letter = input("your letter please: ")
if len(letter) != 1:
print("only one letter! no cheating!")
continue
if not letter.isalpha():
print("letters only please, no numbers nor spaces...")
continue
else:
break
return letter.lower()
def ask_for_bool(self, msg):
while True:
result = input(msg).lower()
if result.startswith("y"):
status = True
break
elif result.startswith("n"):
status = False
break
else:
print("please answer y(es) or n(o)")
return status
def show_message(self, msg):
print(msg)
def run(self):
while True:
self.controller.request_hidden_word()
letter = self.ask_for_letter()
status = self.controller.submit_letter(letter)
if status:
status = self.ask_for_bool("do you want to play a new game?")
if status:
self.controller.request_new_game()
else:
break
self.show_message("bye bye!")
if __name__ == "__main__":
model = Game()
controller = Controller(model)
view = CommandLineInterface(controller)
controller.set_view(view)
view.run()
```
</details>
</details>
## Trivial pursuit multiple choice game
We can fetch questions for an online [api](https://opentdb.com/api_config.php) to make a game.