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)

####################################
# Naive CLI players implementation #
####################################


class CLISpymaster(Spymaster):
    def give_clue(self, game_state: SpymasterState) -> Clue:
        print("\nGive a hint. Board: \n" + game_state.board.printable_string)
        print(f"Revealed cards: {list(game_state.board.revealed_card_indexes)}")
        hint_word = input("Enter hint word: ")
        card_amount = int(input("Enter card amount: "))
        return Clue(word=hint_word, card_amount=card_amount)


class CLIOperative(Operative):
    def guess(self, game_state: OperativeState) -> Guess:
        print(f"\nGuess a card. Given clue: {game_state.current_clue}. Current board state: ")
        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)


def run_cli_game():
    language = "english"
    board = generate_board(language=language)
    blue_spymaster = CLISpymaster("Yoda", ClassicTeam.BLUE)
    blue_operative = CLIOperative("Luke", ClassicTeam.BLUE)
    red_spymaster = CLISpymaster("Einstein", ClassicTeam.RED)
    red_operative = CLIOperative("Newton", ClassicTeam.RED)
    players = GamePlayers.from_collection([blue_spymaster, blue_operative, red_spymaster, red_operative])
    runner = ClassicGameRunner(players=players, board=board)
    winner = runner.run_game()
    print(f"Winner: {winner}")


if __name__ == "__main__":
    run_cli_game()

Example output:

-----
[RED] turn.

Give a hint. Board:
+-----------+-------------+------------+-----------------+------------+
| ‎🟦 pretty |  ‎⬜ young   |  ‎⬜ essay  |    ‎🟥 apple     |  ‎⬜ kiss   |
+-----------+-------------+------------+-----------------+------------+
|  ‎⬜ poem  |  ‎🟦 solve   |   ‎🟥 pan   | ‎🟦 organization |  ‎🟦 union  |
+-----------+-------------+------------+-----------------+------------+
|  ‎🟥 myth  |   ‎🟥 neck   | ‎🟥 shelter |    ‎🟦 locate    |   ‎🟥 pet   |
+-----------+-------------+------------+-----------------+------------+
| ‎🟥 react  |  ‎⬜ person  |  ‎⬜ mood   |    ‎🟥 heart     | ‎⬜ breath  |
+-----------+-------------+------------+-----------------+------------+
|  ‎🟥 rich  | ‎🟦 standard |  ‎🟦 crop   |   ‎💀 chicken    | ‎🟦 wedding |
+-----------+-------------+------------+-----------------+------------+
Revealed cards: []
Enter hint word: example
Enter card amount: 2
Spymaster: [example] 2 card(s)

Guess a card. Given clue: [example] [2]. Current board state:
+--------+----------+---------+--------------+---------+
| ‎pretty |  ‎young   |  ‎essay  |    ‎apple     |  ‎kiss   |
+--------+----------+---------+--------------+---------+
|  ‎poem  |  ‎solve   |   ‎pan   | ‎organization |  ‎union  |
+--------+----------+---------+--------------+---------+
|  ‎myth  |   ‎neck   | ‎shelter |    ‎locate    |   ‎pet   |
+--------+----------+---------+--------------+---------+
| ‎react  |  ‎person  |  ‎mood   |    ‎heart     | ‎breath  |
+--------+----------+---------+--------------+---------+
|  ‎rich  | ‎standard |  ‎crop   |   ‎chicken    | ‎wedding |
+--------+----------+---------+--------------+---------+
Enter card word: heart
Operative: '🟥 heart', correct!

Guess a card. Given clue: [example] [2]. Current board state:
+--------+----------+---------+--------------+---------+
| ‎pretty |  ‎young   |  ‎essay  |    ‎apple     |  ‎kiss   |
+--------+----------+---------+--------------+---------+
|  ‎poem  |  ‎solve   |   ‎pan   | ‎organization |  ‎union  |
+--------+----------+---------+--------------+---------+
|  ‎myth  |   ‎neck   | ‎shelter |    ‎locate    |   ‎pet   |
+--------+----------+---------+--------------+---------+
| ‎react  |  ‎person  |  ‎mood   |   ‎🟥 heart   | ‎breath  |
+--------+----------+---------+--------------+---------+
|  ‎rich  | ‎standard |  ‎crop   |   ‎chicken    | ‎wedding |
+--------+----------+---------+--------------+---------+
Enter card word: pet
Operative: '🟥 pet', correct!

Guess a card. Given clue: [example] [2]. Current board state:
+--------+----------+---------+--------------+---------+
| ‎pretty |  ‎young   |  ‎essay  |    ‎apple     |  ‎kiss   |
+--------+----------+---------+--------------+---------+
|  ‎poem  |  ‎solve   |   ‎pan   | ‎organization |  ‎union  |
+--------+----------+---------+--------------+---------+
|  ‎myth  |   ‎neck   | ‎shelter |    ‎locate    | ‎🟥 pet  |
+--------+----------+---------+--------------+---------+
| ‎react  |  ‎person  |  ‎mood   |   ‎🟥 heart   | ‎breath  |
+--------+----------+---------+--------------+---------+
|  ‎rich  | ‎standard |  ‎crop   |   ‎chicken    | ‎wedding |
+--------+----------+---------+--------------+---------+
Enter card word: mood
Operative: '⬜ mood', wrong!
Operative wrong, turn is over

-----
[BLUE] turn.

Give a hint. Board:
+-----------+-------------+------------+-----------------+------------+
| ‎🟦 pretty |  ‎⬜ young   |  ‎⬜ essay  |    ‎🟥 apple     |  ‎⬜ kiss   |
+-----------+-------------+------------+-----------------+------------+
|  ‎⬜ poem  |  ‎🟦 solve   |   ‎🟥 pan   | ‎🟦 organization |  ‎🟦 union  |
+-----------+-------------+------------+-----------------+------------+
|  ‎🟥 myth  |   ‎🟥 neck   | ‎🟥 shelter |    ‎🟦 locate    |   ‎🟥 pet   |
+-----------+-------------+------------+-----------------+------------+
| ‎🟥 react  |  ‎⬜ person  |  ‎⬜ mood   |    ‎🟥 heart     | ‎⬜ breath  |
+-----------+-------------+------------+-----------------+------------+
|  ‎🟥 rich  | ‎🟦 standard |  ‎🟦 crop   |   ‎💀 chicken    | ‎🟦 wedding |
+-----------+-------------+------------+-----------------+------------+
Revealed cards: [14, 17, 18]
Enter hint word:
......

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.2.tar.gz (29.2 kB view details)

Uploaded Source

Built Distribution

codenames-5.0.2-py3-none-any.whl (35.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: codenames-5.0.2.tar.gz
  • Upload date:
  • Size: 29.2 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.2.tar.gz
Algorithm Hash digest
SHA256 e519e41dbc5f820231f76e1d5628ee48d0ba4e372c54931a3208323a23feb545
MD5 0e3f41aa592e4f1737d90beb000c4d81
BLAKE2b-256 df41889a637a92df4503c4f402120543d101ab230fe4fce0eac911279d9569d2

See more details on using hashes here.

Provenance

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

Publisher: pipeline.yml on asaf-kali/codenames

Attestations:

File details

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

File metadata

  • Download URL: codenames-5.0.2-py3-none-any.whl
  • Upload date:
  • Size: 35.3 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d37a5451bbf0e749d50cb7480ceba844a0f9f959ab89e4e99489c2e7f74cebd9
MD5 62df64ecfd2ef615b9e9e9e7a1f4ed86
BLAKE2b-256 cf4d5d22cd3a8a691abf130dae199db0b7b63776c752e11c0208de2d9a2bc655

See more details on using hashes here.

Provenance

The following attestation bundles were made for codenames-5.0.2-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