Skip to main content

Codenames board game logic implementation in python.

Project description

Codenames

Pipeline PyPI version

Code infrastructure for the Codenames board game.
Designed to serve as a base for implementing different Codenames players (solvers).

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.boards.builder import generate_board
from codenames.game.move import Guess, Hint
from codenames.game.player import Guesser, Hinter
from codenames.game.runner import GameRunner
from codenames.game.state import GuesserGameState, HinterGameState

logging.basicConfig(level=logging.INFO, format="%(message)s", stream=sys.stdout)


# Implement basic players

class CLIHinter(Hinter):
    def pick_hint(self, game_state: HinterGameState) -> Hint:
        print("Board state: \n" + game_state.board.printable_string)
        hint_word = input("Enter hint word: ")
        card_amount = int(input("Enter card amount: "))
        return Hint(word=hint_word, card_amount=card_amount)


class CLIGuesser(Guesser):
    def guess(self, game_state: GuesserGameState) -> Guess:
        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_cli_game():
    language = "english"
    board = generate_board(language=language)
    red_hinter, red_guesser = CLIHinter(name="Einstein"), CLIGuesser(name="Newton")
    blue_hinter, blue_guesser = CLIHinter(name="Yoda"), CLIGuesser(name="Luke")
    runner = GameRunner(
        blue_hinter=blue_hinter,
        blue_guesser=blue_guesser,
        red_hinter=red_hinter,
        red_guesser=red_guesser,
    )
    runner.run_game(board=board)


if __name__ == "__main__":
    run_cli_game()

Example output:

[Blue] turn.
Board state: 
+------------+------------+--------------+-------------+-------------------+
|   ‎🟦 tax   |  ‎⬜ drama  |   ‎⬜ thick   |  ‎🟥 africa  | ‎💀 transformation |
+------------+------------+--------------+-------------+-------------------+
| ‎⬜ project | ‎🟦 athlete | ‎🟥 vegetable | ‎⬜ engineer |     ‎🟥 human      |
+------------+------------+--------------+-------------+-------------------+
|  ‎⬜ chain  |  ‎🟦 cake   |   ‎🟦 shift   |  ‎🟦 study   |      ‎🟥 will      |
+------------+------------+--------------+-------------+-------------------+
| ‎🟥 outcome |  ‎🟥 desk   |  ‎🟥 soviet   |   ‎⬜ rare   |     ‎🟥 youth      |
+------------+------------+--------------+-------------+-------------------+
| ‎🟦 account | ‎🟦 couple  |   ‎⬜ solve   | ‎🟦 academic |     ‎🟦 stable     |
+------------+------------+--------------+-------------+-------------------+
Enter hint word: example
Enter card amount: 2
Hinter: [example] 2 card(s)
+---------+---------+-----------+----------+----------------+
|   ‎tax   |  ‎drama  |   ‎thick   |  ‎africa  | ‎transformation |
+---------+---------+-----------+----------+----------------+
| ‎project | ‎athlete | ‎vegetable | ‎engineer |     ‎human      |
+---------+---------+-----------+----------+----------------+
|  ‎chain  |  ‎cake   |   ‎shift   |  ‎study   |      ‎will      |
+---------+---------+-----------+----------+----------------+
| ‎outcome |  ‎desk   |  ‎soviet   |   ‎rare   |     ‎youth      |
+---------+---------+-----------+----------+----------------+
| ‎account | ‎couple  |   ‎solve   | ‎academic |     ‎stable     |
+---------+---------+-----------+----------+----------------+
Enter card word: account
Guesser: '🟦 account', correct!
+------------+---------+-----------+----------+----------------+
|    ‎tax     |  ‎drama  |   ‎thick   |  ‎africa  | ‎transformation |
+------------+---------+-----------+----------+----------------+
|  ‎project   | ‎athlete | ‎vegetable | ‎engineer |     ‎human      |
+------------+---------+-----------+----------+----------------+
|   ‎chain    |  ‎cake   |   ‎shift   |  ‎study   |      ‎will      |
+------------+---------+-----------+----------+----------------+
|  ‎outcome   |  ‎desk   |  ‎soviet   |   ‎rare   |     ‎youth      |
+------------+---------+-----------+----------+----------------+
| ‎🟦 account | ‎couple  |   ‎solve   | ‎academic |     ‎stable     |
+------------+---------+-----------+----------+----------------+
Enter card word: rare
Guesser: '⬜ rare', wrong!
Guesser wrong, turn is over
...

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

Uploaded Source

Built Distribution

codenames-4.0.2-py3-none-any.whl (36.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: codenames-4.0.2.tar.gz
  • Upload date:
  • Size: 28.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for codenames-4.0.2.tar.gz
Algorithm Hash digest
SHA256 38b56104a5210cd3109384375289b30cf2529ef0fe96103683f4c37c9cab838e
MD5 b0e816c8e6c314e7a661f848cbdd23d6
BLAKE2b-256 e38208b39a331e20ecab8614770164e2dd5f514f1c5a7d4f6e8a63f75d8420c4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codenames-4.0.2-py3-none-any.whl
  • Upload date:
  • Size: 36.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for codenames-4.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 18181fbfeab37827ab353efd622475cb1f45d01f14fb8ed3a4f18c7cf75ed296
MD5 de43418cc31e625d203b5a7bffe4b611
BLAKE2b-256 558f3e20d1827268d46d05f39eb41232bf15877c0bdb6a724942454aa1c2d7d4

See more details on using hashes here.

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