Skip to content

Commit

Permalink
added 3rd phase of wog game
Browse files Browse the repository at this point in the history
- added scores.txt to gitignore
- added function validate_str_in_list_case_insensitive to input_validation
  • Loading branch information
mattisafur committed Jun 29, 2024
1 parent 3196b89 commit e818056
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,4 @@ cython_debug/
#.idea/

credentials.py
scores.txt
6 changes: 5 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import input_validation
from games import currency_roulette_game, guess_game, memory_game
from score import add_score

games = [
memory_game,
Expand Down Expand Up @@ -42,4 +43,7 @@ def start_play() -> None:
difficulty = game.difficulties[parsed_difficulty_level - 1]

# start selected game
game.play(difficulty)
win: bool = game.play(difficulty)

if win:
add_score(parsed_difficulty_level)
1 change: 0 additions & 1 deletion games/memory_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
'hard'
]


def play(difficulty: str) -> bool:
sequence_length: int = difficulties.index(difficulty) + 1
sequence: list[int] = generate_sequence(sequence_length)
Expand Down
53 changes: 35 additions & 18 deletions input_validation.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
def print_if_exist(prompt: str | None) -> None:
if prompt is not None:
print(prompt)


def validate_int_in_range(string: str, min: int, max: int, input_empty_prompt: str | None = None, input_not_number_prompt: str | None = None, input_not_int_prompt: str | None = None, input_not_in_range_prompt: str | None = None) -> int | None:
# no input (empty)
if not string:
if input_empty_prompt is not None:
print(input_empty_prompt)
print_if_exist(input_empty_prompt)
return None

# input is not a number
try:
float(string)
except ValueError:
if input_not_number_prompt is not None:
print(input_not_number_prompt)
print_if_exist(input_not_number_prompt)
return None

# input not an int
try:
result: int = int(string)
except ValueError:
if input_not_int_prompt is not None:
print(input_not_in_range_prompt)
print_if_exist(input_not_int_prompt)
return None

# input not in range
if not min <= result <= max:
if input_not_in_range_prompt is not None:
print(input_not_in_range_prompt)
print_if_exist(input_not_in_range_prompt)
return None

return result
Expand All @@ -33,23 +34,20 @@ def validate_int_in_range(string: str, min: int, max: int, input_empty_prompt: s
def validate_int(string: str, input_empty_prompt: str | None = None, input_not_number_prompt: str | None = None, input_not_int_prompt: str | None = None):
# no input (empty)
if not string:
if input_empty_prompt is not None:
print(input_empty_prompt)
print_if_exist(input_empty_prompt)
return None

# input is not a number
try:
float(string)
except ValueError:
if input_not_number_prompt is not None:
print(input_not_number_prompt)
print_if_exist(input_not_number_prompt)
return None

try:
result: int = int(string)
except ValueError:
if input_not_int_prompt is not None:
print(input_not_int_prompt)
print_if_exist(input_not_int_prompt)
return None

return result
Expand All @@ -58,16 +56,35 @@ def validate_int(string: str, input_empty_prompt: str | None = None, input_not_n
def validate_float(string: str, input_empty_prompt: str | None = None, input_not_float_prompt: str | None = None) -> float | None:
# no input (empty)
if not string:
if input_empty_prompt is not None:
print(input_empty_prompt)
print_if_exist(input_empty_prompt)
return None

# input is not a float (or any number for that matter)
try:
result: float = float(string)
except ValueError:
if input_not_float_prompt is not None:
print(input_not_float_prompt)
print_if_exist(input_not_float_prompt)
return None

return result


def validate_str_in_list_case_insensitive(string: str, options: tuple[str, ...], default_value: str | None = None, input_not_in_list_prompt: str | None = None) -> str | None:
options_case_insensitive: tuple[str, ...] = tuple(
option.casefold() for option in options)

if default_value is not None:
# default option is not in
if default_value.casefold() not in options_case_insensitive:
raise ValueError('Default option not in list of options')

# no input (empty)
if not string:
return default_value

# input is not an option
if string.casefold() not in options_case_insensitive:
print_if_exist(input_not_in_list_prompt)
return None

return string
36 changes: 36 additions & 0 deletions main_score.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from flask import Flask
import utils


score_website: Flask = Flask(__name__)


@score_website.route('/')
def main_page() -> str:
with open(utils.scores_file_name) as scores_file:
score: str = scores_file.read()

# try to parse score. if passes, display score on web page, of not, display erroron web page.
try:
int(score)
return f'''
<html>
<head>
<title> Scores Game</title>
</head>
<body>
<h1>The score is:</h1>
<div id="score">{score}</dev>
</body>
</html>'''
except ValueError as error:
return f'''
<html>
<head>
<title>Scores Game</title>
</head>
<body>
<h1>ERROR :< /h1>
<div id="score" style="color:red">{error}</div>
</body>
</html>'''
46 changes: 46 additions & 0 deletions score.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import utils
import input_validation


def add_score(difficutly_number: int) -> None:
points: int = (difficutly_number * 3) + 5

with open(utils.scores_file_name, 'r+') as scores_file:
score_file_value: str = scores_file.read()

if score_file_value:
previous_score: int = 0
try:
# parse save file
previous_score = int(score_file_value)
except ValueError:
# save file is corrupted, let user choose between clearing the score file or quitting the program
options: tuple[str, ...] = ('y', 'yes', 'n', 'no')
default_option: str = 'n'

while True:
user_input: str = input(
'Score file is corrupted, would you like to clear it? [yes/No]: ')
user_selection = input_validation.validate_str_in_list_case_insensitive(
user_input, options, default_option, input_not_in_list_prompt='Invalid input.')

if user_selection is None:
break

match user_input.casefold():
case 'y' | 'yes':
# clear file and set score to 0
scores_file.write('')
previous_score = 0
break
case 'n' | 'no':
print('Quitting.')
quit()
else:
previous_score: int = 0

score: int = previous_score + points

scores_file.seek(0)
scores_file.write(str(score))
scores_file.truncate()
8 changes: 8 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
scores_file_name = 'Scores.txt'

bad_return_code = 500


def clear_screen() -> None:
print('\x1b[2J')

0 comments on commit e818056

Please sign in to comment.