Skip to content

Commit 100e69e

Browse files
committed
check 4
1 parent 38aed48 commit 100e69e

19 files changed

+127
-98
lines changed

README.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
### Hexlet tests and linter status:
2+
[![Actions Status](https://github.com/s-gala/python-project-49/actions/workflows/hexlet-check.yml/badge.svg)](https://github.com/s-gala/python-project-49/actions)
3+
4+
### CodeClimate
5+
[![Maintainability](https://api.codeclimate.com/v1/badges/487e17fe16141caa4b2a/maintainability)](https://codeclimate.com/github/s-gala/python-project-49/maintainability)
6+
7+
### Asciinema even
8+
[![asciicast]( https://asciinema.org/a/wRnu8Sf53kCCvUfgqQixs0EPh.svg )( https://asciinema.org/a/wRnu8Sf53kCCvUfgqQixs0EPh )
9+
10+
### Asciinema calc
11+
[![asciicast]( https://asciinema.org/a/S9Bly86EkmrpnTJhsAJU8pQDl.svg )( https://asciinema.org/a/S9Bly86EkmrpnTJhsAJU8pQDl )
12+
13+
### Asciinema gcd
14+
[![asciicast]( https://asciinema.org/a/L5FDJeBgVySllsh5oELOwTtKa.svg )( https://asciinema.org/a/L5FDJeBgVySllsh5oELOwTtKa )
15+
16+
### Asciinema progression
17+
[![asciicast]( https://asciinema.org/a/geOtpvl2IneSE65iIyp3BSoGY.svg )( https://asciinema.org/a/geOtpvl2IneSE65iIyp3BSoGY ))
18+
19+
### Asciinema prime
20+
[![asciicast]( https://asciinema.org/a/j2PgQoMiKCmVM1duYVp8pnN3i.svg )( https://asciinema.org/a/j2PgQoMiKCmVM1duYVp8pnN3i )
21+
122
### Brain-games
223
The game "Brain-games" includes 5 games.
324
To win each game, you need to give 3 correct answers.
@@ -25,12 +46,6 @@ You need to answer "yes" if given number is prime. Otherwise answer "no".
2546
* brain-progression
2647
* brain-prime
2748

28-
### Hexlet tests and linter status:
29-
[![Actions Status](https://github.com/s-gala/python-project-49/actions/workflows/hexlet-check.yml/badge.svg)](https://github.com/s-gala/python-project-49/actions)
30-
31-
### CodeClimate
32-
[![Maintainability](https://api.codeclimate.com/v1/badges/487e17fe16141caa4b2a/maintainability)](https://codeclimate.com/github/s-gala/python-project-49/maintainability)
33-
3449
### This project use:
3550
* Python >= 3.10
3651
* prompt >= 0.4.1
@@ -46,18 +61,3 @@ You need to answer "yes" if given number is prime. Otherwise answer "no".
4661
* cd pyton-project-49
4762
* cd ../
4863
* rm -rf python-project-49
49-
50-
### Asciinema even
51-
[![asciicast]( https://asciinema.org/a/wRnu8Sf53kCCvUfgqQixs0EPh )
52-
53-
### Asciinema calc
54-
[![asciicast]( https://asciinema.org/a/S9Bly86EkmrpnTJhsAJU8pQDl )
55-
56-
### Asciinema gcd
57-
[![asciicast]( https://asciinema.org/a/L5FDJeBgVySllsh5oELOwTtKa )
58-
59-
### Asciinema progression
60-
[![asciicast]( https://asciinema.org/a/geOtpvl2IneSE65iIyp3BSoGY )
61-
62-
### Asciinema prime
63-
[![asciicast]( https://asciinema.org/a/j2PgQoMiKCmVM1duYVp8pnN3i )
File renamed without changes.

brain_games/engine.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import prompt
22

3+
NUMBER_OF_ROUNDS = 3
34

4-
def game_engine(game):
5+
6+
def game_engine(INSTRUCTION, game):
57

68
name = prompt.string('Welcome to the Brain Games!\nMay I have your name? ')
7-
print(f'Hello, {name}!')
8-
print(game.INSTRUCTION)
9-
for _ in range(3):
10-
random_question, right_answer = game.get_question_answer()
9+
print(f'Hello, {name}!\n{INSTRUCTION}')
10+
for _ in range(NUMBER_OF_ROUNDS):
11+
random_question, right_answer = game()
1112
print(f"Question: {random_question}")
1213
answer = prompt.string('Your answer: ')
1314
if right_answer == answer:
@@ -17,4 +18,4 @@ def game_engine(game):
1718
print(f"Correct answer was '{right_answer}'.")
1819
print(f"Let's try again, {name}!")
1920
return
20-
return print(f"Congratulations, {name}!")
21+
print(f"Congratulations, {name}!")

brain_games/games/calc.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1-
from brain_games import utils
2-
from brain_games.games import consts
1+
import random
2+
3+
from brain_games import consts
4+
from brain_games.engine import game_engine
5+
from brain_games.utils import get_random_number
36

47
INSTRUCTION = consts.INSTRUCTION_CALC
58

69

710
def get_question_answer():
8-
x, y = utils.get_random_number(1, 100), utils.get_random_number(1, 100)
9-
sign, operator_metod = utils.get_random_operator()
10-
random_question = f'{x}{sign}{y}'
11-
right_answer = str(operator_metod(x, y))
11+
first_num, second_num = get_random_number(1, 100), get_random_number(1, 100)
12+
math_operation = [('+', first_num + second_num),
13+
('-', first_num - second_num),
14+
('*', first_num * second_num),
15+
]
16+
random_math_operation = random.choice(math_operation)
17+
random_question = f'{first_num}{random_math_operation[0]}{second_num}'
18+
right_answer = str(random_math_operation[1])
1219
return random_question, right_answer
20+
21+
22+
def start_calc():
23+
return game_engine(INSTRUCTION, get_question_answer)

brain_games/games/even.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
from brain_games.games import consts
1+
from brain_games import consts
2+
from brain_games.engine import game_engine
3+
from brain_games.utils import get_random_number
24

35
INSTRUCTION = consts.INSTRUCTION_EVEN
46

57

68
def get_question_answer():
7-
from brain_games import utils
8-
random_question = utils.get_random_number(1, 100)
9-
right_answer = 'yes' if random_question % 2 == 0 else 'no'
9+
random_question = get_random_number(1, 100)
10+
right_answer = 'yes' if is_even(random_question) else 'no'
1011
return random_question, right_answer
12+
13+
14+
def is_even(num):
15+
return num % 2 == 0
16+
17+
18+
def start_even():
19+
return game_engine(INSTRUCTION, get_question_answer)

brain_games/games/gcd.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import math
22

3-
from brain_games.games import consts
3+
from brain_games import consts
4+
from brain_games.engine import game_engine
5+
from brain_games.utils import get_random_number
46

57
INSTRUCTION = consts.INSTRUCTION_GCD
68

79

810
def get_question_answer():
9-
from brain_games import utils
10-
x = utils.get_random_number(1, 100)
11-
y = utils.get_random_number(1, 100)
12-
random_question = f'{x} {y}'
13-
right_answer = str(math.gcd(x, y))
11+
first_num, second_num = get_random_number(1, 100), get_random_number(1, 100)
12+
random_question = f'{first_num} {second_num}'
13+
right_answer = str(math.gcd(first_num, second_num))
1414
return random_question, right_answer
15+
16+
17+
def start_gcd():
18+
return game_engine(INSTRUCTION, get_question_answer)

brain_games/games/prime.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1-
from brain_games.games import consts
1+
from brain_games import consts
2+
from brain_games.engine import game_engine
3+
from brain_games.utils import get_random_number
24

35
INSTRUCTION = consts.INSTRUCTION_PRIME
46

57

68
def get_question_answer():
7-
from brain_games import utils
8-
random_question = utils.get_random_number(1, 100)
9-
k = 0
10-
for i in range(2, random_question // 2 + 1):
11-
if (random_question % i == 0):
12-
k = k + 1
13-
right_answer = 'yes' if (k <= 0) else 'no'
9+
random_question = get_random_number(1, 100)
10+
right_answer = 'yes' if is_prime(random_question) else 'no'
1411
return random_question, right_answer
12+
13+
14+
def is_prime(num):
15+
acc = 0
16+
for i in range(2, int(num ** (0.5))):
17+
if (num % i == 0):
18+
acc = acc + 1
19+
return acc == 0
20+
21+
22+
def start_prime():
23+
return game_engine(INSTRUCTION, get_question_answer)

brain_games/games/progression.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
from brain_games import utils
2-
from brain_games.games import consts
1+
from brain_games import consts
2+
from brain_games.engine import game_engine
3+
from brain_games.utils import get_random_number
34

45
INSTRUCTION = consts.INSTRUCTION_PROGRESSION
56

67

78
def get_question_answer():
8-
x = utils.get_random_number(5, 11)
9-
begin = utils.get_random_number(1, 40)
10-
step = utils.get_random_number(1, int((100 - begin) / x - 1))
11-
progression = (list(range(begin, 100, step)[:x]))
12-
i = utils.get_random_number(0, len(progression) - 1)
13-
right_answer = str(progression[i])
14-
progression[i] = '..'
15-
random_question = ''
16-
for item in progression:
17-
random_question = random_question + ' ' + str(item)
9+
length_progression = get_random_number(5, 11)
10+
begin = get_random_number(1, 40)
11+
step = get_random_number(1, int((100 - begin) / length_progression - 1))
12+
progression = (list(range(begin, 100, step)[:length_progression]))
13+
index_hidden_num = get_random_number(0, len(progression) - 1)
14+
right_answer = str(progression[index_hidden_num])
15+
progression[index_hidden_num] = '..'
16+
random_question = ' '.join(str(num) for num in progression)
1817
return random_question, right_answer
18+
19+
20+
def start_progression():
21+
return game_engine(INSTRUCTION, get_question_answer)
-47 Bytes
Binary file not shown.
-71 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)