Welcome to BoardGame SQLite, the ultimate Python library for crafting 2D game boards that stick around like a well-played chess move! 🧩 Whether you’re building a strategy game, a dungeon crawler, or a digital Risk clone, this library lets you manage a board with attributes (think tanks, health points, or sneaky rogues) and saves it all to SQLite—because no one likes losing their game state. With a rock-solid test suite, you’re ready to roll the dice! 🎯
Imagine your game board as a battlefield: every square holds troops, treasures, or traps. BoardGame SQLite is your trusty general, letting you:
- 🗺️ Command a 2D Board: Set up an n x n grid and assign attributes like
unit: tankorhealth: 5. - 💾 Persist Like a Pro: Save your board state to SQLite, in-memory for testing or file-based for epic campaigns.
- 🧑💻 Code with Confidence: 8 unit tests cover every move, from initialization to persistence.
- 🚀 Get Started Fast: No dependencies, just pure Python magic.
Whether you’re a hobbyist coder or a game dev wizard, this library is your shortcut to building robust game mechanics without sweating the storage details. Let’s dive in! ⚔️
- 🐍 Python 3.6+ (because who’s still on 2.x?)
- 📦 SQLite (comes with Python’s
sqlite3module)
- Clone this repo like you’re capturing a flag:
git clone https://github.com/your-username/boardgame-sqlite.git cd boardgame-sqlite - That’s it! No
pip installnonsense—pure Python standard library goodness.
Here’s how to set up a 5x5 board and deploy a tank:
from board_game import BoardGame
# Create a 5x5 board with a file-based SQLite database
game = BoardGame(size=5, db_name="battle.db")
# Deploy a tank at (2, 3)
game.set_position(2, 3, [["unit", "tank"], ["health", 5]])
print(game.get_position(2, 3)) # [['unit', 'tank'], ['health', 5]]
# Heal the tank
game.update_attribute(2, 3, "health", 10)
print(game.get_position(2, 3)) # [['unit', 'tank'], ['health', 10]]
# Clear the position
game.remove_position(2, 3)
print(game.get_position(2, 3)) # None
# Save and close
game.close()Run this, and your board state is safely tucked away in battle.db. Reload it later, and your tank’s ready for round two! 🛡️
The included test suite (TestBoardGame.py) is your quality assurance squad, with 8 tests covering every angle:
test_init: Ensures your board starts fresh.test_validate_coord: Keeps moves within bounds.test_set_position: Sets and overwrites attributes.test_get_position: Retrieves your game pieces.test_update_attribute: Updates or adds attributes.test_remove_position: Clears the battlefield.test_get_board_state: Grabs the full board state (safely copied).test_persistence: Confirms SQLite saves your game.
Run the tests:
python3 TestBoardGame.pyExpect a victory lap:
=== Setting up test ===
Testing __init__
__init__: Size and empty board - PASSED
...
Testing persistence
persistence: File-based persistence - PASSED
Ran 8 tests in 0.005s
OK
- Flexible Board Management 📍: Handle any n x n grid with sparse storage (only occupied positions are stored).
- SQLite Superpowers 💽: Toggle between in-memory (
:memory:) for tests and file-based (e.g.,game.db) for persistence. - Attribute Awesomeness 🏰: Store key-value pairs (e.g.,
[['unit', 'soldier'], ['health', 10]]) at any position. - Robust Validation 🔍: Coordinate checks prevent off-board shenanigans.
- Test-Driven Glory 🏆: 100% test coverage ensures your game logic is bulletproof.
boardgame-sqlite/
├── README.md 📜 Your guide to glory
├── board_game.py 🕹️ The BoardGame class with SQLite magic
├── TestBoardGame.py 🧪 Tests (with embedded BoardGame for sandbox compatibility)
├── header.txt 📄 Empty placeholder (future docs?)
├── footer.txt 📄 Empty placeholder (more to come?)
├── additional.txt 📄 Empty placeholder (room for extras)
board_game.py: The standaloneBoardGameclass, ready for production.TestBoardGame.py: Unit tests with an embeddedBoardGameclass to dodge import issues in sandboxes like Grok.com.header.txt,footer.txt,additional.txt: Empty files, waiting for your creative touch (game rules, metadata, or ASCII art?).
- Extend Attributes: Add complex attributes (e.g., nested lists) by tweaking the JSON serialization in
board_game.py. - Scale Up: Test with large boards (e.g.,
size=1000) to stress-test SQLite performance. - Add Transactions: Wrap SQLite operations in
BEGIN TRANSACTIONfor atomic updates in multiplayer games. - Visualize It: Pair with a library like
matplotlibto plot your board state. Want a heatmap of occupied positions? Ping me! - Cleanup: The test suite auto-deletes
test_board.db, but keep an eye on file-based DBs in production.
Want to level up this project? Contributions are as welcome as a critical hit! 🎉
- Fork the repo.
- Create a branch (
git checkout -b feature/EpicFeature). - Commit your brilliance (
git commit -m 'Add EpicFeature'). - Push it (
git push origin feature/EpicFeature). - Open a Pull Request and bask in the glory.
Please add tests for new features and ensure existing tests pass. Got ideas? Open an issue!
Licensed under the MIT License. Build, share, and conquer!
- 🐍 Python and SQLite for making this a breeze.
- ☕ Coffee, the true MVP of late-night coding.
- 🎮 Board game fans everywhere—keep rolling those dice!
🌟 Star this repo to show some love!
📬 Questions? Issues? Hit up the issues page.
🧑💻 Built with passion for devs like you. Now, go make some epic games!