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.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
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
922647a566a0d2d66466b23c5780194bb87e8555142753adf0534858dbab5af5
|
|
| MD5 |
9c274e53d3202632b11331a110e8c20e
|
|
| BLAKE2b-256 |
e149832a0f9781e9890323c812075603045ba0879dcde20550cd574bb2d7d5d7
|
Provenance
The following attestation bundles were made for codenames-5.5.5.tar.gz:
Publisher:
pipeline.yml on asaf-kali/codenames
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
codenames-5.5.5.tar.gz -
Subject digest:
922647a566a0d2d66466b23c5780194bb87e8555142753adf0534858dbab5af5 - Sigstore transparency entry: 186602472
- Sigstore integration time:
-
Permalink:
asaf-kali/codenames@4b95ca4188439e293795cf9321e43ecbfc7f35e7 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/asaf-kali
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pipeline.yml@4b95ca4188439e293795cf9321e43ecbfc7f35e7 -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80a413852fc349d5c4a4bb450c8de4690e9c145a4a3b0a75953a1125a9d3ecf8
|
|
| MD5 |
74348def64c8abdf6225322bc278218c
|
|
| BLAKE2b-256 |
ba3a45ae3970a223f5fd8cc3b32e50a89dcd0a4afc9e610806bea44604d37a49
|
Provenance
The following attestation bundles were made for codenames-5.5.5-py3-none-any.whl:
Publisher:
pipeline.yml on asaf-kali/codenames
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
codenames-5.5.5-py3-none-any.whl -
Subject digest:
80a413852fc349d5c4a4bb450c8de4690e9c145a4a3b0a75953a1125a9d3ecf8 - Sigstore transparency entry: 186602474
- Sigstore integration time:
-
Permalink:
asaf-kali/codenames@4b95ca4188439e293795cf9321e43ecbfc7f35e7 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/asaf-kali
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pipeline.yml@4b95ca4188439e293795cf9321e43ecbfc7f35e7 -
Trigger Event:
push
-
Statement type: