Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
moritzkoerber committed Jul 3, 2021
0 parents commit c45a145
Show file tree
Hide file tree
Showing 15 changed files with 191 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[flake8]
max-line-length = 80
select = C,E,F,W,B,B950
ignore = E203, E501, W503
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__
dist/
*.egg-info/
4 changes: 4 additions & 0 deletions .isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[settings]
multi_line_output=3
line_length=88
include_trailing_comma=True
21 changes: 21 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
repos:
- repo: https://github.com/psf/black
rev: 21.5b1
hooks:
- id: black

- repo: https://github.com/pycqa/isort
rev: 5.8.0
hooks:
- id: isort

- repo: https://github.com/humitos/mirrors-autoflake
rev: v1.1
hooks:
- id: autoflake
args: ['--in-place', '--remove-all-unused-imports', '--remove-unused-variable']

- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.9
hooks:
- id: flake8
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Moritz Körber

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 6 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include pyproject.toml
include LICENSE
include setup.py

# Include the README
include *.md
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# binary4fun

binary4fun is a small game written in Python that can be played in the cli. The game basically tries to guess a number between 1 and 100 by binary search.

## Installation
### pip

You can install binary4fun from PyPI using pip:
```
pip install binary4fun
```

## Usage

To start the game, run the following in the command line:
```
python3 -m binary4fun
```

## Release History

* 0.1.0: First release

The package on PyPI: https://pypi.org/project/binary4fun

Distributed under the MIT license. See ``LICENSE`` for more information.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools>=40.8.0", "wheel"]
build-backend = "setuptools.build_meta"
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[metadata]
license_files = LICENSE
34 changes: 34 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pathlib

from setuptools import find_packages, setup

README = (pathlib.Path(__file__).parent / "README.md").read_text()


def get_version(rel_path: str):
file_path = pathlib.Path(__file__).parent.absolute() / rel_path
for line in file_path.read_text().splitlines():
if line.startswith("__version__"):
return line.split('"')[1]
raise RuntimeError("Unable to find version string.")


setup(
name="binary4fun",
version=get_version("src/binary4fun/__init__.py"),
author="Moritz Körber",
author_email="[email protected]",
description="binary4fun is a small game, which tries to guess a number between 1 and 100 by binary search.",
long_description=README,
long_description_content_type="text/markdown",
url="https://github.com/moritzkoerber/binary4fun",
keywords="game, binary search",
classifiers=[
"License :: OSI Approved :: MIT License",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
],
package_dir={"": "src"},
packages=find_packages(where="src"),
python_requires=">=3",
)
1 change: 1 addition & 0 deletions src/binary4fun/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.1.0"
53 changes: 53 additions & 0 deletions src/binary4fun/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from .utils.clear_screen import clear_screen


def main():
"""
Runs the game.
"""

clear_screen()
input("Welcome to binary4fun! Please press 'Enter' to start playing!")
input(
"Cool, let's go! Please think of a number between 1 and 100. Please press 'Enter' if you have one."
)
clear_screen()
print("I will now try to guess the number. I bet I can do it in within 7 guesses!")

current_ll, current_ul, current_guess, number_o_guesses = 1, 100, (1 + 100) // 2, 1

while (
input(f"Is your number {current_guess}? Please answer 'y' (yes) or 'n' (no):\n")
) != "y":
number_o_guesses += 1
clear_screen()
direction = input(
"Is the number higher or lower? Please answer 'h' (higher) or 'l' (lower):\n"
)
clear_screen()
if direction == "l":
current_ul = current_guess - 1
elif direction == "h":
current_ll = current_guess + 1
if current_ll == current_ul:
print(f"Okay, I must be {current_ll} then!\n")
break
current_guess = (current_ll + current_ul) // 2
else:
clear_screen()
print(
f"Cool! :) That took only {number_o_guesses} guess{'es' if number_o_guesses > 1 else ''} – which is within 7 guesses!"
)
if (
input(
"Thank you for playing! Do you want to play again? Please answer 'y' (yes) or 'n' (no).\n"
)
== "y"
):
main()
clear_screen()
print("Okay, goodbye!")


if __name__ == "__main__":
main()
Empty file.
8 changes: 8 additions & 0 deletions src/binary4fun/utils/clear_screen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os


def clear_screen():
"""
Clears the output on Windows and Unix systems.
"""
os.system("cls||clear")
5 changes: 5 additions & 0 deletions tests/test_version_return.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sh


def test_should_return_version():
assert sh.python(["setup.py", "--version"])

0 comments on commit c45a145

Please sign in to comment.