Skip to content

The training project "Brain Games" on the Python Development course on Hexlet.io

Notifications You must be signed in to change notification settings

IgorGakhov/Brain-Games-py

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

50 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Brain Games


The training project "Brain Games" on the Python Development course on Hexlet.io.

Actions Status Pylinter-on-push Maintainability

Built With

Languages, frameworks and libraries used in the implementation of the project:

Dependencies

List of dependencies, without which the project code will not work correctly:

  • python = "^3.9"
  • prompt = "^0.4.1"

Description

"Brain Games" is a set of five console games based on the popular mobile brain-pumping apps. Each game asks questions that need to be answered correctly. After three correct answers, the game is considered to be completed. Wrong answers end the game and offer to play it again. Games:

Games:

  • Brain Even - Answer "yes" if the number is even, otherwise answer "no"
  • Brain Calculator - Answer what is the result of the expression?
  • Brain GCD - Answer what is the greatest common divisor of given numbers.
  • Brain Progression - Answer what number is missing in the progression?
  • Brain Prime - Answer "yes" if given number is prime, otherwise answer "no".

You can call them with simple commands:

>> brain-even
>> brain-calc
>> brain-gcd
>> brain-progression
>> brain-prime

The first project is simultaneously the first full-fledged program outside of the Hexlet Environment. It introduces the basic steps needed to start any new project: installing the language (interpreter), setting up the environment (operating system, editor, linters), connecting additional libraries, creating a git repository. At this stage begins the in-depth work with the terminal. You will be introduced to Poetry, the utility for project management: installing and upgrading additional libraries, publishing packages, and much more. Here also comes the formation of the right engineering culture. One of the first tasks in the setup is to connect a linter (flake8), which automatically monitors the code style and finds potential errors. Another powerful element of real-world development is continuous integration (CI). Such systems are an integral part of any professional development. In Hexlet projects, continuous integration is connected to every project. Among the many systems, Github Actions is chosen as a free and Github-integrated build system.

The main issue in the project is the architecture. The architecture relies on basic principles of code organization: isolating side-effects, creating the right abstraction barriers (high modularity). A lot of questions arise here: "who is responsible for what?", "who interacts with the user?" "how does the game run?" and more. The architecture is a lot of work to do, even if you have some real development experience.

Summary


Installation

Python

Before installing the package, you need to make sure that you have Python version 3.9 or higher installed:

# Windows, Ubuntu, MacOS:
>> python --version # or python -V
Python 3.9.0+

โš ๏ธ If a command without a version does not work, specify the Python version explicitly: python3 --version.

If you have an older version installed, update with the following commands:

# Windows:
>> pip install python --upgrade
# Ubuntu:
>> sudo apt-get upgrade python3.X
# MacOS:
>> brew update && brew upgrade python
# * X - version number to be installed

If you don't have Python installed, you can download and install it from the official Python website. If you are an Ubuntu or MacOS user, then it is better to do this procedure through package managers. Open a terminal and run the command for your operating system:

# Ubuntu:
>> sudo apt update
>> sudo apt install python3.X
# MacOS:
# https://brew.sh/index_ru.html
>> brew install python3.X
# * X - version number to be installed

โ— The configuration of assemblies of different versions of operating systems can vary greatly from each other, which makes it impossible to write a common instruction. If you're running an OS other than the above, or you're having errors after the suggested commands, search Stack Overflow for answers, maybe someone else has come across them before you! Setting up the environment is not easy! ๐Ÿ™‚

Poetry

The project uses the Poetry manager. Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. You can read more about this tool on the official Poetry website.

Poetry provides a custom installer that will install poetry isolated from the rest of your system by vendorizing its dependencies. This is the recommended way of installing poetry.

# Windows (WSL), Linux, MacOS:
>> curl -sSL https://install.python-poetry.org | python3 -
# Windows (Powershell):
>> (Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py -
# If you have installed Python through the Microsoft Store, replace "py" with "python" in the command above.

โš ๏ธ On some systems, python may still refer to Python 2 instead of Python 3. The Poetry Team suggests a python3 binary to avoid ambiguity.

โš ๏ธ By default, Poetry is installed into a platform and user-specific directory:

  • ~/Library/Application Support/pypoetry on MacOS.
  • ~/.local/share/pypoetry on Linux/Unix.
  • %APPDATA%\pypoetry on Windows.

If you wish to change this, you may define the $POETRY_HOME environment variable:

>> curl -sSL https://install.python-poetry.org | POETRY_HOME=/etc/poetry python3 -

Add Poetry to your PATH.

Once Poetry is installed and in your $PATH, you can execute the following:

>> poetry --version

Project package

To work with the package, you need to clone the repository to your computer. This is done using the git clone command. Clone the project on the command line:

# clone via HTTPS:
>> git clone https://github.com/IgorGakhov/python-project-lvl1.git
# clone via SSH:
>> git clone [email protected]:IgorGakhov/python-project-lvl1.git

It remains to move to the directory and install the package:

>> cd python-project-lvl1
>> poetry build
>> python3 -m pip install --user dist/*.whl
# If you have previously installed a package and want to update it, use the following command:
# >> python3 -m pip install --user --force-reinstall dist/*.whl

On *nix-like systems you can do this:

asciicast

Finally, we can move on to using the project functionality!


Usage

Demo

๐ŸŽฎ Brain Even:

brain-even

The user is shown a random number. And he has to answer yes if the number is even, or no if it is odd:

Examples:

Answer "yes" if the number is even, otherwise answer "no".
Question: 15
>> Your answer: yes
'yes' is wrong answer ;(. Correct answer was 'no'.

Answer "yes" if the number is even, otherwise answer "no".
Question: 15
>> Your answer: no
Correct!

asciicast

๐ŸŽฎ Brain Calculator:

brain-calc

The user is shown a random mathematical expression, such as 87 + 48, which must be calculated and write down the correct answer.

Examples:

What is the result of the expression?
Question: 87 + 48
>> Your answer: 126
'126' is wrong answer ;(. Correct answer was '135'.

What is the result of the expression?
Question: 87 + 48
>> Your answer: 135
Correct!

asciicast

๐ŸŽฎ Brain GCD:

brain-gcd

The user is shown two random numbers, for example 25 and 50. The user must calculate and enter the greatest common divisor of these numbers.

Examples:

Find the greatest common divisor of given numbers.
Question: 25 50
>> Your answer: 5
'5' is wrong answer ;(. Correct answer was '25'.

Find the greatest common divisor of given numbers.
Question: 25 50
>> Your answer: 25
Correct!

asciicast

๐ŸŽฎ Brain Progression:

brain-progression

The user is shown a series of numbers with a missing number, forming an arithmetic progression. The player has to determine this number.

Examples:

What number is missing in the progression?
Question: 3 26 .. 72 95 118 141 164 187
>> Your answer: 43
'43' is wrong answer ;(. Correct answer was '49'.

What number is missing in the progression?
Question: 3 26 .. 72 95 118 141 164 187
>> Your answer: 49
Correct!

asciicast

๐ŸŽฎ Brain Prime:

brain-prime

The user is shown a random number. And he needs to answer yes if the number is prime, or no if it is composite:

Examples:

Answer "yes" if given number is prime. Otherwise answer "no".
Question: 67
>> Your answer: no
'no' is wrong answer ;(. Correct answer was 'yes'.

Answer "yes" if given number is prime. Otherwise answer "no".
Question: 67
>> Your answer: yes
Correct!

asciicast


Development

Dev Dependencies

List of dev-dependencies:

  • flake8 = "^4.0.1"

Project Organization

.
โ”œโ”€โ”€ brain_games
โ”‚ย ย  โ”œโ”€โ”€ __init__.py
โ”‚ย ย  โ”œโ”€โ”€ cli.py
โ”‚ย ย  โ”œโ”€โ”€ engine
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ __init__.py
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ game_engine.py
โ”‚ย ย  โ”œโ”€โ”€ games
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ __init__.py
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ calc.py
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ even.py
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ gcd.py
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ progression.py
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ prime.py
โ”‚ย ย  โ””โ”€โ”€ scripts
โ”‚ย ย      โ”œโ”€โ”€ __init__.py
โ”‚ย ย      โ”œโ”€โ”€ brain_games.py
โ”‚ย ย      โ”œโ”€โ”€ brain_calc.py
โ”‚ย ย      โ”œโ”€โ”€ brain_even.py
โ”‚ย ย      โ”œโ”€โ”€ brain_gcd.py
โ”‚ย ย      โ”œโ”€โ”€ brain_progression.py
โ”‚ย ย      โ””โ”€โ”€ brain_prime.py
โ”œโ”€โ”€ Makefile
โ”œโ”€โ”€ poetry.lock
โ”œโ”€โ”€ pyproject.toml
โ”œโ”€โ”€ README.md
โ””โ”€โ”€ setup.cfg

Useful commands

The commands most used in development are listed in the Makefile:

make package-install
Installing a package in the user environment.
make build
Building the distribution of he Poetry package.
make package-force-reinstall
Reinstalling the package in the user environment.
make lint
Checking code with linter.

Thank you for attention!

๐Ÿ‘จโ€๐Ÿ’ป Author: @IgorGakhov