From c45a1451800329a2a9fce54b44b638aa0b9fbc28 Mon Sep 17 00:00:00 2001 From: moritzkoerber Date: Wed, 19 May 2021 22:25:04 +0200 Subject: [PATCH] initial commit --- .flake8 | 4 +++ .gitignore | 3 ++ .isort.cfg | 4 +++ .pre-commit-config.yaml | 21 +++++++++++ LICENSE | 21 +++++++++++ MANIFEST.in | 6 ++++ README.md | 26 ++++++++++++++ pyproject.toml | 3 ++ setup.cfg | 2 ++ setup.py | 34 ++++++++++++++++++ src/binary4fun/__init__.py | 1 + src/binary4fun/__main__.py | 53 ++++++++++++++++++++++++++++ src/binary4fun/utils/__init__.py | 0 src/binary4fun/utils/clear_screen.py | 8 +++++ tests/test_version_return.py | 5 +++ 15 files changed, 191 insertions(+) create mode 100644 .flake8 create mode 100644 .gitignore create mode 100644 .isort.cfg create mode 100644 .pre-commit-config.yaml create mode 100644 LICENSE create mode 100644 MANIFEST.in create mode 100644 README.md create mode 100644 pyproject.toml create mode 100644 setup.cfg create mode 100644 setup.py create mode 100644 src/binary4fun/__init__.py create mode 100755 src/binary4fun/__main__.py create mode 100644 src/binary4fun/utils/__init__.py create mode 100644 src/binary4fun/utils/clear_screen.py create mode 100644 tests/test_version_return.py diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..5376b8a --- /dev/null +++ b/.flake8 @@ -0,0 +1,4 @@ +[flake8] +max-line-length = 80 +select = C,E,F,W,B,B950 +ignore = E203, E501, W503 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bb66b28 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__ +dist/ +*.egg-info/ \ No newline at end of file diff --git a/.isort.cfg b/.isort.cfg new file mode 100644 index 0000000..ae4e081 --- /dev/null +++ b/.isort.cfg @@ -0,0 +1,4 @@ +[settings] +multi_line_output=3 +line_length=88 +include_trailing_comma=True diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..9162a8f --- /dev/null +++ b/.pre-commit-config.yaml @@ -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 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d21d0f2 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..f263105 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,6 @@ +include pyproject.toml +include LICENSE +include setup.py + +# Include the README +include *.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..a5ca872 --- /dev/null +++ b/README.md @@ -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. \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..2f21011 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["setuptools>=40.8.0", "wheel"] +build-backend = "setuptools.build_meta" diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..0792c86 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[metadata] +license_files = LICENSE \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..3ebb7cb --- /dev/null +++ b/setup.py @@ -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="moritz.koerber@gmail.com", + 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", +) diff --git a/src/binary4fun/__init__.py b/src/binary4fun/__init__.py new file mode 100644 index 0000000..3dc1f76 --- /dev/null +++ b/src/binary4fun/__init__.py @@ -0,0 +1 @@ +__version__ = "0.1.0" diff --git a/src/binary4fun/__main__.py b/src/binary4fun/__main__.py new file mode 100755 index 0000000..4d144c2 --- /dev/null +++ b/src/binary4fun/__main__.py @@ -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() diff --git a/src/binary4fun/utils/__init__.py b/src/binary4fun/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/binary4fun/utils/clear_screen.py b/src/binary4fun/utils/clear_screen.py new file mode 100644 index 0000000..abbb55e --- /dev/null +++ b/src/binary4fun/utils/clear_screen.py @@ -0,0 +1,8 @@ +import os + + +def clear_screen(): + """ + Clears the output on Windows and Unix systems. + """ + os.system("cls||clear") diff --git a/tests/test_version_return.py b/tests/test_version_return.py new file mode 100644 index 0000000..4a5d466 --- /dev/null +++ b/tests/test_version_return.py @@ -0,0 +1,5 @@ +import sh + + +def test_should_return_version(): + assert sh.python(["setup.py", "--version"])