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.
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
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.0.4.tar.gz
.
File metadata
- Download URL: codenames-5.0.4.tar.gz
- Upload date:
- Size: 29.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ab7b1cf2e748a464ca01bbe88fe6a8c466efe385c37eda99cb593fc44e904ba1 |
|
MD5 | 18347ecd3a70f35e6a8338bf81e01acf |
|
BLAKE2b-256 | 564b78dd082bc94af046f0ee9dd9d54ca31ef75339c622a612ff1cf1be6a958e |
Provenance
The following attestation bundles were made for codenames-5.0.4.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.0.4.tar.gz
- Subject digest:
ab7b1cf2e748a464ca01bbe88fe6a8c466efe385c37eda99cb593fc44e904ba1
- Sigstore transparency entry: 148513332
- Sigstore integration time:
- Predicate type:
File details
Details for the file codenames-5.0.4-py3-none-any.whl
.
File metadata
- Download URL: codenames-5.0.4-py3-none-any.whl
- Upload date:
- Size: 35.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 | f10a1611c84f728e9bce44443c8a5c36ee74dc5a64004dafef810e12a09fbf40 |
|
MD5 | 9d7e3d2a002864d40697002457b15b7b |
|
BLAKE2b-256 | 5681d5ed4cf7fa01a75bf69cdf1dcb8419e72bd40badab9f26b91aae88a2fcb7 |
Provenance
The following attestation bundles were made for codenames-5.0.4-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.0.4-py3-none-any.whl
- Subject digest:
f10a1611c84f728e9bce44443c8a5c36ee74dc5a64004dafef810e12a09fbf40
- Sigstore transparency entry: 148513334
- Sigstore integration time:
- Predicate type: