Skip to content

Commit

Permalink
Refactoring all files in brain_games
Browse files Browse the repository at this point in the history
  • Loading branch information
AntiViruS90 committed Jun 26, 2024
1 parent c9a55ff commit 7ab7276
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 133 deletions.
1 change: 1 addition & 0 deletions brain_games/game_const.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
ROUND_COUNT = 3
GREETING = "Welcome to the Brain Games!\nMay I have your name?"
PROGRESSION_LENGTH = 10
OPERATORS = ["+", "-", "*"]
34 changes: 10 additions & 24 deletions brain_games/games/calc_engine.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
import random
import prompt
from brain_games.game_const import GAME_INSTRUCTIONS, ROUND_COUNT, GREETING
from brain_games.game_const import GAME_INSTRUCTIONS, OPERATORS
from brain_games.game_engine import run_game


def math_calc():
player_name = prompt.string(f"{GREETING} ")
print(f'Hello, {player_name}!'
f'\n{GAME_INSTRUCTIONS["calc"]}')
for _ in range(ROUND_COUNT):
first_num = random.randint(1, 10)
second_num = random.randint(1, 10)
calc_sign_list = ["+", "-", "*"]
calc_sign_random = random.choice(calc_sign_list)
expression = f"{first_num} {calc_sign_random} {second_num}"
correct_answer = eval(expression)
def math_expression_and_result():
first_num, second_num = random.randint(1, 10), random.randint(1, 10)
operator = random.choice(OPERATORS)
expression = f"{first_num} {operator} {second_num}"
result = eval(expression)
return expression, str(result)

player_answer = prompt.string(f"\nQuestion: {expression}"
f"\nAnswer: ")

if correct_answer == int(player_answer):
print("Correct!")
else:
print(f"'{player_answer}' is wrong answer ;(. "
f"Correct answer was {correct_answer}.\n"
f"Let's try again, {player_name}!")
return

return print(f"Congratulations, {player_name}!")
def run_calc_engine_game():
run_game(math_expression_and_result, GAME_INSTRUCTIONS['calc'])
29 changes: 8 additions & 21 deletions brain_games/games/even_engine.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
import prompt
import random
from brain_games.game_const import GAME_INSTRUCTIONS, ROUND_COUNT, GREETING
from brain_games.game_const import GAME_INSTRUCTIONS
from brain_games.game_engine import run_game


def even_game():
player_name = prompt.string(f"{GREETING} ")
print(f'Hello, {player_name}!'
f'\n{GAME_INSTRUCTIONS["even"]}')
def number_and_even_answer():
num = random.randint(1, 30)
correct_answer = 'yes' if num % 2 == 0 else 'no'
return num, correct_answer

for _ in range(ROUND_COUNT):
num = random.randint(1, 31)
correct_answer = 'yes' if num % 2 == 0 else 'no'

player_answer = prompt.string(f'Question: {num}'
f'\nAnswer: ').lower()

if correct_answer == player_answer:
print("Correct!")
else:
print(f"'{player_answer}' is wrong answer ;(. "
f"Correct answer was {correct_answer}.\n"
f"Let's try again, {player_name}!")
return

return print(f"Congratulations, {player_name}!")
def run_even_engine_game():
run_game(number_and_even_answer, GAME_INSTRUCTIONS['even'])
33 changes: 10 additions & 23 deletions brain_games/games/gcd_engine.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
import math
import random
import prompt
from math import gcd
from brain_games.game_const import GAME_INSTRUCTIONS, ROUND_COUNT, GREETING
from brain_games.game_engine import run_game
from brain_games.game_const import GAME_INSTRUCTIONS


def func_gcd():
player_name = prompt.string(f"{GREETING} ")
print(f"Hello, {player_name}!"
f"\n{GAME_INSTRUCTIONS['gcd']}")
for _ in range(ROUND_COUNT):
first_num = random.randint(1, 100)
second_num = random.randint(1, 55)
def func_numbers_and_gcd():
first_num, second_num = random.randint(1, 50), random.randint(1, 30)
gcd_answer = math.gcd(first_num, second_num)
numbers = f"{first_num} {second_num}"
return numbers, str(gcd_answer)

correct_answer = gcd(first_num, second_num)

player_answer = prompt.string(f"\nQuestion: {first_num} {second_num}"
f"\nAnswer: ")

if correct_answer == int(player_answer):
print("Correct!")
else:
print(f"'{player_answer}' is wrong answer ;(. "
f"Correct answer was {correct_answer}.\n"
f"Let's try again, {player_name}!")
return

return print(f"Congratulations, {player_name}!")
def run_gcd_engine_game():
run_game(func_numbers_and_gcd, GAME_INSTRUCTIONS['gcd'])
46 changes: 14 additions & 32 deletions brain_games/games/prgss_engine.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,21 @@
import random
import prompt
from brain_games.game_const import (
PROGRESSION_LENGTH,
GAME_INSTRUCTIONS,
ROUND_COUNT,
GREETING
)
from brain_games.game_engine import run_game
from brain_games.game_const import PROGRESSION_LENGTH, GAME_INSTRUCTIONS


def func_progression():
player_name = prompt.string(f"{GREETING} ")
print(f"Hello, {player_name}!"
f"\n{GAME_INSTRUCTIONS['progression']}")
def func_progression_and_missed_num():
start_num, step = random.randint(1, 5), random.randint(1, 5)
progression = []

for _ in range(ROUND_COUNT):
progression = []
for i in range(PROGRESSION_LENGTH):
progression.append(start_num + step * i)

start_num, step = random.randint(1, 5), random.randint(1, 5)
for i in range(PROGRESSION_LENGTH):
progression.append(start_num + step * i)
index_missed = random.randint(1, PROGRESSION_LENGTH - 1)
missed_num = progression[index_missed]
progression[index_missed] = '..'
progression_for_player = ' '.join(map(str, progression))
return progression_for_player, str(missed_num)

index_missed = random.randint(1, PROGRESSION_LENGTH - 1)
missed_num = progression[index_missed]
progression[index_missed] = '..'
progression_for_player = ' '.join(map(str, progression))

player_answer = prompt.string(f"\nQuestion: {progression_for_player}"
f"\nAnswer: ")

if missed_num == int(player_answer):
print("Correct!")
else:
print(f"'{player_answer}' is wrong answer ;(. "
f"Correct answer was {missed_num}.\n"
f"Let's try again, {player_name}!")
return

return print(f"Congratulations, {player_name}!")
def run_func_progression_and_answer():
run_game(func_progression_and_missed_num, GAME_INSTRUCTIONS['progression'])
35 changes: 13 additions & 22 deletions brain_games/games/prime_engine.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
import random
import prompt
from brain_games.game_const import ROUND_COUNT, GREETING, GAME_INSTRUCTIONS
from brain_games.game_engine import run_game
from brain_games.game_const import GAME_INSTRUCTIONS


def func_prime():
player_name = prompt.string(f"{GREETING} ")
print(f"Hello, {player_name}!"
f"\n{GAME_INSTRUCTIONS['prime']}")
def func_prime(num):
prime_number = 'no' if num <= 1 or any(
num % i == 0 for i in range(2, (num // 2 + 1))
) else 'yes'
return prime_number

for _ in range(ROUND_COUNT):
prime_num = random.randint(1, 30)

correct_answer = 'no' if prime_num <= 1 or any(
prime_num % i == 0 for i in range(2, (prime_num // 2 + 1))
) else 'yes'
def get_prime_num_and_answer():
num = random.randint(1, 30)
prime = func_prime(num)
return num, prime

player_answer = prompt.string(f"\nQuestion: {prime_num}"
f"\nAnswer: ")

if correct_answer == player_answer:
print("Correct!")
else:
print(f"'{player_answer}' is wrong answer ;(. "
f"Correct answer was '{correct_answer}'.\n"
f"Let's try again, {player_name}!")
return

return print(f"Congratulations, {player_name}!")
def run_prime_engine_game():
run_game(get_prime_num_and_answer, GAME_INSTRUCTIONS['prime'])
4 changes: 2 additions & 2 deletions brain_games/scripts/brain_calc.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python3
from brain_games.games.calc_engine import math_calc
from brain_games.games.calc_engine import run_calc_engine_game


def main():
math_calc()
run_calc_engine_game()


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions brain_games/scripts/brain_even.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python3
from brain_games.games.even_engine import even_game
from brain_games.games.even_engine import run_even_engine_game


def main():
even_game()
run_even_engine_game()


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions brain_games/scripts/brain_gcd.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python3
from brain_games.games.gcd_engine import func_gcd
from brain_games.games.gcd_engine import run_gcd_engine_game


def main():
func_gcd()
run_gcd_engine_game()


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions brain_games/scripts/brain_prime.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python3
from brain_games.games.prime_engine import func_prime
from brain_games.games.prime_engine import run_prime_engine_game


def main():
func_prime()
run_prime_engine_game()


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions brain_games/scripts/brain_progression.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python3
from brain_games.games.prgss_engine import func_progression
from brain_games.games.prgss_engine import run_func_progression_and_answer


def main():
func_progression()
run_func_progression_and_answer()


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ packages = [
[tool.poetry.dependencies]
python = "^3.10"
prompt = "^0.4.1"
flake8 = "^7.1.0"

[tool.poetry.scripts]
brain-games = "brain_games.scripts.brain_games:main"
Expand Down

0 comments on commit 7ab7276

Please sign in to comment.