hangman-words 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hangman_words-0.1.0/LICENSE +21 -0
- hangman_words-0.1.0/MANIFEST.in +2 -0
- hangman_words-0.1.0/PKG-INFO +72 -0
- hangman_words-0.1.0/README.md +55 -0
- hangman_words-0.1.0/pyproject.toml +40 -0
- hangman_words-0.1.0/setup.cfg +4 -0
- hangman_words-0.1.0/src/hangman/__init__.py +15 -0
- hangman_words-0.1.0/src/hangman/__main__.py +47 -0
- hangman_words-0.1.0/src/hangman/data/words.json +79 -0
- hangman_words-0.1.0/src/hangman/game.py +113 -0
- hangman_words-0.1.0/src/hangman/words.py +50 -0
- hangman_words-0.1.0/src/hangman_words.egg-info/PKG-INFO +72 -0
- hangman_words-0.1.0/src/hangman_words.egg-info/SOURCES.txt +15 -0
- hangman_words-0.1.0/src/hangman_words.egg-info/dependency_links.txt +1 -0
- hangman_words-0.1.0/src/hangman_words.egg-info/entry_points.txt +2 -0
- hangman_words-0.1.0/src/hangman_words.egg-info/top_level.txt +1 -0
- hangman_words-0.1.0/tests/test_game.py +41 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Hangman Words Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: hangman-words
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A dependency-free Hangman game with a built-in word and meaning bank.
|
|
5
|
+
Author: Hangman Words Contributors
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Keywords: hangman,word-game,vocabulary,education
|
|
8
|
+
Classifier: Development Status :: 4 - Beta
|
|
9
|
+
Classifier: Intended Audience :: Education
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
12
|
+
Classifier: Topic :: Games/Entertainment
|
|
13
|
+
Requires-Python: >=3.10
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
Dynamic: license-file
|
|
17
|
+
|
|
18
|
+
# Hangman Words
|
|
19
|
+
|
|
20
|
+
A small, dependency-free Python package for playing Hangman with a prebuilt vocabulary and learner-friendly word meanings.
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
python -m pip install hangman-words
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Python API
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
from hangman import HangmanGame, default_word_bank
|
|
32
|
+
|
|
33
|
+
bank = default_word_bank()
|
|
34
|
+
entry = bank.random(category="science")
|
|
35
|
+
game = HangmanGame(entry)
|
|
36
|
+
|
|
37
|
+
result = game.guess("a")
|
|
38
|
+
print(result.masked_word)
|
|
39
|
+
print(entry.meaning)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The bundled word bank includes technology, science, travel, and vocabulary categories. `WordBank` also accepts your own `WordEntry` objects for custom games.
|
|
43
|
+
|
|
44
|
+
## Command line
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
hangman
|
|
48
|
+
hangman --category vocabulary --attempts 8
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Build and publish
|
|
52
|
+
|
|
53
|
+
Build both a wheel and source distribution with:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
python -m pip install --upgrade build twine
|
|
57
|
+
python -m build
|
|
58
|
+
python -m twine check dist/*
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Upload to TestPyPI first, then PyPI:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
python -m twine upload --repository testpypi dist/*
|
|
65
|
+
python -m twine upload dist/*
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Do not commit API tokens. Configure Twine credentials through its supported keyring or environment-variable workflow.
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
MIT
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Hangman Words
|
|
2
|
+
|
|
3
|
+
A small, dependency-free Python package for playing Hangman with a prebuilt vocabulary and learner-friendly word meanings.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
python -m pip install hangman-words
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Python API
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
from hangman import HangmanGame, default_word_bank
|
|
15
|
+
|
|
16
|
+
bank = default_word_bank()
|
|
17
|
+
entry = bank.random(category="science")
|
|
18
|
+
game = HangmanGame(entry)
|
|
19
|
+
|
|
20
|
+
result = game.guess("a")
|
|
21
|
+
print(result.masked_word)
|
|
22
|
+
print(entry.meaning)
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The bundled word bank includes technology, science, travel, and vocabulary categories. `WordBank` also accepts your own `WordEntry` objects for custom games.
|
|
26
|
+
|
|
27
|
+
## Command line
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
hangman
|
|
31
|
+
hangman --category vocabulary --attempts 8
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Build and publish
|
|
35
|
+
|
|
36
|
+
Build both a wheel and source distribution with:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
python -m pip install --upgrade build twine
|
|
40
|
+
python -m build
|
|
41
|
+
python -m twine check dist/*
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Upload to TestPyPI first, then PyPI:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
python -m twine upload --repository testpypi dist/*
|
|
48
|
+
python -m twine upload dist/*
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Do not commit API tokens. Configure Twine credentials through its supported keyring or environment-variable workflow.
|
|
52
|
+
|
|
53
|
+
## License
|
|
54
|
+
|
|
55
|
+
MIT
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "hangman-words"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A dependency-free Hangman game with a built-in word and meaning bank."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Hangman Words Contributors" },
|
|
14
|
+
]
|
|
15
|
+
keywords = ["hangman", "word-game", "vocabulary", "education"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Intended Audience :: Education",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
21
|
+
"Topic :: Games/Entertainment",
|
|
22
|
+
]
|
|
23
|
+
dependencies = []
|
|
24
|
+
|
|
25
|
+
[project.scripts]
|
|
26
|
+
hangman = "hangman.__main__:main"
|
|
27
|
+
|
|
28
|
+
[tool.setuptools]
|
|
29
|
+
package-dir = { "" = "src" }
|
|
30
|
+
include-package-data = true
|
|
31
|
+
|
|
32
|
+
[tool.setuptools.packages.find]
|
|
33
|
+
where = ["src"]
|
|
34
|
+
|
|
35
|
+
[tool.setuptools.package-data]
|
|
36
|
+
hangman = ["data/*.json"]
|
|
37
|
+
|
|
38
|
+
[tool.pytest.ini_options]
|
|
39
|
+
testpaths = ["tests"]
|
|
40
|
+
addopts = "-q"
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"""A small, dependency-free Hangman game with built-in vocabulary."""
|
|
2
|
+
|
|
3
|
+
from .game import GameStatus, GuessResult, HangmanGame, WordEntry
|
|
4
|
+
from .words import WordBank, default_word_bank
|
|
5
|
+
|
|
6
|
+
__all__ = [
|
|
7
|
+
"GameStatus",
|
|
8
|
+
"GuessResult",
|
|
9
|
+
"HangmanGame",
|
|
10
|
+
"WordBank",
|
|
11
|
+
"WordEntry",
|
|
12
|
+
"default_word_bank",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""Command-line interface for Hangman."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
|
|
7
|
+
from .game import GameStatus, HangmanGame
|
|
8
|
+
from .words import default_word_bank
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def main() -> None:
|
|
12
|
+
parser = argparse.ArgumentParser(description="Play a vocabulary-building game of Hangman.")
|
|
13
|
+
parser.add_argument("--category", help="limit the word to a category")
|
|
14
|
+
parser.add_argument("--attempts", type=int, default=6, help="number of incorrect guesses allowed")
|
|
15
|
+
args = parser.parse_args()
|
|
16
|
+
|
|
17
|
+
bank = default_word_bank()
|
|
18
|
+
try:
|
|
19
|
+
game = HangmanGame(bank.random(args.category), max_attempts=args.attempts)
|
|
20
|
+
except (LookupError, ValueError) as error:
|
|
21
|
+
parser.error(str(error))
|
|
22
|
+
|
|
23
|
+
print("Hangman - type 'quit' to exit")
|
|
24
|
+
while game.status is GameStatus.PLAYING:
|
|
25
|
+
print(f"\nWord: {game.masked_word} Attempts left: {game.remaining_attempts}")
|
|
26
|
+
guess = input("Guess a letter: ").strip()
|
|
27
|
+
if guess.lower() == "quit":
|
|
28
|
+
print(f"The word was {game.entry.word}: {game.entry.meaning}")
|
|
29
|
+
return
|
|
30
|
+
try:
|
|
31
|
+
result = game.guess(guess)
|
|
32
|
+
except ValueError as error:
|
|
33
|
+
print(error)
|
|
34
|
+
continue
|
|
35
|
+
if result.repeated:
|
|
36
|
+
print("You already guessed that letter.")
|
|
37
|
+
elif not result.correct:
|
|
38
|
+
print("Not in the word.")
|
|
39
|
+
|
|
40
|
+
if game.status is GameStatus.WON:
|
|
41
|
+
print(f"\nYou won! {game.entry.word}: {game.entry.meaning}")
|
|
42
|
+
else:
|
|
43
|
+
print(f"\nOut of attempts. The word was {game.entry.word}: {game.entry.meaning}")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
if __name__ == "__main__":
|
|
47
|
+
main()
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
[
|
|
2
|
+
{"word": "algorithm", "meaning": "A step-by-step method for solving a problem.", "category": "technology"},
|
|
3
|
+
{"word": "browser", "meaning": "Software used to view and navigate websites.", "category": "technology"},
|
|
4
|
+
{"word": "compiler", "meaning": "A program that translates source code into machine code.", "category": "technology"},
|
|
5
|
+
{"word": "database", "meaning": "An organized collection of information.", "category": "technology"},
|
|
6
|
+
{"word": "encryption", "meaning": "The process of protecting information by converting it into a code.", "category": "technology"},
|
|
7
|
+
{"word": "firewall", "meaning": "A security system that controls network traffic.", "category": "technology"},
|
|
8
|
+
{"word": "hardware", "meaning": "The physical parts of a computer or electronic system.", "category": "technology"},
|
|
9
|
+
{"word": "internet", "meaning": "A worldwide network connecting computers and devices.", "category": "technology"},
|
|
10
|
+
{"word": "keyboard", "meaning": "A set of keys used to enter text or commands.", "category": "technology"},
|
|
11
|
+
{"word": "pixel", "meaning": "The smallest unit of a digital image.", "category": "technology"},
|
|
12
|
+
{"word": "robotics", "meaning": "The study and design of machines that can perform tasks.", "category": "technology"},
|
|
13
|
+
{"word": "software", "meaning": "Programs and instructions that run on a computer.", "category": "technology"},
|
|
14
|
+
{"word": "galaxy", "meaning": "A huge system of stars, gas, and dust held together by gravity.", "category": "science"},
|
|
15
|
+
{"word": "gravity", "meaning": "The force that attracts objects toward one another.", "category": "science"},
|
|
16
|
+
{"word": "molecule", "meaning": "A group of atoms bonded together.", "category": "science"},
|
|
17
|
+
{"word": "volcano", "meaning": "An opening in Earth's crust through which lava can erupt.", "category": "science"},
|
|
18
|
+
{"word": "atom", "meaning": "The smallest unit of an element that keeps its properties.", "category": "science"},
|
|
19
|
+
{"word": "bacteria", "meaning": "Tiny living organisms made of a single cell.", "category": "science"},
|
|
20
|
+
{"word": "climate", "meaning": "The usual weather conditions of a place over time.", "category": "science"},
|
|
21
|
+
{"word": "ecosystem", "meaning": "A community of living things and their environment.", "category": "science"},
|
|
22
|
+
{"word": "fossil", "meaning": "The preserved remains or traces of ancient life.", "category": "science"},
|
|
23
|
+
{"word": "habitat", "meaning": "The natural home of a plant or animal.", "category": "science"},
|
|
24
|
+
{"word": "orbit", "meaning": "The curved path of an object around a planet or star.", "category": "science"},
|
|
25
|
+
{"word": "telescope", "meaning": "An instrument used to observe distant objects in space.", "category": "science"},
|
|
26
|
+
{"word": "compass", "meaning": "An instrument used to find direction.", "category": "travel"},
|
|
27
|
+
{"word": "journey", "meaning": "An act of traveling from one place to another.", "category": "travel"},
|
|
28
|
+
{"word": "lantern", "meaning": "A portable light protected by a transparent covering.", "category": "travel"},
|
|
29
|
+
{"word": "suitcase", "meaning": "A case used for carrying clothes and belongings while traveling.", "category": "travel"},
|
|
30
|
+
{"word": "airport", "meaning": "A place where aircraft take off and land.", "category": "travel"},
|
|
31
|
+
{"word": "backpack", "meaning": "A bag carried on the back with shoulder straps.", "category": "travel"},
|
|
32
|
+
{"word": "boarding", "meaning": "The act of getting onto a ship, train, or aircraft.", "category": "travel"},
|
|
33
|
+
{"word": "destination", "meaning": "The place to which someone or something is going.", "category": "travel"},
|
|
34
|
+
{"word": "itinerary", "meaning": "A planned route or schedule for a journey.", "category": "travel"},
|
|
35
|
+
{"word": "passport", "meaning": "An official document used to identify a traveler internationally.", "category": "travel"},
|
|
36
|
+
{"word": "souvenir", "meaning": "An object kept as a reminder of a place or event.", "category": "travel"},
|
|
37
|
+
{"word": "curious", "meaning": "Eager to learn or know something.", "category": "vocabulary"},
|
|
38
|
+
{"word": "diligent", "meaning": "Showing careful and persistent effort.", "category": "vocabulary"},
|
|
39
|
+
{"word": "elegant", "meaning": "Graceful and stylish in appearance or manner.", "category": "vocabulary"},
|
|
40
|
+
{"word": "resilient", "meaning": "Able to recover quickly from difficulty.", "category": "vocabulary"},
|
|
41
|
+
{"word": "abundant", "meaning": "Existing in large quantities; plentiful.", "category": "vocabulary"},
|
|
42
|
+
{"word": "brilliant", "meaning": "Very bright, impressive, or exceptionally clever.", "category": "vocabulary"},
|
|
43
|
+
{"word": "concise", "meaning": "Expressing something clearly in few words.", "category": "vocabulary"},
|
|
44
|
+
{"word": "generous", "meaning": "Willing to give or share freely.", "category": "vocabulary"},
|
|
45
|
+
{"word": "humble", "meaning": "Having a modest view of one's importance.", "category": "vocabulary"},
|
|
46
|
+
{"word": "precise", "meaning": "Exact and accurate.", "category": "vocabulary"},
|
|
47
|
+
{"word": "vivid", "meaning": "Producing strong, clear images in the mind.", "category": "vocabulary"},
|
|
48
|
+
{"word": "waterfall", "meaning": "Water falling over a steep edge in a stream or river.", "category": "nature"},
|
|
49
|
+
{"word": "meadow", "meaning": "A field of grass and wildflowers.", "category": "nature"},
|
|
50
|
+
{"word": "mountain", "meaning": "A large natural elevation of Earth's surface.", "category": "nature"},
|
|
51
|
+
{"word": "rainbow", "meaning": "An arc of colors formed when light passes through water drops.", "category": "nature"},
|
|
52
|
+
{"word": "sunflower", "meaning": "A tall plant with a large yellow flower.", "category": "nature"},
|
|
53
|
+
{"word": "thunder", "meaning": "The loud sound caused by lightning.", "category": "nature"},
|
|
54
|
+
{"word": "woodland", "meaning": "Land covered mainly by trees.", "category": "nature"},
|
|
55
|
+
{"word": "canvas", "meaning": "A strong cloth surface used for painting.", "category": "arts"},
|
|
56
|
+
{"word": "melody", "meaning": "A sequence of musical notes forming a recognizable tune.", "category": "arts"},
|
|
57
|
+
{"word": "palette", "meaning": "A range of colors used by an artist.", "category": "arts"},
|
|
58
|
+
{"word": "portrait", "meaning": "An artwork depicting a particular person.", "category": "arts"},
|
|
59
|
+
{"word": "sculpture", "meaning": "A three-dimensional work of art.", "category": "arts"},
|
|
60
|
+
{"word": "theater", "meaning": "A place where plays and performances are presented.", "category": "arts"},
|
|
61
|
+
{"word": "poetry", "meaning": "Literature that uses concentrated language and artistic rhythm.", "category": "arts"},
|
|
62
|
+
{"word": "rhythm", "meaning": "A regular pattern of beats or movements.", "category": "arts"},
|
|
63
|
+
{"word": "ancient", "meaning": "Belonging to a very distant time in history.", "category": "history"},
|
|
64
|
+
{"word": "empire", "meaning": "A group of territories ruled by one authority.", "category": "history"},
|
|
65
|
+
{"word": "heritage", "meaning": "Traditions, achievements, and objects passed down through generations.", "category": "history"},
|
|
66
|
+
{"word": "medieval", "meaning": "Relating to the period of European history between ancient and modern times.", "category": "history"},
|
|
67
|
+
{"word": "monument", "meaning": "A structure built to remember a person or event.", "category": "history"},
|
|
68
|
+
{"word": "pioneer", "meaning": "A person who is among the first to explore or develop something.", "category": "history"},
|
|
69
|
+
{"word": "revolution", "meaning": "A major and often sudden change in government or society.", "category": "history"},
|
|
70
|
+
{"word": "timeline", "meaning": "A display of events arranged in the order they happened.", "category": "history"},
|
|
71
|
+
{"word": "biscuit", "meaning": "A small baked food that is often crisp or lightly sweet.", "category": "food"},
|
|
72
|
+
{"word": "cinnamon", "meaning": "A fragrant spice made from tree bark.", "category": "food"},
|
|
73
|
+
{"word": "ingredient", "meaning": "One of the foods used to make a dish.", "category": "food"},
|
|
74
|
+
{"word": "noodle", "meaning": "A thin strip of dough cooked as food.", "category": "food"},
|
|
75
|
+
{"word": "pancake", "meaning": "A thin, flat cake cooked in a pan.", "category": "food"},
|
|
76
|
+
{"word": "sandwich", "meaning": "Food made by placing a filling between slices of bread.", "category": "food"},
|
|
77
|
+
{"word": "seasoning", "meaning": "An ingredient added to improve the flavor of food.", "category": "food"},
|
|
78
|
+
{"word": "vegetable", "meaning": "A plant or part of a plant eaten as food.", "category": "food"}
|
|
79
|
+
]
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"""Core Hangman game state and rules."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from enum import Enum
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class GameStatus(str, Enum):
|
|
10
|
+
"""Possible states of a Hangman game."""
|
|
11
|
+
|
|
12
|
+
PLAYING = "playing"
|
|
13
|
+
WON = "won"
|
|
14
|
+
LOST = "lost"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass(frozen=True, slots=True)
|
|
18
|
+
class WordEntry:
|
|
19
|
+
"""A playable word and its learner-friendly meaning."""
|
|
20
|
+
|
|
21
|
+
word: str
|
|
22
|
+
meaning: str
|
|
23
|
+
category: str = "general"
|
|
24
|
+
|
|
25
|
+
def __post_init__(self) -> None:
|
|
26
|
+
normalized = self.word.strip().lower()
|
|
27
|
+
if not normalized.isalpha():
|
|
28
|
+
raise ValueError("word must contain letters only")
|
|
29
|
+
if not self.meaning.strip():
|
|
30
|
+
raise ValueError("meaning cannot be empty")
|
|
31
|
+
object.__setattr__(self, "word", normalized)
|
|
32
|
+
object.__setattr__(self, "meaning", self.meaning.strip())
|
|
33
|
+
object.__setattr__(self, "category", self.category.strip().lower() or "general")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass(frozen=True, slots=True)
|
|
37
|
+
class GuessResult:
|
|
38
|
+
"""The outcome of a guess submitted to a game."""
|
|
39
|
+
|
|
40
|
+
letter: str
|
|
41
|
+
correct: bool
|
|
42
|
+
repeated: bool
|
|
43
|
+
status: GameStatus
|
|
44
|
+
masked_word: str
|
|
45
|
+
remaining_attempts: int
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class HangmanGame:
|
|
49
|
+
"""Play one Hangman round.
|
|
50
|
+
|
|
51
|
+
The game accepts one letter at a time, ignores letter case, and allows
|
|
52
|
+
repeated guesses without charging an additional attempt.
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
def __init__(self, entry: WordEntry, max_attempts: int = 6) -> None:
|
|
56
|
+
if max_attempts < 1:
|
|
57
|
+
raise ValueError("max_attempts must be at least 1")
|
|
58
|
+
self.entry = entry
|
|
59
|
+
self.max_attempts = max_attempts
|
|
60
|
+
self._guessed_letters: set[str] = set()
|
|
61
|
+
self._incorrect_letters: set[str] = set()
|
|
62
|
+
self._status = GameStatus.PLAYING
|
|
63
|
+
|
|
64
|
+
@property
|
|
65
|
+
def guessed_letters(self) -> frozenset[str]:
|
|
66
|
+
return frozenset(self._guessed_letters)
|
|
67
|
+
|
|
68
|
+
@property
|
|
69
|
+
def incorrect_letters(self) -> frozenset[str]:
|
|
70
|
+
return frozenset(self._incorrect_letters)
|
|
71
|
+
|
|
72
|
+
@property
|
|
73
|
+
def remaining_attempts(self) -> int:
|
|
74
|
+
return self.max_attempts - len(self._incorrect_letters)
|
|
75
|
+
|
|
76
|
+
@property
|
|
77
|
+
def status(self) -> GameStatus:
|
|
78
|
+
return self._status
|
|
79
|
+
|
|
80
|
+
@property
|
|
81
|
+
def masked_word(self) -> str:
|
|
82
|
+
return " ".join(
|
|
83
|
+
letter if letter in self._guessed_letters else "_"
|
|
84
|
+
for letter in self.entry.word
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
def guess(self, letter: str) -> GuessResult:
|
|
88
|
+
"""Submit a single alphabetic letter and return the result."""
|
|
89
|
+
normalized = letter.strip().lower()
|
|
90
|
+
if len(normalized) != 1 or not normalized.isalpha():
|
|
91
|
+
raise ValueError("guess must be a single letter")
|
|
92
|
+
if self._status is not GameStatus.PLAYING:
|
|
93
|
+
raise RuntimeError("the game is already over")
|
|
94
|
+
|
|
95
|
+
repeated = normalized in self._guessed_letters
|
|
96
|
+
if not repeated:
|
|
97
|
+
self._guessed_letters.add(normalized)
|
|
98
|
+
if normalized not in self.entry.word:
|
|
99
|
+
self._incorrect_letters.add(normalized)
|
|
100
|
+
|
|
101
|
+
if all(character in self._guessed_letters for character in self.entry.word):
|
|
102
|
+
self._status = GameStatus.WON
|
|
103
|
+
elif self.remaining_attempts == 0:
|
|
104
|
+
self._status = GameStatus.LOST
|
|
105
|
+
|
|
106
|
+
return GuessResult(
|
|
107
|
+
letter=normalized,
|
|
108
|
+
correct=normalized in self.entry.word,
|
|
109
|
+
repeated=repeated,
|
|
110
|
+
status=self._status,
|
|
111
|
+
masked_word=self.masked_word,
|
|
112
|
+
remaining_attempts=self.remaining_attempts,
|
|
113
|
+
)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""Load and query the built-in Hangman vocabulary."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import random
|
|
7
|
+
from importlib.resources import files
|
|
8
|
+
from typing import Iterable
|
|
9
|
+
|
|
10
|
+
from .game import WordEntry
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class WordBank:
|
|
14
|
+
"""An immutable collection of playable words."""
|
|
15
|
+
|
|
16
|
+
def __init__(self, entries: Iterable[WordEntry]) -> None:
|
|
17
|
+
self._entries = tuple(entries)
|
|
18
|
+
if not self._entries:
|
|
19
|
+
raise ValueError("word bank cannot be empty")
|
|
20
|
+
|
|
21
|
+
def __len__(self) -> int:
|
|
22
|
+
return len(self._entries)
|
|
23
|
+
|
|
24
|
+
def __iter__(self):
|
|
25
|
+
return iter(self._entries)
|
|
26
|
+
|
|
27
|
+
@property
|
|
28
|
+
def entries(self) -> tuple[WordEntry, ...]:
|
|
29
|
+
return self._entries
|
|
30
|
+
|
|
31
|
+
def categories(self) -> tuple[str, ...]:
|
|
32
|
+
return tuple(sorted({entry.category for entry in self._entries}))
|
|
33
|
+
|
|
34
|
+
def by_category(self, category: str) -> tuple[WordEntry, ...]:
|
|
35
|
+
normalized = category.strip().lower()
|
|
36
|
+
return tuple(entry for entry in self._entries if entry.category == normalized)
|
|
37
|
+
|
|
38
|
+
def random(self, category: str | None = None, rng: random.Random | None = None) -> WordEntry:
|
|
39
|
+
"""Choose a random entry, optionally limited to one category."""
|
|
40
|
+
choices = self.by_category(category) if category else self._entries
|
|
41
|
+
if not choices:
|
|
42
|
+
raise LookupError(f"no words found for category: {category}")
|
|
43
|
+
return (rng or random).choice(choices)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def default_word_bank() -> WordBank:
|
|
47
|
+
"""Return the vocabulary distributed with the package."""
|
|
48
|
+
resource = files("hangman").joinpath("data/words.json")
|
|
49
|
+
records = json.loads(resource.read_text(encoding="utf-8"))
|
|
50
|
+
return WordBank(WordEntry(**record) for record in records)
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: hangman-words
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A dependency-free Hangman game with a built-in word and meaning bank.
|
|
5
|
+
Author: Hangman Words Contributors
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Keywords: hangman,word-game,vocabulary,education
|
|
8
|
+
Classifier: Development Status :: 4 - Beta
|
|
9
|
+
Classifier: Intended Audience :: Education
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
12
|
+
Classifier: Topic :: Games/Entertainment
|
|
13
|
+
Requires-Python: >=3.10
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
Dynamic: license-file
|
|
17
|
+
|
|
18
|
+
# Hangman Words
|
|
19
|
+
|
|
20
|
+
A small, dependency-free Python package for playing Hangman with a prebuilt vocabulary and learner-friendly word meanings.
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
python -m pip install hangman-words
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Python API
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
from hangman import HangmanGame, default_word_bank
|
|
32
|
+
|
|
33
|
+
bank = default_word_bank()
|
|
34
|
+
entry = bank.random(category="science")
|
|
35
|
+
game = HangmanGame(entry)
|
|
36
|
+
|
|
37
|
+
result = game.guess("a")
|
|
38
|
+
print(result.masked_word)
|
|
39
|
+
print(entry.meaning)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The bundled word bank includes technology, science, travel, and vocabulary categories. `WordBank` also accepts your own `WordEntry` objects for custom games.
|
|
43
|
+
|
|
44
|
+
## Command line
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
hangman
|
|
48
|
+
hangman --category vocabulary --attempts 8
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Build and publish
|
|
52
|
+
|
|
53
|
+
Build both a wheel and source distribution with:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
python -m pip install --upgrade build twine
|
|
57
|
+
python -m build
|
|
58
|
+
python -m twine check dist/*
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Upload to TestPyPI first, then PyPI:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
python -m twine upload --repository testpypi dist/*
|
|
65
|
+
python -m twine upload dist/*
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Do not commit API tokens. Configure Twine credentials through its supported keyring or environment-variable workflow.
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
MIT
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.md
|
|
4
|
+
pyproject.toml
|
|
5
|
+
src/hangman/__init__.py
|
|
6
|
+
src/hangman/__main__.py
|
|
7
|
+
src/hangman/game.py
|
|
8
|
+
src/hangman/words.py
|
|
9
|
+
src/hangman/data/words.json
|
|
10
|
+
src/hangman_words.egg-info/PKG-INFO
|
|
11
|
+
src/hangman_words.egg-info/SOURCES.txt
|
|
12
|
+
src/hangman_words.egg-info/dependency_links.txt
|
|
13
|
+
src/hangman_words.egg-info/entry_points.txt
|
|
14
|
+
src/hangman_words.egg-info/top_level.txt
|
|
15
|
+
tests/test_game.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
hangman
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
from hangman import GameStatus, HangmanGame, WordEntry, default_word_bank
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def test_default_bank_contains_words_and_categories():
|
|
5
|
+
bank = default_word_bank()
|
|
6
|
+
|
|
7
|
+
assert len(bank) >= 16
|
|
8
|
+
assert {"science", "technology"}.issubset(bank.categories())
|
|
9
|
+
assert bank.by_category("SCIENCE")
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def test_game_wins_and_reveals_meaning():
|
|
13
|
+
game = HangmanGame(WordEntry("cat", "A small domesticated mammal."))
|
|
14
|
+
|
|
15
|
+
for letter in "cat":
|
|
16
|
+
result = game.guess(letter)
|
|
17
|
+
|
|
18
|
+
assert result.status is GameStatus.WON
|
|
19
|
+
assert result.masked_word == "c a t"
|
|
20
|
+
assert game.remaining_attempts == 6
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def test_repeated_guess_does_not_cost_attempt():
|
|
24
|
+
game = HangmanGame(WordEntry("book", "A written work."), max_attempts=2)
|
|
25
|
+
|
|
26
|
+
first = game.guess("x")
|
|
27
|
+
second = game.guess("x")
|
|
28
|
+
|
|
29
|
+
assert not first.correct
|
|
30
|
+
assert second.repeated
|
|
31
|
+
assert game.remaining_attempts == 1
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def test_game_loses_after_incorrect_guesses():
|
|
35
|
+
game = HangmanGame(WordEntry("sun", "The star at the center of our solar system."), max_attempts=2)
|
|
36
|
+
|
|
37
|
+
game.guess("x")
|
|
38
|
+
result = game.guess("y")
|
|
39
|
+
|
|
40
|
+
assert result.status is GameStatus.LOST
|
|
41
|
+
assert game.masked_word == "_ _ _"
|