Skip to content

Commit 93f5a49

Browse files
committed
reworked games and scripts folders and engine
1 parent 691d8bc commit 93f5a49

26 files changed

+127
-220
lines changed

brain_games/cli.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

brain_games/engine.py

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,24 @@
11
# engine.py
22

3-
def welcome_user():
3+
def run_game(game_module):
44
print("Welcome to the Brain Games!")
55
name = input("May I have your name? ")
66
print(f"Hello, {name}!")
7-
return name
8-
9-
10-
def ask_question(question):
11-
print(f"Question: {question}")
12-
return input("Your answer: ")
13-
14-
15-
def check_answer(user_answer, correct_answer, name):
16-
if user_answer == correct_answer:
17-
print("Correct!")
18-
return True
19-
else:
20-
print(
21-
f"'{user_answer}' is wrong answer ;(. "
22-
f"Correct answer was '{correct_answer}'."
23-
)
24-
print(f"Let's try again, {name}!")
25-
return False
26-
27-
28-
def run_game(game_logic):
29-
name = welcome_user()
30-
print(game_logic['instructions'])
31-
32-
for _ in range(3): # Максимальное количество раундов
33-
question, correct_answer = game_logic['get_question_and_answer']()
34-
user_answer = ask_question(question)
35-
36-
if not check_answer(user_answer, correct_answer, name):
7+
8+
print(game_module.DESCRIPTION)
9+
10+
for _ in range(3):
11+
question, correct_answer = game_module.get_question_and_answer()
12+
print(f"Question: {question}")
13+
answer = input("Your answer: ")
14+
15+
if answer == correct_answer:
16+
print("Correct!")
17+
else:
18+
print(f"'{answer}' is wrong answer ;(. Correct answer was '{correct_answer}'.")
19+
print(f"Let's try again, {name}!")
3720
return
38-
21+
3922
print(f"Congratulations, {name}!")
4023

4124

brain_games/games/brain_calc.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import random
2+
3+
DESCRIPTION = 'What is the result of the expression?'
4+
5+
def get_random_expression():
6+
operations = ['+', '-', '*']
7+
num1 = random.randint(1, 100)
8+
num2 = random.randint(1, 100)
9+
operation = random.choice(operations)
10+
expression = f"{num1} {operation} {num2}"
11+
return expression, eval(expression)
12+
13+
def get_question_and_answer():
14+
expression, correct_answer = get_random_expression()
15+
return expression, str(correct_answer)

brain_games/games/brain_even.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import random
2+
3+
DESCRIPTION = 'Answer "yes" if the number is even, otherwise answer "no".'
4+
5+
def get_question_and_answer():
6+
number = random.randint(1, 100)
7+
question = number
8+
correct_answer = 'yes' if number % 2 == 0 else 'no'
9+
return question, correct_answer

brain_games/games/brain_gcd.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import random
2+
import math
3+
4+
DESCRIPTION = 'Find the greatest common divisor of given numbers.'
5+
6+
def get_question_and_answer():
7+
number1 = random.randint(1, 100)
8+
number2 = random.randint(1, 100)
9+
correct_answer = str(math.gcd(number1, number2))
10+
return f"{number1} {number2}", correct_answer

brain_games/games/brain_prime.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import random
2+
3+
DESCRIPTION = 'Answer "yes" if the given number is prime. Otherwise answer "no".'
4+
5+
def is_prime(n):
6+
if n < 2:
7+
return False
8+
for i in range(2, int(n ** 0.5) + 1):
9+
if n % i == 0:
10+
return False
11+
return True
12+
13+
def get_question_and_answer():
14+
number = random.randint(1, 100)
15+
correct_answer = 'yes' if is_prime(number) else 'no'
16+
return str(number), correct_answer
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import random
2+
3+
DESCRIPTION = "What number is missing in the progression?"
4+
5+
def get_question_and_answer():
6+
length = random.randint(5, 10)
7+
start = random.randint(1, 20)
8+
step = random.randint(1, 5)
9+
progression = [start + i * step for i in range(length)]
10+
11+
hidden_index = random.randint(0, length - 1)
12+
hidden_value = progression[hidden_index]
13+
14+
progression[hidden_index] = '..'
15+
16+
return ' '.join(map(str, progression)), str(hidden_value)
-170 Bytes
Binary file not shown.
-1.68 KB
Binary file not shown.
-1.33 KB
Binary file not shown.

0 commit comments

Comments
 (0)