-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring all files in brain_games
- Loading branch information
1 parent
c9a55ff
commit 7ab7276
Showing
13 changed files
with
68 additions
and
133 deletions.
There are no files selected for viewing
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
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
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']) |
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
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']) |
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
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']) |
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
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']) |
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
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']) |
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
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
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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