#! /usr/bin/python3
# ===================================================================
# A simple number guessing game
# ===================================================================
# This game is not as simple as it first seems. It has several states
# and conditions that the programmer must take into account. Also,
# getting and validate user input.
#
# Note: Never trust the user. Always validate their input
# before using it.
# ===================================================================
import sys
import random
import user_interface as ui
min = 1 # guessing range minimum
max = 100 # guessing range maximum
# ---- display the game description
def game_description():
ui.clear_screen()
print('---------- A simple Number Guessing Game ----------')
print(f'An integer will be selected from {min} <= i <= {max}.')
print('You must guess the number. If you guess correctly,')
print('you win. Otherwise, you are told if your guess is')
print('high or low. If you enter nothing at the prompt,')
print('you will exit the game.')
# ---- play the game
while(True):
game_description()
# ---- select the target number in the range
rn = random.randint(min, max)
##print()
##print(f'Debug: rn = {rn}')
# ---- process the users input
while(True):
# ---- ask the user for input
print()
s = ui.get_user_input('Enter your guess: ')
if not s: # empty string?
print()
sys.exit()
# ---- test the user's input
x,n = ui.is_int(s) # user's input an integer?
if not x:
print()
print('input was not an integer, try again')
continue
if n < min or n > max:
print()
print('Guess is out of range - try again')
continue
# ---- compare the user's input to the target number
if n == rn:
print('Hurray you guessed correctly')
break
if n < rn:
print('Your guess is low')
else:
print('your guess is high')
# ---- want to another game?
print()
s = ui.get_user_input('Want another game [Y,y]? ')
if len(s) > 0 and (s[0] == 'y' or s[0] == 'Y'):
continue
print()
break