Codenames board game logic implementation in python.
Project description
Codenames
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.color import ClassicTeam
from codenames.classic.runner import ClassicGamePlayers, ClassicGameRunner
from codenames.generic.move import Clue, Guess
from codenames.generic.player import Operative, Spymaster
from codenames.generic.state import OperativeState, SpymasterState
from codenames.utils.vocabulary.languages import get_vocabulary
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)
############
# 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__":
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file codenames-5.1.0.tar.gz
.
File metadata
- Download URL: codenames-5.1.0.tar.gz
- Upload date:
- Size: 34.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 53baa64ce604b57d30af8a4791622fa78cbf129919c18952dcfaddfb8db15f52 |
|
MD5 | 971838787ac71571dacb69db4a34feff |
|
BLAKE2b-256 | 29c7224262850166827e3ae8ed68528885b009866cd1fc632b533ab3e4c29828 |
Provenance
The following attestation bundles were made for codenames-5.1.0.tar.gz
:
Publisher:
pipeline.yml
on asaf-kali/codenames
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
codenames-5.1.0.tar.gz
- Subject digest:
53baa64ce604b57d30af8a4791622fa78cbf129919c18952dcfaddfb8db15f52
- Sigstore transparency entry: 149285875
- Sigstore integration time:
- Predicate type:
File details
Details for the file codenames-5.1.0-py3-none-any.whl
.
File metadata
- Download URL: codenames-5.1.0-py3-none-any.whl
- Upload date:
- Size: 43.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f65424c118137f25ec2d4c9147d85cbc1f283860a429a590860014090f7504ba |
|
MD5 | 275cb880376b04ef49c7c0912eafc9ad |
|
BLAKE2b-256 | 20124fb2828dcb884a83b0394ebc2ce9ae6a184f148f6f739f2c56e2102cc1fe |
Provenance
The following attestation bundles were made for codenames-5.1.0-py3-none-any.whl
:
Publisher:
pipeline.yml
on asaf-kali/codenames
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
codenames-5.1.0-py3-none-any.whl
- Subject digest:
f65424c118137f25ec2d4c9147d85cbc1f283860a429a590860014090f7504ba
- Sigstore transparency entry: 149285876
- Sigstore integration time:
- Predicate type: