Spaces:
Running
Running
File size: 19,078 Bytes
032159d 1b1b6cc ba28a1f 032159d 7606f52 032159d b66ffa9 032159d 0b81bf2 507acad 032159d 0b81bf2 032159d 0b81bf2 032159d 0b81bf2 3666fb8 0b81bf2 2de3d96 0b81bf2 507acad 032159d 7606f52 0ee3b82 7606f52 032159d fbf85a5 7606f52 fbf85a5 0b81bf2 032159d 7606f52 507acad 7606f52 032159d 7f5ea9e 507acad 032159d 3666fb8 7ef6f2b 7606f52 0b81bf2 1b1b6cc 2de3d96 0b81bf2 522064b 0b81bf2 96b8e12 61f620d 96b8e12 61f620d 522064b 7606f52 c8aba37 f50a1b0 02ecbe1 f969c4d 507acad f969c4d 7606f52 9ecb8cf 7606f52 9ecb8cf fd702c9 7606f52 c004d56 7606f52 63cbfa3 7606f52 63cbfa3 4dab390 7606f52 4dab390 4302853 7606f52 4302853 7896205 7606f52 7896205 7606f52 7896205 8c718b2 7606f52 8c718b2 1dcc163 7606f52 1dcc163 7606f52 10dda8b 7606f52 10dda8b 7606f52 99df003 7606f52 d891c6e 7606f52 7ef6f2b 7606f52 7ef6f2b 7606f52 ca7de5e 7606f52 ca7de5e 7606f52 ca7de5e 7606f52 fbf85a5 7606f52 fbf85a5 7606f52 fbf85a5 7606f52 3666fb8 7606f52 3666fb8 7606f52 3666fb8 032159d fbf85a5 032159d 7606f52 032159d 7606f52 032159d 7606f52 032159d 7606f52 032159d 7606f52 032159d 7606f52 032159d 7896205 7606f52 7896205 0b81bf2 9ecb8cf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
---
title: BattleWords
emoji: π²
colorFrom: blue
colorTo: indigo
sdk: streamlit
sdk_version: 1.51.0
python_version: 3.12.8
app_port: 8501
app_file: app.py
tags:
- game
- vocabulary
- streamlit
- education
---
# BattleWords
> **This project is used by [huggingface.co](https://huggingface.co/spaces/Surn/BattleWords) as a demonstration of interactive word games in Python.**
BattleWords is a vocabulary learning game inspired by classic Battleship mechanics. The objective is to discover hidden words on a grid, earning points for strategic guessing before all letters are revealed.
## Features
### Core Gameplay
- 12x12 grid with six hidden words (2x4-letter, 2x5-letter, 2x6-letter)
- Words placed horizontally or vertically
- Animated radar visualization showing word boundaries
- Reveal grid cells and guess words for points
- Scoring tiers: Good (34β37), Great (38β41), Fantastic (42+)
- Game ends when all words are guessed or all word letters are revealed
- Incorrect guess history with tooltip and optional display (enabled by default)
- 10 incorrect guess limit per game
- Two game modes: Classic (chain guesses) and Too Easy (single guess per reveal)
### Audio & Visuals
- Ocean-themed gradient background with wave animations
- Background music system (toggleable with volume control)
- Sound effects for hits, misses, correct/incorrect guesses
- Responsive UI built with Streamlit
### Customization
- Multiple word lists (classic, fourth_grade, wordlist)
- Wordlist sidebar controls (picker + one-click sort)
- Configurable word spacing (0-2 cells between words)
- Audio volume controls (music and effects separate)
### β
Challenge Mode (v0.2.20+)
- **Shareable challenge links** via short URLs (`?game_id=<sid>`)
- **Multi-user leaderboards** sorted by score and time
- **Remote storage** via Hugging Face datasets
- **Word list difficulty calculation** and display
- **Submit results** to existing challenges or create new ones
- **Top 5 leaderboard** display in Challenge Mode banner
- **"Show Challenge Share Links" toggle** (default OFF) to control URL visibility
- Each player gets different random words from the same wordlist
### Deployment & Technical
- **Dockerfile-based deployment** supported for Hugging Face Spaces and other container platforms
- **Environment variables** for Challenge Mode (HF_API_TOKEN, HF_REPO_ID, SPACE_NAME)
- Works offline without HF credentials (Challenge Mode features disabled gracefully)
### Progressive Web App (PWA) - v0.2.28
- Installable on desktop and mobile from your browser:
- Chrome/Edge: Menu β βInstall appβ
- Android Chrome: βAdd to Home screenβ
- iOS Safari: Share β βAdd to Home Screenβ
- Includes `service worker` and `manifest.json` with basic offline caching of static assets
- See `INSTALL_GUIDE.md` for platform-specific steps
### Planned (v0.3.0)
- Local persistent storage for personal game history
- Personal high scores sidebar (offline-capable)
- Player statistics tracking
- Deterministic seed UI for custom puzzles
### Beta (v0.5.0)
- Word overlaps on shared letters (crossword-style)
- Enhanced responsive layout for mobile/tablet
- Keyboard navigation and guessing
### Full (v1.0.0)
- Daily puzzle mode with global leaderboards
- Practice mode with hints
- Multiple difficulty levels
- Internationalization (i18n) support
## Challenge Mode & Leaderboard
When playing a shared challenge (via a `game_id` link), the leaderboard displays all submitted results for that challenge. The leaderboard is **sorted by highest score (descending), then by fastest time (ascending)**. This means players with the most points appear at the top, and ties are broken by the shortest completion time.
## Installation
1. Clone the repository:
```
git clone https://github.com/Oncorporation/BattleWords.git
cd battlewords
```
2. (Optional) Create and activate a virtual environment:
```
python -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`
```
3. Install dependencies: ( add --system if not using a virutal environment)
```
uv pip install -r requirements.txt --link-mode=copy
```
## Running BattleWords
You can run the app locally using either [uv](https://github.com/astral-sh/uv) or Streamlit directly:
```
uv run streamlit run app.py
```
or
```
streamlit run app.py
```
### Dockerfile Deployment (Hugging Face Spaces and more)
BattleWords supports containerized deployment using a `Dockerfile`. This is the recommended method for deploying to [Hugging Face Spaces](https://huggingface.co/docs/hub/spaces-sdks-docker) or any Docker-compatible environment.
To deploy on Hugging Face Spaces:
1. Add a `Dockerfile` to your repository root (see [Spaces Dockerfile guide](https://huggingface.co/docs/hub/spaces-sdks-docker)).
2. Push your code to your Hugging Face Space.
3. The platform will build and run your app automatically.
For local Docker runs:
```sh
docker build -t battlewords .
docker run -p8501:8501 battlewords
```
### Environment Variables (for Challenge Mode)
Challenge Mode requires a `.env` file in the project root with HuggingFace Hub credentials:
```bash
# Required for Challenge Mode
HF_API_TOKEN=hf_xxxxxxxxxxxxxxxxxxxxx # or HF_TOKEN
HF_REPO_ID=YourUsername/YourRepo # Target HF dataset repo
SPACE_NAME=YourUsername/BattleWords # Your HF Space name
# Optional
CRYPTO_PK= # Reserved for future signing
```
**How to get your HF_API_TOKEN:**
1. Go to https://huggingface.co/settings/tokens
2. Create a new token with `write` access
3. Add to `.env` file as `HF_API_TOKEN=hf_...`
**Note:** The app works without these variables, but Challenge Mode features (sharing, leaderboards) will be disabled.
## Folder Structure
- `app.py` β Streamlit entry point
- `battlewords/` β Python package
- `models.py` β data models and types
- `word_loader.py` β word list loading and validation
- `generator.py` β word placement logic
- `logic.py` β game mechanics (reveal, guess, scoring)
- `ui.py` β Streamlit UI composition
- `game_storage.py` β Hugging Face remote storage integration and challenge sharing
- `local_storage.py` β local JSON storage for results and high scores
- `storage.py` β (legacy) local storage and high scores
- `words/wordlist.txt` β candidate words
- `specs/` β documentation (`specs.md`, `requirements.md`)
- `tests/` β unit tests
## How to Play
1. Click grid squares to reveal letters or empty spaces.
2. After revealing a letter, enter a guess for a word in the text box.
3. Earn points for correct guesses and bonus points for unrevealed letters.
4. **The game ends when all six words are found or all word letters are revealed. Your score tier is displayed.**
5. **To play a shared challenge, use a link with `?game_id=<sid>`. Your result will be added to the challenge leaderboard.**
## Changelog
### v0.3.0 (planned)
- Local persistent storage for personal game history (offline-capable)
- Personal high scores sidebar with filtering
- Player statistics tracking (games played, averages, bests)
-0.2.29
- change difficulty calculation
- add test_compare_difficulty_functions
- streamlit version update to 1.51.0
-0.2.28
- PWA INSTALL_GUIDE.md added
- PWA implementation with service worker and manifest.json added
-0.2.27
- Add "Show Challenge Share Links" setting (default: off)
- When disabled:
- Header Challenge Mode: hides the Share Challenge link
- Game Over: allows submitting results but suppresses displaying the generated share URL
- The setting is saved in session state and preserved across "New Game"
- No changes to game logic or storage; only UI visibility behavior
-0.2.26
- fix copy/share link button
-0.2.25
- Share challenge from expander
- fix incorrect guess overlap of guess box
-0.2.24
- compress height
- change incorrect guess tooltip location
- update final screen layout
- add word difficulty formula
- update documentation
-0.2.23
- Update miss and correct guess sound effects to new versions
- allow iframe hosted version to pass url as a query string parameter (&iframe_host=https%3A%2F%2Fwww.battlewords.com%2Fplaynow.html) url encoding is required.
- minimal security added to prevent users from changing the options in a challenge.
-0.2.22
- fix challenge mode link
- challenge mode UI improvements
-0.2.21
- fix tests
-0.2.20
- Remote Storage game_id:
- Per-game JSON settings uploaded to a storage server (Hugging Face repo) under unique `games/{uid}/settings.json`
- A shortened URL id (sid) is generated; shareable link: `?game_id=<sid>`
- On load with `game_id`, the app resolves sid to the JSON and applies word_list, game_mode, grid_size, puzzle options
- High Scores: add remote `highscores/highscores.json` (repo) alongside local highscores
- Dependencies: add `huggingface_hub` and `python-dotenv`
- Env: `.env` should include `HF_API_TOKEN` (or `HF_TOKEN`), `CRYPTO_PK`, `HF_REPO_ID`, `SPACE_NAME`
### Environment Variables
- HF_API_TOKEN or HF_TOKEN: HF Hub access token
- CRYPTO_PK: reserved for signing (optional)
- HF_REPO_ID: e.g., Surn/Storage
- SPACE_NAME: e.g., Surn/BattleWords
### Remote Storage Structure
- shortener.json
- games/{uid}/settings.json
- highscores/highscores.json
Note
- `battlewords/storage.py` remains local-only storage; a separate HF integration wrapper is provided as `game_storage.py` for remote challenge mode.
-0.2.19
- Fix music and sound effect volume issues
- Update documentation for proposed new features
-0.2.18
- Fix sound effect volume wiring and apply volume to all effects (hit/miss/correct/incorrect)
- Respect "Enable music" and "Volume" when playing congratulations music and when resuming background music (uses selected track)
- Add "Enable Sound Effects" checkbox (on by default) and honor it across the app
- Save generated effects to `assets/audio/effects/` so they are picked up by the app
- Add `requests` dependency for sound effect generation
-0.2.17
- documentation updates and corrections
- updated CLAUDE.md with accurate feature status and project structure
- clarified v0.3.0 planned features vs current implementation
-0.2.16
- replace question marks in score panel with underscores
- add option to toggle incorrect guess history display in settings (enabled by default)
- game over popup updated to ensure it is fully visible on screen
-0.2.15
- fix music playing after game end
- change incorrect guesses icon
- fix sound effect and music volume issues
-0.2.14
- bug fix on final score popup
- score panel alignment centered
- change incorrect guess history UI
-0.2.13
- upgrade background ocean view
- apply volume control to sound effects
-0.2.12
- fix music looping on congratulations screen
-0.2.11
- update timer to be live during gameplay, but reset with each action
- compact design
- remove fullscreen image tooltip
-0.2.10
- reduce sonar graphic size
- update music and special effects file locations
- remove some music and sound effects
- change Guess Text input color
- incorrect guess UI update
- scoreboard update
-0.2.9
- fix sonar grid alignment issue on some browsers
- When all letters of a word are revealed, it is automatically marked as found.
-0.2.8
- Add10 incorrect guess limit per game
-0.2.7
- fix background music playback issue on some browsers
- add sound effects
- enhance sonar grid visualization
- add claude.md documentation
-0.2.6
- fix sonar grid alignment
- improve score summary layout and styling
- Add timer to game display in sidebar
-0.2.5
- fix finale pop up issue
- make grid cells square on wider devices
-0.2.4
- Add music files to repo
- disable music by default
-0.2.3
- Update version information display
- adjust sonar grid alignment
- fix settings scroll issue
-0.2.2
- Add Musical background and settings to toggle sound on/off.
-0.2.1
- Add Theme toggle (light/dark/custom) in sidebar.
-0.2.0
- Added a loading screen when starting a new game.
- Added a congratulations screen with your final score and tier when the game ends.
-0.1.13
- Improved score summary layout for clarity and style.
-0.1.12
- Improved score summary layout and styling.
- Enhanced overall appearance and readability.
-0.1.11
- Game now ends when all words are found or revealed.
- Added word spacing logic and improved settings.
-0.1.10
- Added game mode selector and improved UI feedback.
-0.1.9
- Improved background and mobile layout.
-0.1.8
- Updated to Python3.12.
-0.1.5
- Added hit/miss indicator and improved grid feedback.
-0.1.4
- Radar visualization improved and mobile layout enhanced.
-0.1.3
- Added wordlist picker and sort feature.
- Improved score panel and final score display.
## Known Issues / TODO
- Word list loading bug: the app may not select the proper word lists in some environments. Investigate `word_loader.get_wordlist_files()` / `load_word_list()` and sidebar selection persistence to ensure the chosen file is correctly used by the generator.
## Development Phases
- **Proof of Concept (0.1.0):** No overlaps, basic UI, single session.
- **Beta (0.5.0):** Overlaps allowed on shared letters, responsive layout, keyboard support, deterministic seed.
- **Full (1.0.0):** Enhanced UX, persistence, leaderboards, daily/practice modes, advanced features.
See `specs/requirements.md` and `specs/specs.md` for full details and roadmap.
## License
BattlewordsTM. All Rights Reserved. All content, trademarks and logos are copyrighted by the owner.
## Hugging Face Spaces Configuration
BattleWords is deployable as a Hugging Face Space. You can use either the YAML config block or a Dockerfile for advanced/custom deployments.
To configure your Space with the YAML block, add it at the top of your `README.md`:
```yaml
---
title: BattleWords
emoji: π²
colorFrom: blue
colorTo: indigo
sdk: streamlit
sdk_version:1.25.0
python_version:3.10
app_file: app.py
tags:
- game
- vocabulary
- streamlit
- education
---
```
**Key parameters:**
- `title`, `emoji`, `colorFrom`, `colorTo`: Visuals for your Space.
- `sdk`: Use `streamlit` for Streamlit apps.
- `sdk_version`: Latest supported Streamlit version.
- `python_version`: Python version (default is3.10).
- `app_file`: Entry point for your app.
- `tags`: List of descriptive tags.
**Dependencies:**
Add a `requirements.txt` with your Python dependencies (e.g., `streamlit`, etc.).
**Port:**
Streamlit Spaces use port `8501` by default.
**Embedding:**
Spaces can be embedded in other sites using an `<iframe>`:
```html
<iframe src="https://Surn-BattleWords.hf.space?embed=true" title="BattleWords"></iframe>
```
For full configuration options, see [Spaces Config Reference](https://huggingface.co/docs/hub/spaces-config-reference) and [Streamlit SDK Guide](https://huggingface.co/docs/hub/spaces-sdks-streamlit).
# Assets Setup
To fully experience BattleWords, especially the audio elements, ensure you set up the following assets:
- Place your background music `.mp3` files in `battlewords/assets/audio/music/` to enable music.
- Place your sound effect files (`.mp3` or `.wav`) in `battlewords/assets/audio/effects/` for sound effects.
Refer to the documentation for guidance on compatible audio formats and common troubleshooting tips.
# Sound Asset Generation
To generate and save custom sound effects for BattleWords, you can use the `generate_sound_effect` function.
## Function: `generate_sound_effect`
```python
def generate_sound_effect(effect: str, save_to_assets: bool = False, use_api: str = "huggingface") -> str:
"""
Generate a sound effect and save it as a file.
Parameters:
- `effect`: Name of the effect to generate.
- `save_to_assets`: If `True`, saves the effect to the assets directory;
if `False`, saves to a temporary location. Default is `False`.
- `use_api`: API to use for generation. Options are "huggingface" or "replicate". Default is "huggingface".
Returns:
- File path to the saved sound effect.
"""
# ... [sound generation code] ...
if save_to_assets:
# Save to effects directory
assets_dir = os.path.join(os.path.dirname(__file__), "assets", "audio", "effects")
os.makedirs(assets_dir, exist_ok=True)
filename = f"{effect}.wav"
path = os.path.join(assets_dir, filename)
with open(path, "wb") as f:
f.write(audio_bytes)
print(f" Saved to: {path}")
else:
# Save to temporary file
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmpfile:
tmpfile.write(audio_bytes)
path = tmpfile.name
print(f" Saved to: {path}")
return path
```
## Parameters
- `effect`: The name of the sound effect you want to generate (e.g., "explosion", "powerup").
- `save_to_assets` (optional): Set to `True` to save the generated sound effect to the game's assets directory. If `False`, the effect is saved to a temporary location. Default is `False`.
- `use_api` (optional): The API to use for generating the sound. Options are `"huggingface"` or `"replicate"`. Default is `"huggingface"`.
## Returns
- The function returns the file path to the saved sound effect, whether it's in the assets directory or a temporary location.
## Usage Example
To generate a sound effect and save it to the assets directory:
```python
generate_sound_effect("your_effect_name", save_to_assets=True)
```
To generate a sound effect and keep it in a temporary location:
```python
temp_path = generate_sound_effect("your_effect_name", save_to_assets=False)
```
## Note
Ensure you have the necessary permissions and API access (if required) to use the sound generation service. Generated sounds are subject to the terms of use of the respective API.
For any issues or enhancements, please refer to the project documentation or contact the project maintainer.
Happy gaming and sound designing!
## What's New in v0.2.20-0.2.27: Challenge Mode π―
### Remote Challenge Sharing π
- Share challenges with friends via short URLs (`?game_id=<sid>`)
- Each player gets different random words from the same wordlist
- Multi-user leaderboards sorted by score and time
- Word list difficulty calculation and display
- Compare your performance against others!
### Leaderboards π
- Top 5 players displayed in Challenge Mode banner
- Results sorted by: highest score β fastest time β highest difficulty
- Submit results to existing challenges or create new ones
- Player names supported (optional, defaults to "Anonymous")
### Remote Storage πΎ
- Challenge data stored in Hugging Face dataset repositories
- Automatic save on game completion (with user consent)
- "Show Challenge Share Links" toggle for privacy control (default OFF)
- Works offline when HF credentials not configured
## What's Planned for v0.3.0
### Local Player History (Coming Soon)
- Personal game results saved locally in `~/.battlewords/data/`
- Offline-capable high score tracking
- Player statistics (games played, averages, bests)
- Privacy-first: no cloud dependency for personal data
- Easy data management (delete `~/.battlewords/data/` to reset)
|