Skip to content

How to play a game with a customized hand?

Michael Cochez edited this page Jan 20, 2025 · 1 revision

To play a game with a customized hand, for example to debug, but also if you want to do experiments with specific biases in the cards, you can create an engine with your own HandGenerator. This can be done as follows:

import random

from schnapsen.bots import RandBot

from schnapsen.deck import Card, OrderedCardCollection
from schnapsen.game import (GamePlayEngine, Hand, HandGenerator, SchnapsenDeckGenerator,
                            SchnapsenGamePlayEngine, SchnapsenMoveValidator, SchnapsenTrickImplementer, SchnapsenTrickScorer, SimpleMoveRequester, Talon, )


def custom_hand_game():
    class MyHandGenerator(HandGenerator):
        def generateHands(self, cards: OrderedCardCollection) -> tuple[Hand, Hand, Talon]:
            hand1 = Hand([Card.ACE_CLUBS, Card.ACE_DIAMONDS, Card.ACE_HEARTS, Card.ACE_SPADES, Card.QUEEN_CLUBS])
            hand2 = Hand([Card.KING_CLUBS, Card.KING_DIAMONDS, Card.KING_HEARTS, Card.KING_SPADES, Card.QUEEN_DIAMONDS])
            talon = Talon([card for card in cards if card not in hand1 and card not in hand2])
            # you can also explicitly specify the content of the talon, but want to be sure all cards are somewhere.
            # the variable cards contains all cards in the deck used in Schnapsen
            return (hand1, hand2, talon)

    engine = GamePlayEngine(
        deck_generator=SchnapsenDeckGenerator(),
        hand_generator=MyHandGenerator(),
        trick_implementer=SchnapsenTrickImplementer(),
        move_requester=SimpleMoveRequester(),
        move_validator=SchnapsenMoveValidator(),
        trick_scorer=SchnapsenTrickScorer()
    )
    bot1 = RandBot(random.Random(464566))
    bot2 = RandBot(random.Random(464566))
    outcome = engine.play_game(bot1, bot2, random.Random(94))
    print(outcome)