Last active
October 16, 2021 07:10
-
-
Save classmember/d1d29248213a13a42527ab836a8026f8 to your computer and use it in GitHub Desktop.
Number Guessing Game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Random number guessing game | |
""" | |
import sys | |
from random import randrange | |
from math import ceil, floor | |
class NumberGuessingGame: | |
def __init__(self, low=1, high=1000): | |
""" | |
Params: | |
low (int) lowest number in range (inclusive) | |
high (int) highest number in range (inclusive) | |
Side Effect: | |
Plays a fun number guessing game | |
""" | |
self.__running__ = True | |
self.answer = randrange(low, high + 1) | |
self.low = low | |
self.low_hint = low | |
self.high = high | |
self.high_hint = high | |
self.hints = 'disabled' | |
self.wins = 0 | |
def display_rules(self): | |
print("==================================") | |
print("= Number Guessing Game =") | |
print("==================================") | |
print("= =") | |
print("= > A random number between =") | |
print(f"= {self.low} and {self.high} ") | |
print("= is generated =") | |
print("= =") | |
print("= > Player guesses the number =") | |
print("= =") | |
print("= > If the player guesses the =") | |
print("= correct number, you win! =") | |
print("= =") | |
print("= > Ctrl + C to exit =") | |
print("= =") | |
print("==================================") | |
print() | |
def handle_level_up(self): | |
if self.wins == 1: | |
print("Level Up! > Hints enabled") | |
self.hints = 'enabled' | |
if self.wins >= 2: | |
print("Level Up! > Hints get closer to correct answer") | |
self.hints = 'advanced' | |
print(f"! LVL {self.wins} !") | |
def win(self): | |
self.wins = self.wins + 1 | |
self.low_hint = self.low | |
self.high_hint = self.high | |
print() | |
print("==================================") | |
print("= =") | |
print(f"= {self.answer} is correct! ") | |
print("= =") | |
print("==================================") | |
print(" ") | |
print("==================================") | |
print("= =") | |
print("= You win! =") | |
print("= =") | |
print("==================================") | |
self.answer = randrange(self.low, self.high + 1) | |
self.handle_level_up() | |
def wrong_answer(self, guess): | |
if guess < self.answer: | |
if guess > self.low_hint: | |
self.low_hint = guess | |
return f"^ higher" | |
if guess > self.answer: | |
if guess < self.high_hint: | |
self.high_hint = guess | |
return f"v lower" | |
def wrong_answer_with_hints(self, guess): | |
if guess < self.answer: | |
if guess > self.low_hint: | |
self.low_hint = guess | |
return f"^ higher [{self.low_hint} - {self.high_hint}]" | |
if guess > self.answer: | |
if guess < self.high_hint: | |
self.high_hint = guess | |
return f"v lower [{self.low_hint} - {self.high_hint}]" | |
def closer_lower_hint(self, guess): | |
closer = guess + floor(self.answer * (self.wins/125)) | |
if closer > self.answer: | |
return self.answer | |
else: | |
little_closer = closer + ceil(self.answer * (self.wins/250)) | |
if little_closer > self.answer: | |
return self.answer | |
else: | |
return little_closer | |
def closer_higher_hint(self, guess): | |
closer = guess - ceil(self.answer * (self.wins/125)) | |
if closer < self.answer: | |
return self.answer | |
else: | |
little_closer = closer - ceil(self.answer * (self.wins/250)) | |
if little_closer < self.answer: | |
return self.answer | |
else: | |
return little_closer | |
def wrong_answer_with_advanced_hints(self, guess): | |
if guess < self.answer: | |
if guess > self.low_hint: | |
self.low_hint = self.closer_lower_hint(guess) | |
return f"^ higher [{self.low_hint} - {self.high_hint}]" | |
if guess > self.answer: | |
if guess < self.high_hint: | |
self.high_hint = self.closer_higher_hint(guess) | |
return f"v lower [{self.low_hint} - {self.high_hint}]" | |
def check_answer(self, guess): | |
if guess == self.answer: | |
self.win() | |
else: | |
if self.hints == 'disabled': | |
print(self.wrong_answer(guess)) | |
if self.hints == 'enabled': | |
print(self.wrong_answer_with_hints(guess)) | |
if self.hints == 'advanced': | |
print(self.wrong_answer_with_advanced_hints(guess)) | |
def validated_range(self, guess): | |
if guess < self.low: | |
print(f"[Too Low]\n please enter a number between {self.low} and {self.high}") | |
return False | |
if guess > self.high: | |
print(f"[Too High]\n please enter a number between {self.low} and {self.high}") | |
return False | |
return True | |
def user_provided_valid_guess(self, guess_text): | |
try: | |
guess = int(guess_text) | |
result = self.validated_range(guess) | |
return result | |
except ValueError as err: | |
print(f"[Invalid Input]\n please enter a number between {self.low} and {self.high}") | |
return False | |
def handle_user_input(self): | |
guess_text = input("> ") | |
if self.user_provided_valid_guess(guess_text): | |
guess = int(guess_text) | |
self.check_answer(guess) | |
def game_loop(self): | |
while self.__running__: | |
self.handle_user_input() | |
def run(self): | |
"""show rules and run main game loop""" | |
self.display_rules() | |
try: | |
self.game_loop() | |
except KeyboardInterrupt: | |
print(f"The correct number was {self.answer}. Goodbye") | |
exit(0) | |
def main(): | |
game = NumberGuessingGame() | |
game.run() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment