Ejemplo n.º 1
0
def prev_tricks():
    first_trick = Trick(leading_player_index=0,
                        cards=[(OBER, ACORNS), (ACE, HEARTS), (KING, HEARTS),
                               (EIGHT, HEARTS)],
                        winner=0,
                        score=18)
    sec_trick = Trick(leading_player_index=0,
                      cards=[(UNTER, BELLS), (OBER, LEAVES), (UNTER, LEAVES),
                             (NINE, HEARTS)],
                      winner=1,
                      score=7)
    return [first_trick, sec_trick]
Ejemplo n.º 2
0
def test_play_wenz(wenz_game):
    assert not wenz_game.finished()
    wenz_game.play()
    assert wenz_game.bidding_game.finished()
    assert wenz_game.bidding_game.game_mode == (WENZ, None)
    assert wenz_game.trick_game.finished()
    assert len(wenz_game.trick_game.tricks) == 8
    trick = Trick(leading_player_index=3,
                  cards=[(KING, LEAVES), (KING, BELLS), (TEN, HEARTS),
                         (NINE, HEARTS)],
                  winner=2,
                  score=18)
    trick.current_player_index = 2
    assert wenz_game.trick_game.tricks[-1] == trick
Ejemplo n.º 3
0
def test_play_partner_mode(partner_game):
    assert not partner_game.finished()
    partner_game.play()
    assert partner_game.bidding_game.finished()
    assert partner_game.trick_game.offensive_players == [1, 0]
    assert partner_game.trick_game.finished()
    assert len(partner_game.trick_game.tricks) == 8
    trick = Trick(leading_player_index=1,
                  cards=[(NINE, ACORNS), (SEVEN, BELLS), (KING, ACORNS),
                         (TEN, BELLS)],
                  winner=3,
                  score=14)
    trick.current_player_index = 0
    assert partner_game.trick_game.tricks[-1] == trick
Ejemplo n.º 4
0
def test_play_solo(solo_game):
    assert not solo_game.finished()
    solo_game.play()
    assert solo_game.bidding_game.finished()
    assert solo_game.bidding_game.game_mode == (SOLO, ACORNS)
    assert solo_game.trick_game.finished()
    assert len(solo_game.trick_game.tricks) == 8
    trick = Trick(leading_player_index=1,
                  cards=[(SEVEN, BELLS), (TEN, BELLS), (NINE, BELLS),
                         (ACE, BELLS)],
                  winner=3,
                  score=21)
    trick.current_player_index = 0
    assert solo_game.trick_game.tricks[2] == trick
Ejemplo n.º 5
0
    def sample_game_state(self, public_info):
        # sample opponent hands
        if public_info["current_trick"] is None:
            current_trick = Trick(
                leading_player_index=public_info["leading_player_index"])
        else:
            current_trick = public_info["current_trick"]

        player_hands = sample_opponent_hands(
            tricks=public_info["tricks"],
            current_trick=current_trick,
            trumpcards=public_info["trumpcards"],
            playerindex=public_info["current_player_index"],
            player_hand=self.hand)

        # recreate possible mode proposals from public info
        mode_proposals = sample_mode_proposals(public_info)

        # add player_hands and possible actions to game state
        game_state = deepcopy(public_info)
        game_state["mode_proposals"] = mode_proposals
        game_state["player_hands"] = player_hands
        game = Game(game_state=game_state,
                    players=[
                        RandomPlayer(),
                        RandomPlayer(),
                        RandomPlayer(),
                        RandomPlayer()
                    ])
        game_state["possible_actions"] = game.get_possible_actions()

        return game_state
Ejemplo n.º 6
0
def test_possible_cards(trick_game_during):
    curr_trick = trick_game_during.current_trick
    hand = trick_game_during.playerlist[3].get_hand()
    assert trick_game_during.possible_cards(current_trick=curr_trick,
                                            hand=hand) == [(UNTER, HEARTS),
                                                           (ACE, LEAVES),
                                                           (NINE, LEAVES),
                                                           (EIGHT, LEAVES),
                                                           (TEN, BELLS),
                                                           (EIGHT, BELLS)]
    hand = trick_game_during.playerlist[0].get_hand()
    assert trick_game_during.possible_cards(current_trick=curr_trick,
                                            hand=hand) == [(TEN, ACORNS),
                                                           (NINE, ACORNS),
                                                           (SEVEN, ACORNS)]
    curr_trick = Trick(leading_player_index=0,
                       cards=[(SEVEN, HEARTS), None, None, None])
    hand = trick_game_during.playerlist[3].get_hand()
    assert trick_game_during.possible_cards(current_trick=curr_trick,
                                            hand=hand) == [(UNTER, HEARTS)]
    hand = trick_game_during.playerlist[1].get_hand()
    assert trick_game_during.possible_cards(current_trick=curr_trick,
                                            hand=hand) == [(OBER, HEARTS),
                                                           (UNTER, ACORNS),
                                                           (SEVEN, HEARTS)]
Ejemplo n.º 7
0
def get_possible_cards(game_mode, card_sequence, player_hand):
    num_cards_curr_trick = len(card_sequence) % 4
    if len(card_sequence) == 0:
        return player_hand
    else:
        first_card = card_sequence[-num_cards_curr_trick][0]
        leading_player = card_sequence[-num_cards_curr_trick][1]
        curr_trick = Trick(leading_player_index=leading_player)
        curr_trick.num_cards = num_cards_curr_trick
        curr_trick.cards[leading_player] = first_card
        previously_ran_away = False
        if game_mode == (PARTNER_MODE, ACORNS):
            num_tricks_before = len(card_sequence) // 4
            for card_index in range(0, num_tricks_before, 4):
                # check if ACORNS was played in that trick
                first_card = card_sequence[card_index][0]
                if first_card[1] == ACORNS and first_card[0] not in [UNTER, OBER]:
                    # if yes, check if ACE was played
                    cards_in_trick = [c[0] for c in card_sequence[card_index: card_index + 4]]
                    if (ACE, ACORNS) not in cards_in_trick:
                        previously_ran_away = True
        return possible_cards(game_mode, curr_trick, player_hand, previously_ran_away)
Ejemplo n.º 8
0
 def __init__(self, playerlist, game_state):
     self.playerlist = playerlist
     self.max_num_tricks = 8
     self.leading_player_index = game_state['leading_player_index']
     self.game_mode = game_state['game_mode']
     self.mode_proposals = game_state['mode_proposals']
     self.offensive_players = [game_state['declaring_player']]
     if self.game_mode[0] == PARTNER_MODE:
         for player in self.playerlist:
             if (7, self.game_mode[1]) in player.starting_hand:
                 self.offensive_players.append(
                     self.playerlist.index(player))
                 break
     self.trumpcards = define_trumpcards(self.game_mode)
     self.tricks = game_state['tricks']
     if game_state['current_trick'] is not None:
         self.current_trick = game_state['current_trick']
     else:
         self.current_trick = Trick(
             leading_player_index=self.leading_player_index)
     self.current_player_index = self.current_trick.current_player_index
     self.scores = [0 for player in playerlist]
     for trick in game_state["tricks"]:
         self.scores[trick.winner] += trick.calculate_points()
Ejemplo n.º 9
0
class TrickGame:
    def __init__(self, playerlist, game_state):
        self.playerlist = playerlist
        self.max_num_tricks = 8
        self.leading_player_index = game_state['leading_player_index']
        self.game_mode = game_state['game_mode']
        self.mode_proposals = game_state['mode_proposals']
        self.offensive_players = [game_state['declaring_player']]
        if self.game_mode[0] == PARTNER_MODE:
            for player in self.playerlist:
                if (7, self.game_mode[1]) in player.starting_hand:
                    self.offensive_players.append(
                        self.playerlist.index(player))
                    break
        self.trumpcards = define_trumpcards(self.game_mode)
        self.tricks = game_state['tricks']
        if game_state['current_trick'] is not None:
            self.current_trick = game_state['current_trick']
        else:
            self.current_trick = Trick(
                leading_player_index=self.leading_player_index)
        self.current_player_index = self.current_trick.current_player_index
        self.scores = [0 for player in playerlist]
        for trick in game_state["tricks"]:
            self.scores[trick.winner] += trick.calculate_points()

    def next_player(self):
        self.current_player_index = (self.current_player_index + 1) % 4
        self.current_trick.current_player_index = self.current_player_index

    def get_current_player(self):
        return self.playerlist[self.current_player_index]

    def get_public_info(self):
        mode_proposals_public = []
        for proposal in self.mode_proposals:
            if proposal[0] == NO_GAME:
                mode_proposals_public.append(0)
            else:
                mode_proposals_public.append(1)
        return deepcopy({
            'leading_player_index': self.leading_player_index,
            'current_player_index': self.current_player_index,
            'mode_proposals': mode_proposals_public,
            'declaring_player': self.offensive_players[0],
            'game_mode': self.game_mode,
            'trumpcards': self.trumpcards,
            'tricks': self.tricks,
            'current_trick': self.current_trick
        })

    def suit_in_hand(self, suit, hand):
        suit_cards = [
            card for card in hand
            if card[1] == suit and card not in self.trumpcards
        ]
        if len(suit_cards) > 0:
            return suit_cards
        else:
            return hand

    def possible_cards(self, current_trick, hand):

        if current_trick.num_cards == 0:
            # " Check in case of PARTNER MODE if running away is possible"
            if self.game_mode[0] == PARTNER_MODE and (
                    ACE, self.game_mode[1]) in hand:
                if len(self.suit_in_hand(suit=self.game_mode[1],
                                         hand=hand)) < 4:
                    forbidden_cards = [
                        card for card in hand if card not in self.trumpcards
                        and card[1] == self.game_mode[1] and card[0] != 7
                    ]
                    poss_cards = [
                        card for card in hand if card not in forbidden_cards
                    ]
                else:
                    poss_cards = hand
            else:
                poss_cards = hand

        else:
            first_card = current_trick.cards[
                current_trick.leading_player_index]

            if first_card in self.trumpcards:
                players_trumpcards = [
                    trump for trump in self.trumpcards if trump in hand
                ]
                if len(players_trumpcards) > 0:
                    poss_cards = players_trumpcards
                else:
                    poss_cards = hand
            elif self.game_mode[0] == PARTNER_MODE and first_card[1] == self.game_mode[1] \
                    and (7, self.game_mode[1]) in hand:
                previously_ran_away = self.previously_ran_away()
                if not previously_ran_away:
                    poss_cards = [(7, self.game_mode[1])]
                else:
                    suit = first_card[1]
                    poss_cards = self.suit_in_hand(suit, hand)
            else:
                suit = first_card[1]
                poss_cards = self.suit_in_hand(suit, hand)

            # check if searched Ace can be played
            poss_cards = poss_cards[:]
            if self.game_mode[0] == PARTNER_MODE and (
                    ACE, self.game_mode[1]) in poss_cards:
                if not self.previously_ran_away() and len(poss_cards) > 1:
                    poss_cards.remove((ACE, self.game_mode[1]))

        return poss_cards[:]

    def reset_current_trick(self):
        self.tricks.append(self.current_trick)
        self.current_trick = Trick(self.current_player_index)

    def play_next_card(self):
        current_player = self.get_current_player()
        if self.current_trick.num_cards == 0:
            self.current_trick.leading_player_index = self.current_player_index
        options = self.possible_cards(self.current_trick,
                                      current_player.get_hand())
        info = self.get_public_info()
        next_card = current_player.play_card(public_info=info, options=options)
        assert next_card in options, 'Player {} tried to play non legal card'.format(
            current_player)
        self.current_trick.cards[self.current_player_index] = next_card
        self.current_trick.num_cards += 1

    def trick_finished(self):
        if self.current_trick.finished():
            self.current_trick.calculate_points()
            self.current_trick.determine_trickwinner(self.trumpcards)
            self.current_player_index = self.current_trick.winner
            self.scores[self.current_player_index] += self.current_trick.score
            self.reset_current_trick()
        else:
            self.next_player()

    def finished(self):
        if len(self.tricks
               ) == self.max_num_tricks or self.game_mode[0] == NO_GAME:
            return True
        else:
            return False

    def play(self):
        while not self.finished():
            self.play_next_card()
            self.trick_finished()

    def previously_ran_away(self):
        prev_ran_away = False
        partnerindex = self.offensive_players[1]
        searched_suit = self.game_mode[1]
        for trick in self.tricks:
            if trick.leading_player_index == partnerindex and trick.cards[
                    partnerindex][0] == searched_suit:
                prev_ran_away = True
                break
        return prev_ran_away
Ejemplo n.º 10
0
 def reset_current_trick(self):
     self.tricks.append(self.current_trick)
     self.current_trick = Trick(self.current_player_index)
Ejemplo n.º 11
0
def current_trick():
    curr_trick = Trick(leading_player_index=1,
                       cards=[None, (ACE, ACORNS), (EIGHT, ACORNS), None])
    return curr_trick
Ejemplo n.º 12
0
import pytest
from schafkopf.trick import Trick
import schafkopf.helpers as hlp
from schafkopf.suits import BELLS, HEARTS, LEAVES, ACORNS, SUITS
from schafkopf.ranks import SEVEN, EIGHT, NINE, UNTER, OBER, KING, TEN, ACE, RANKS


trumpcards = [(OBER, i) for i in SUITS] + [(UNTER, i) for i in SUITS] \
                    + [(ACE, HEARTS), (TEN, HEARTS), (KING, HEARTS), (NINE, HEARTS), (EIGHT, HEARTS), (SEVEN, HEARTS)]

first_trick = Trick(leading_player_index=0)
first_trick.cards = [(NINE, HEARTS), (UNTER, ACORNS), (OBER, HEARTS),
                     (TEN, BELLS)]
first_trick.score = 15
first_trick.winner = 2
sec_trick = Trick(leading_player_index=2)
sec_trick.cards = [(ACE, LEAVES), (ACE, HEARTS), (ACE, ACORNS),
                   (SEVEN, ACORNS)]
sec_trick.score = 26
sec_trick.winner = 1

tricks = [first_trick, sec_trick]

current_trick = Trick(leading_player_index=1)
current_trick.cards = [None, (UNTER, BELLS), (TEN, LEAVES), None]

playerindex = 1
player_hand = [(TEN, HEARTS), (OBER, LEAVES), (UNTER, LEAVES), (SEVEN, LEAVES),
               (OBER, BELLS)]