diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 5b930bf..721bfb1 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -1,3 +1,20 @@ -name: lab-0 +name: code quality tests -# add your pipline description below \ No newline at end of file +on: push + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v4 + with: + python-version: '3.11.6' + - name: Run image + uses: abatilo/actions-poetry@v2 + with: + poetry-version: '1.7.1' + - name: Install dependencies + run: poetry install; poetry add pytest + - name: Run pytest + run: poetry run pytest . \ No newline at end of file diff --git a/.gitignore b/.gitignore index 68bc17f..71bb0b0 100644 --- a/.gitignore +++ b/.gitignore @@ -36,7 +36,7 @@ MANIFEST pip-log.txt pip-delete-this-directory.txt -# Unit test / coverage reports +# Unit tests / coverage reports htmlcov/ .tox/ .nox/ diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/README.md b/README.md index 9035c52..a352bea 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ The workflow should contain the basic components. Specify when the workflow should run (on push or pull request), outline the steps to set up the environment, and a command to run `pytest`. This GitHub Actions workflow ensures automated testing for code quality and reliability. ```YAML -name: code quality test +name: code quality tests on: push @@ -136,3 +136,4 @@ jobs: # Hint Look in the [tutorial](https://testdriven.io/blog/python-environments/) +# tempi diff --git a/lab0maksktl/__init__.py b/lab0maksktl/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lab0maksktl/hangman.py b/lab0maksktl/hangman.py new file mode 100644 index 0000000..a167bc6 --- /dev/null +++ b/lab0maksktl/hangman.py @@ -0,0 +1,188 @@ +# Console based hangman game. + +import random +import os + +# Constants +WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone", "hangman", + "computer", "science", "programming", "mathematics", "player", "condition", + "reverse", "water", "board", "geeks", "keyboard", "laptop", "headphone", + "mouse", "printer", "scanner", "software", "hardware", "network", "server") + +HANGMAN_STAGES = ( +""" + ------- + |/ | + | + | + | + | + | + /|\\ +----------- +""",""" + ------- + |/ | + | O + | + | + | + | + /|\\ +----------- +""",""" + ------- + |/ | + | O + | | + | + | + | + /|\\ +----------- +""",""" + ------- + |/ | + | O + | | + | | + | + | + /|\\ +----------- +""",""" + ------- + |/ | + | O + | /| + | | + | + | + /|\\ +----------- +""",""" + ------- + |/ | + | O + | /|\\ + | | + | + | + /|\\ +----------- +""",""" + ------- + |/ | + | O + | /|\\ + | | + | / + | + /|\\ +----------- +""",""" + ------- + |/ | + | O + | /|\\ + | | + | / \\ + | + /|\\ +----------- +""") + +# Functions +def clear_screen(): + """Clears the screen.""" + os.system("cls" if os.name == "nt" else "clear") + +def get_random_word(): + """Returns a random word from the WORDS tuple.""" + return random.choice(WORDS) + +def splash_screen(): + """The splash screen.""" + print(""" + _ _ ----- + | | | | __ _ _ __ __ _ _ __ ___ __ _ _ __ | o + | |_| |/ _` | '_ \\ / _` | '_ ` _ \\ / _` | '_ \\ | /|\\ + | _ | (_| | | | | (_| | | | | | | (_| | | | | | / \\ + |_| |_|\\__,_|_| |_|\\__, |_| |_| |_|\\__,_|_| |_| | + |___/ v1.0 + """) + input("Press enter to continue...") + clear_screen() + + +def hangman(): + """The hangman game.""" + try: + # game outer loop + while True: + # Setup + word = get_random_word() + guessed_letters = [] + guessed_word = ["_"] * len(word) + wrong_guesses = 0 + + # game inner loop + while True: + clear_screen() + print("Hangman game. Try to guess the word. (CTRL+C to quit)") + print(HANGMAN_STAGES[wrong_guesses]) + print(" ".join(guessed_word), end=' ') + print("(Guessed letters: " + ", ".join(guessed_letters),")") + print() + + # check if player has won + if "_" not in guessed_word: + print("You win!") + break + + # check if player has lost + if wrong_guesses == len(HANGMAN_STAGES)-1: + print("You lose!") + break + + # make sure player enters a single letter + while True: + guess = input("Guess a letter: ").lower() + if len(guess) == 1 and guess.isalpha(): + # check if letter has already been guessed + if guess in guessed_letters: + print("You have already guessed that letter.") + else: + break + else: + print("Invalid guess. Please enter a single letter.") + + # add guess to guessed letters + guessed_letters.append(guess) + + # check if guess is in word + if guess in word: + # add guess to guessed word + for i in range(len(word)): + if word[i] == guess: + guessed_word[i] = guess + else: + # increment wrong guesses + wrong_guesses += 1 + + # Ask the player if they want to play again + play_again = input("Play again? (y/n): ").lower() + if play_again != "y": + break + + print("\nGoodbye!") + + except KeyboardInterrupt: + print("\nGoodbye!") + exit() + + +# Main +if __name__ == "__main__": + splash_screen() + hangman() diff --git a/poetry.lock b/poetry.lock new file mode 100644 index 0000000..fc34378 --- /dev/null +++ b/poetry.lock @@ -0,0 +1,74 @@ +# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand. + +[[package]] +name = "colorama" +version = "0.4.6" +description = "Cross-platform colored terminal text." +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7" +files = [ + {file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"}, + {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, +] + +[[package]] +name = "iniconfig" +version = "2.0.0" +description = "brain-dead simple config-ini parsing" +optional = false +python-versions = ">=3.7" +files = [ + {file = "iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374"}, + {file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"}, +] + +[[package]] +name = "packaging" +version = "23.2" +description = "Core utilities for Python packages" +optional = false +python-versions = ">=3.7" +files = [ + {file = "packaging-23.2-py3-none-any.whl", hash = "sha256:8c491190033a9af7e1d931d0b5dacc2ef47509b34dd0de67ed209b5203fc88c7"}, + {file = "packaging-23.2.tar.gz", hash = "sha256:048fb0e9405036518eaaf48a55953c750c11e1a1b68e0dd1a9d62ed0c092cfc5"}, +] + +[[package]] +name = "pluggy" +version = "1.4.0" +description = "plugin and hook calling mechanisms for python" +optional = false +python-versions = ">=3.8" +files = [ + {file = "pluggy-1.4.0-py3-none-any.whl", hash = "sha256:7db9f7b503d67d1c5b95f59773ebb58a8c1c288129a88665838012cfb07b8981"}, + {file = "pluggy-1.4.0.tar.gz", hash = "sha256:8c85c2876142a764e5b7548e7d9a0e0ddb46f5185161049a79b7e974454223be"}, +] + +[package.extras] +dev = ["pre-commit", "tox"] +testing = ["pytest", "pytest-benchmark"] + +[[package]] +name = "pytest" +version = "8.0.0" +description = "pytest: simple powerful testing with Python" +optional = false +python-versions = ">=3.8" +files = [ + {file = "pytest-8.0.0-py3-none-any.whl", hash = "sha256:50fb9cbe836c3f20f0dfa99c565201fb75dc54c8d76373cd1bde06b06657bdb6"}, + {file = "pytest-8.0.0.tar.gz", hash = "sha256:249b1b0864530ba251b7438274c4d251c58d868edaaec8762893ad4a0d71c36c"}, +] + +[package.dependencies] +colorama = {version = "*", markers = "sys_platform == \"win32\""} +iniconfig = "*" +packaging = "*" +pluggy = ">=1.3.0,<2.0" + +[package.extras] +testing = ["argcomplete", "attrs (>=19.2.0)", "hypothesis (>=3.56)", "mock", "nose", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"] + +[metadata] +lock-version = "2.0" +python-versions = "^3.11" +content-hash = "b439de7b0c1dcc9846389b7d06c9d856f9f0b7eedcfdf44f2c335d4b4aed0ed1" diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..eb01c89 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,15 @@ +[tool.poetry] +name = "lab0-maksktl" +version = "0.1.0" +description = "" +authors = ["maksktl "] +readme = "README.md" + +[tool.poetry.dependencies] +python = "^3.11" +pytest = "^8.0.0" + + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_hangman.py b/tests/test_hangman.py new file mode 100644 index 0000000..c838029 --- /dev/null +++ b/tests/test_hangman.py @@ -0,0 +1,16 @@ +from lab0maksktl.hangman import get_random_word + + +def test_hangman(): + WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone", "hangman", + "computer", "science", "programming", "mathematics", "player", "condition", + "reverse", "water", "board", "geeks", "keyboard", "laptop", "headphone", + "mouse", "printer", "scanner", "software", "hardware", "network", "server") + assert find_in_word_list(get_random_word(), WORDS) + + +def find_in_word_list(word, list_of_words) -> bool: + for list_part in list_of_words: + if word == list_part: + return True + return False