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 CodeRabbit Pull Request Reviews

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.

Game rules:

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.board import ClassicBoard
from codenames.classic.player import ClassicOperative, ClassicSpymaster
from codenames.classic.runner import ClassicGamePlayers, ClassicGameRunner
from codenames.classic.state import ClassicOperativeState, ClassicSpymasterState
from codenames.classic.team import ClassicTeam
from codenames.generic.move import Clue, Guess
from codenames.utils.vocabulary.languages import get_vocabulary

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


class CLISpymaster(ClassicSpymaster):
    def give_clue(self, game_state: ClassicSpymasterState) -> 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(ClassicOperative):
    def guess(self, game_state: ClassicOperativeState) -> 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)


############
# Run Game #
############

def run_classic_cli_game():
    # Init players
    blue_spymaster = CLISpymaster(name="Yoda", team=ClassicTeam.BLUE)
    blue_operative = CLIOperative(name="Luke", team=ClassicTeam.BLUE)
    red_spymaster = CLISpymaster(name="Einstein", team=ClassicTeam.RED)
    red_operative = CLIOperative(name="Newton", team=ClassicTeam.RED)
    players = ClassicGamePlayers.from_collection(blue_spymaster, blue_operative, red_spymaster, red_operative)
    # Init board
    vocabulary = get_vocabulary(language="english")
    board = ClassicBoard.from_vocabulary(vocabulary=vocabulary)
    # Run game
    runner = ClassicGameRunner(players=players, board=board)
    winner = runner.run_game()
    print(f"Winner: {winner}")


if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO, format="%(message)s", stream=sys.stdout)
    run_classic_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.5.5.tar.gz (38.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

codenames-5.5.5-py3-none-any.whl (49.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: codenames-5.5.5.tar.gz
  • Upload date:
  • Size: 38.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for codenames-5.5.5.tar.gz
Algorithm Hash digest
SHA256 922647a566a0d2d66466b23c5780194bb87e8555142753adf0534858dbab5af5
MD5 9c274e53d3202632b11331a110e8c20e
BLAKE2b-256 e149832a0f9781e9890323c812075603045ba0879dcde20550cd574bb2d7d5d7

See more details on using hashes here.

Provenance

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

Publisher: pipeline.yml on asaf-kali/codenames

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: codenames-5.5.5-py3-none-any.whl
  • Upload date:
  • Size: 49.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for codenames-5.5.5-py3-none-any.whl
Algorithm Hash digest
SHA256 80a413852fc349d5c4a4bb450c8de4690e9c145a4a3b0a75953a1125a9d3ecf8
MD5 74348def64c8abdf6225322bc278218c
BLAKE2b-256 ba3a45ae3970a223f5fd8cc3b32e50a89dcd0a4afc9e610806bea44604d37a49

See more details on using hashes here.

Provenance

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

Publisher: pipeline.yml on asaf-kali/codenames

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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