Skip to main content

bridgepy is a python package for playing floating bridge!

Project description

bridgepy

bridgepy is a Python package for playing floating bridge!

Visit repository on Github


Game state

The heart of this project is in the game state below


Quick Guide

from bridgepy.datastore import Datastore
from bridgepy.player import PlayerId, PlayerAction
from bridgepy.game import GameId, Game
from bridgepy.card import Card
from bridgepy.bid import Bid
from bridgepy.bridge import BridgeClient

Step 1: Create your own datastore for Game and override insert, update, delete, and query

In this example, is a local datastore

class GameLocalDataStore(Datastore[GameId, Game]):

    def __init__(self) -> None:
        self.games: list[Game] = []

    def insert(self, game: Game) -> None:
        i = self.__query_index(game.id)
        if i is not None:
            return
        self.games.append(game)

    def update(self, game: Game) -> None:
        i = self.__query_index(game.id)
        if i is None:
            return
        self.games = self.games[:i] + [game] + self.games[i+1:]

    def delete(self, id: GameId) -> None:
        i = self.__query_index(id)
        if i is None:
            return
        self.games = self.games[:i] + self.games[i+1:]

    def query(self, id: GameId) -> Game | None:
        i = self.__query_index(id)
        if i is None:
            return None
        return self.games[i]

    def __query_index(self, id: GameId) -> int | None:
        for i in range(len(self.games)):
            if self.games[i].id == id:
                return i
        return None

Step 2: Inject your game datastore to bridge client

In this example, is injecting the local datastore to the bridge client

game_datastore = GameLocalDataStore()
client = BridgeClient(game_datastore)

Step 3: Let 1 player create a game, and let 3 other players join the game

game_id = GameId("1")
player_id1 = PlayerId("111")
player_id2 = PlayerId("222")
player_id3 = PlayerId("333")
player_id4 = PlayerId("444")

client.create_game(player_id1, game_id)
client.join_game(player_id2, game_id)
client.join_game(player_id3, game_id)
client.join_game(player_id4, game_id)

Step 4: Let players bid, choose partner, and trick

player_id = player_id2 # replace with player_id1, player_id2, player_id3, player_id4
# player polls for their game snapshot (maybe every 5 seconds)
snapshot = client.view_game(player_id, game_id)
print(snapshot)
if PlayerAction.BID in snapshot.player_actions:
    print("bid turn")
    bid_input = input() # 1C for 1 club, 2NT for 2 no trump, None for pass
    bid = None if bid_input == "" else Bid.from_string(bid_input)
    client.bid(player_id, game_id, bid)
if PlayerAction.CHOOSE_PARTNER in snapshot.player_actions:
    print("choose partner")
    partner_input = input() # AC for ace club, 2H for 2 heart, 10S for 10 spade
    client.choose_partner(player_id, game_id, Card.from_string(partner_input))
if PlayerAction.TRICK in snapshot.player_actions:
    print("trick turn")
    trick_input = input() # AC for ace club, 2H for 2 heart, 10S for 10 spade
    client.trick(player_id, game_id, Card.from_string(trick_input))
if PlayerAction.RESET in snapshot.player_actions:
    print("reset")
    reset_input = input() # y for yes
    if reset_input == "y":
        client.reset_game(player_id, game_id)

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

bridgepy-0.0.12.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

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

bridgepy-0.0.12-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file bridgepy-0.0.12.tar.gz.

File metadata

  • Download URL: bridgepy-0.0.12.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for bridgepy-0.0.12.tar.gz
Algorithm Hash digest
SHA256 c794ffb2d9341d1a202c2cc23f7dc4e7c76b14ca8b0cf93404e2b4459c48a009
MD5 313902291c861e79eadb97d7e1bdf8d1
BLAKE2b-256 3a525fcb00a4f20743166518ad8b46def54bde32c684fac6bb1cae5953029e0a

See more details on using hashes here.

File details

Details for the file bridgepy-0.0.12-py3-none-any.whl.

File metadata

  • Download URL: bridgepy-0.0.12-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for bridgepy-0.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 7baebe20da7abe16360c9e18b05da7787c68ecf56441f8c447bebfbd7eda6167
MD5 fe6ca5049de24205b5b910e152b8672d
BLAKE2b-256 441e26de0dcb355ed9d68fadd1f6cb8b977e0a45c32eb009e15f2777e620caba

See more details on using hashes here.

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