Skip to content

jwc20/bncpy

Repository files navigation

Project Logo

BNCPY

Unleashing Collaborative Code-Breaking Fun for Everyone

license last-commit repo-top-language repo-language-count

Benjamin and Charlotte python library

Demo: https://bnc-client-psi.vercel.app

Docs: https://jwc20.github.io/bnc-docs/

Mono-repo: https://github.com/jwc20/bnc-game

BE: https://github.com/jwc20/bncapi

FE: https://github.com/jwc20/bnc-client



Table of Contents


Overview

bncpy is a robust Python-based game development tool that simplifies the process of creating a code-breaking game.

This project streamlines the game development process, providing a comprehensive suite of features for building a Bulls and Cows game. The core features include:

  • Game Logic Management: Handles the setup, progress, and conclusion of the game, ensuring a smooth gaming experience.
  • Game State Management: Manages game state, player state, and game configuration, maintaining the integrity of the game.

Project Structure

└── bncpy/
    ├── .github
    │   └── workflows
    ├── LICENSE
    ├── README.md
    ├── bnc
    │   ├── __init__.py
    │   ├── board.py
    │   ├── game.py
    │   ├── player.py
    │   ├── state.py
    │   └── utils.py
    ├── example.py
    ├── pyproject.toml
    ├── requirements.txt
    ├── tests
    │   ├── __init__.py
    │   ├── test_board.py
    │   ├── test_game.py
    │   ├── test_integration.py
    │   ├── test_player.py
    │   ├── test_state.py
    │   └── test_utils.py
    └── uv.lock

Project Index

BNCPY/
__root__
⦿ __root__
File Name Summary
LICENSE - The LICENSE file provides the legal framework for the project, granting users the freedom to use, modify, and distribute the software under the terms of the MIT License
- It disclaims warranties and limits liability, ensuring Jae W
- Chois rights are protected while encouraging open collaboration and sharing.
requirements.txt - Requirements.txt serves as a manifest for the projects Python dependencies, generated automatically by uv from pyproject.toml
- It lists libraries like anyio, certifi, httpx, and others, indicating their versions and interdependencies
- This file is crucial for ensuring consistent environment setup across different development and deployment scenarios.
pyproject.toml - The pyproject.toml file serves as the configuration blueprint for the bncpy project, a game developed by Jae W
- Choi
- It outlines the build system, project details, dependencies, and formatting rules
- The project requires Python 3.12 or higher and uses tools like setuptools and wheel for building and packaging
- It also specifies coding style guidelines using the ruff tool.
example.py - Example.py demonstrates the functionality of a collaborative and competitive game mode in a code-breaking game
- It showcases the process of setting up the game, adding players, submitting guesses, and tracking game status
- The script also tests JSON serialization and deserialization of the game state.
bnc
⦿ bnc
File Name Summary
board.py - Board.py manages the game board for a Bulls and Cows game, handling the initialization and validation of game parameters such as code length, number of colors, and number of guesses
- It also manages the game state, including tracking guesses, evaluating the correctness of guesses, and determining game outcomes.
game.py - Game.py manages the game logic for a board game involving multiple players
- It handles the setup, progress, and conclusion of the game, including player actions such as submitting guesses and setting secret codes
- The module ensures consistency across player boards and determines the games winner(s).
utils.py - Bnc/utils.py` serves as a utility module in the codebase, providing essential functions for validating user input, generating random guesses, calculating game scores, and fetching random numbers
- It ensures the integrity of the game logic and enhances the overall functionality of the application.
player.py - Player, located at bnc/player.py, represents a participant in the game, maintaining their name and game board
- It provides methods to set a secret code, make a guess, and check the game status
- It interacts with the Board class to evaluate guesses and determine game outcomes.
state.py - The state.py module in the Bulls and Cows game codebase manages the game state, player state, and game configuration
- It handles player actions such as submitting guesses, adding or removing players, and resetting the game
- It also validates game configurations and maintains the games state, including tracking winners and remaining guesses.
.github
⦿ .github
workflows
⦿ .github.workflows
File Name Summary
publish.yml - Publishing Python Package to PyPI automates the process of packaging and uploading Python projects to the Python Package Index
- It triggers on a new release, sets up Python, installs dependencies, builds the package, stores the distribution, and uploads it to PyPI using GitHub Actions.

Getting Started

Prerequisites

This project requires the following dependencies:

  • Programming Language: Python
  • Package Manager: Pip, Uv

Installation

Build bncpy from the source and intsall dependencies:

  1. Clone the repository:
git clone https://github.com/jwc20/bncpy
  1. Navigate to the project directory:
cd bncpy
  1. Install the dependencies:
pip install -r requirements.txt
uv sync --all-extras --dev

Alternatively, you can pip install:

# Install package from PyPi
pip install bncpy

Usage

from bnc import Board, Game, Player
from bnc.utils import generate_guess, get_random_number

# Game configuration
config = {
  "code_length": 4,
  "num_of_colors": 6,
  "num_of_guesses": 10
}

# Create players with boards
players = [
  Player(name="Jae", board=Board(**config)),
  Player(name="Soo", board=Board(**config)),
  Player(name="Benjamin", board=Board(**config)),
  Player(name="Charlotte", board=Board(**config))
]

# Create game with secret code
secret_code = get_random_number(length=4, max_value=5)  # Random 4-digit code
game = Game(players, secret_code=secret_code)

# Submit guesses
game.submit_guess(players[0], "1234")
game.submit_guess(players[1], generate_guess(config["code_length"], config["num_of_colors"]))  # Random guess

# Check game state
print(game.state)
players[0].board.display_board()

# Check winners
if game.winners:
  for winner in game.winners:
    print(f"Winner: {winner.name}")

Setting the secret code

  • Multiple ways to set secret codes:
    • Via Game initialization: Game(players, secret_code="1234")
    • Per player: player.set_secret_code_to_board("1234")
    • Random generation: game.set_random_secret_code()

Testing

Bncpy uses the pytest test framework. Run the test suite with:

Using pip:

pytest

Using uv:

uv run pytest tests/

Dependencies

About

Benjamin and Charlotte, the code guessing game

Topics

Resources

License

Stars

Watchers

Forks

Languages