Skip to main content

Codenames board game logic implementation in python.

Project description

Codenames

PyPI version Pipeline codecov Ruff Code style: black Imports: isort Type checked: mypy Linting: pylint

Code infrastructure for the Codenames board game.
Designed to serve as a base for implementing players (agents) with different strategies and algorithms.
See the codenames-solvers repository for such examples.

Installation

Install from PyPI using pip: pip install codenames

Usage

Here is a simple example of command-line based players, and a GameRunner that runs a game between them:

import logging
import sys

from codenames.classic.builder import generate_board
from codenames.classic.color import ClassicTeam
from codenames.classic.runner.models import GamePlayers
from codenames.classic.runner.runner import ClassicGameRunner
from codenames.generic.move import Clue, Guess
from codenames.generic.player import Operative, Spymaster
from codenames.generic.state import OperativeState, SpymasterState

logging.basicConfig(level=logging.INFO, format="%(message)s", stream=sys.stdout)


# Implement basic players

class CLIHinter(Spymaster):
    def give_clue(self, game_state: SpymasterState) -> Clue:
        print("Board state: \n" + game_state.board.printable_string)
        hint_word = input("Enter hint word: ")
        card_amount = int(input("Enter card amount: "))
        return Clue(word=hint_word, card_amount=card_amount)


class CLIGuesser(Operative):
    def guess(self, game_state: OperativeState) -> Guess:
        print(game_state.board.printable_string)
        card_word = input("Enter card word: ")
        card_index = game_state.board.find_card_index(word=card_word)
        return Guess(card_index=card_index)


# Run game

def run_cli_game():
    language = "english"
    board = generate_board(language=language)
    blue_hinter, blue_guesser = CLIHinter("Yoda", ClassicTeam.BLUE), CLIGuesser("Luke", ClassicTeam.BLUE)
    red_hinter, red_guesser = CLIHinter("Einstein", ClassicTeam.RED), CLIGuesser("Newton", ClassicTeam.RED)
    players = GamePlayers.from_collection([blue_hinter, blue_guesser, red_hinter, red_guesser])
    runner = ClassicGameRunner(players=players, board=board)
    winner = runner.run_game()
    print(f"Winner: {winner}")


if __name__ == "__main__":
    run_cli_game()

Example output:

[Blue] turn.
Board state:
+------------+------------+--------------+-------------+-------------------+
|   ‎🟦 tax   |  ‎⬜ drama  |   ‎⬜ thick   |  ‎🟥 africa  | ‎💀 transformation |
+------------+------------+--------------+-------------+-------------------+
| ‎⬜ project | ‎🟦 athlete | ‎🟥 vegetable | ‎⬜ engineer |     ‎🟥 human      |
+------------+------------+--------------+-------------+-------------------+
|  ‎⬜ chain  |  ‎🟦 cake   |   ‎🟦 shift   |  ‎🟦 study   |      ‎🟥 will      |
+------------+------------+--------------+-------------+-------------------+
| ‎🟥 outcome |  ‎🟥 desk   |  ‎🟥 soviet   |   ‎⬜ rare   |     ‎🟥 youth      |
+------------+------------+--------------+-------------+-------------------+
| ‎🟦 account | ‎🟦 couple  |   ‎⬜ solve   | ‎🟦 academic |     ‎🟦 stable     |
+------------+------------+--------------+-------------+-------------------+
Enter hint word: example
Enter card amount: 2
Hinter: [example] 2 card(s)
+---------+---------+-----------+----------+----------------+
|   ‎tax   |  ‎drama  |   ‎thick   |  ‎africa  | ‎transformation |
+---------+---------+-----------+----------+----------------+
| ‎project | ‎athlete | ‎vegetable | ‎engineer |     ‎human      |
+---------+---------+-----------+----------+----------------+
|  ‎chain  |  ‎cake   |   ‎shift   |  ‎study   |      ‎will      |
+---------+---------+-----------+----------+----------------+
| ‎outcome |  ‎desk   |  ‎soviet   |   ‎rare   |     ‎youth      |
+---------+---------+-----------+----------+----------------+
| ‎account | ‎couple  |   ‎solve   | ‎academic |     ‎stable     |
+---------+---------+-----------+----------+----------------+
Enter card word: account
Guesser: '🟦 account', correct!
+------------+---------+-----------+----------+----------------+
|    ‎tax     |  ‎drama  |   ‎thick   |  ‎africa  | ‎transformation |
+------------+---------+-----------+----------+----------------+
|  ‎project   | ‎athlete | ‎vegetable | ‎engineer |     ‎human      |
+------------+---------+-----------+----------+----------------+
|   ‎chain    |  ‎cake   |   ‎shift   |  ‎study   |      ‎will      |
+------------+---------+-----------+----------+----------------+
|  ‎outcome   |  ‎desk   |  ‎soviet   |   ‎rare   |     ‎youth      |
+------------+---------+-----------+----------+----------------+
| ‎🟦 account | ‎couple  |   ‎solve   | ‎academic |     ‎stable     |
+------------+---------+-----------+----------+----------------+
Enter card word: rare
Guesser: '⬜ rare', wrong!
Guesser wrong, turn is over
...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

codenames-5.0.1.tar.gz (28.9 kB view details)

Uploaded Source

Built Distribution

codenames-5.0.1-py3-none-any.whl (35.1 kB view details)

Uploaded Python 3

File details

Details for the file codenames-5.0.1.tar.gz.

File metadata

  • Download URL: codenames-5.0.1.tar.gz
  • Upload date:
  • Size: 28.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for codenames-5.0.1.tar.gz
Algorithm Hash digest
SHA256 f4bfa0591a5ac6e94e84f817260b9d1474427a2fb5565c9e75382862940a2da1
MD5 2ebd5bdffa70938ffe2f03147c81c7b8
BLAKE2b-256 0b573072ebc70f9f711e2287e89c18ec019f18c65138e4d3298224efaafca715

See more details on using hashes here.

Provenance

The following attestation bundles were made for codenames-5.0.1.tar.gz:

Publisher: pipeline.yml on asaf-kali/codenames

Attestations:

File details

Details for the file codenames-5.0.1-py3-none-any.whl.

File metadata

  • Download URL: codenames-5.0.1-py3-none-any.whl
  • Upload date:
  • Size: 35.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for codenames-5.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b12af65aa9da77f8820331a34841158b8a35995356c341e2ddf7e63efb1631cc
MD5 5e708bf35f6cb3e542f59e97fe5a4b95
BLAKE2b-256 d814dc89d41474209e3187cbb697de15b3578ab85d2b1be271010187f3825614

See more details on using hashes here.

Provenance

The following attestation bundles were made for codenames-5.0.1-py3-none-any.whl:

Publisher: pipeline.yml on asaf-kali/codenames

Attestations:

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page