Example #1
0
class InputHandler:
    deck = Deck()
    trumpfs = [trumpf.name for trumpf in list(Trumpf)]

    nr_cards = 36
    nr_player = 4
    nr_trumpf_modes = 6

    pos_hand_cards = 0
    pos_table = 1 * nr_cards
    pos_player_played_card = [
        2 * nr_cards, 3 * nr_cards, 4 * nr_cards, 5 * nr_cards
    ]
    pos_trumpf = 6 * nr_cards

    input_size = 6 * nr_cards + nr_trumpf_modes
    output_size = nr_cards

    def __init__(self):
        self.state = None
        self.reset()

    def reset(self):
        self.state = np.zeros(self.input_size, dtype='float32')

    def update_state_stich(self, game_state, cards, player_id):
        table_list = analyse_table(game_state['table'])
        self.set_player(table_list, player_id)

    def update_state_choose_card(self, game_state, cards, player_id):
        table_list = analyse_table(game_state['table'])
        self.set_hand(cards)
        self.set_table(table_list)
        self.set_player(table_list, player_id)
        self.set_trumpf(trumpf=game_state['trumpf'])

    def set_player(self, table, current_player_id):
        self.state[self.pos_player_played_card[0]:self.
                   pos_player_played_card[len(self.pos_player_played_card) -
                                          1] + self.nr_cards] = 0.
        player_id = 0
        for id, card in table:
            if id != current_player_id:
                self.state[self.pos_player_played_card[player_id] +
                           card_to_index(card)] = 1.
                player_id += 1

    def set_table(self, table):
        for _, card in table:
            self.state[self.pos_table + card_to_index(card)] = 1.

    def set_hand(self, cards):
        self.state[self.pos_hand_cards:self.pos_hand_cards +
                   self.nr_cards] = 0.
        for card in cards:
            self.state[self.pos_hand_cards + card_to_index(card)] = 1.

    def set_trumpf(self, trumpf):
        self.state[self.pos_trumpf + self.trumpfs.index(trumpf)] = 1.
Example #2
0
def test_add_points(trumpf):
    round_points = 152
    deck = Deck()
    random_players = [RandomPlayer(name=i) for i in range(4)]
    team_1 = Team(players=[random_players[0], random_players[1]])
    team_2 = Team(players=[random_players[1], random_players[2]])
    teams = [team_1, team_2]
    game = Game(teams=teams, use_counting_factor=True)
    game.trumpf = trumpf
    game.add_points(team_index=0, cards=deck.cards, last=False)
    assert team_1.points == round_points * counting_factor[trumpf]
    game.use_counting_factor = False
    game.add_points(team_index=1, cards=deck.cards, last=False)
    assert team_2.points == round_points
Example #3
0
 def remaining_cards(self, gone):
     d = Deck()
     return [x for x in d.cards if x not in gone]
Example #4
0
class InputHandler:
    deck = Deck()
    trumpfs = [trumpf.name for trumpf in list(Trumpf)]

    nr_cards = 36
    nr_trumpf_modes = 6
    amount_players = 4
    amount_card_sets = 37

    pos_hand_cards = 0
    pos_player_on_table = [
        1 * nr_cards, 2 * nr_cards, 3 * nr_cards, 4 * nr_cards
    ]
    # pos_players_per_stich is the history
    pos_trumpf = amount_card_sets * nr_cards

    input_size = amount_card_sets * nr_cards + nr_trumpf_modes  # 1338 input neurons
    output_size = nr_cards

    def __init__(self):
        self.state = None
        self.reset()
        self.pos_players_per_stich = self.generate_player_per_stich(
            self.amount_players + 1, self.amount_card_sets)

    def reset(self):
        self.state = np.zeros(self.input_size, dtype='float32')

    def update_state_stich(self, game_state, cards, player_id):
        table_list = analyse_table(game_state['table'])
        self.set_history(table_list, cards)

    def update_state_choose_card(self, game_state, cards, player_id):
        table_list = analyse_table(game_state['table'])
        self.set_hand(cards)
        self.set_table(table_list)
        self.set_history(table_list, cards)
        self.set_trumpf(trumpf=game_state['trumpf'])

    def set_history(self, table, cards):
        max_history_stich = 8
        amount_hand_cards = len(cards)
        stich_number = max_history_stich - amount_hand_cards
        if stich_number < max_history_stich:
            for player_id, card in table:
                self.state[self.pos_players_per_stich[stich_number][player_id]
                           + card_to_index(card)] = 1.

    def set_table(self, table):
        self.state[self.pos_player_on_table[0]:self.pos_player_on_table[3] +
                   self.nr_cards] = 0
        for player_position, card in table:
            self.state[self.pos_player_on_table[player_position] +
                       card_to_index(card)] = 1.

    def set_hand(self, cards):
        self.state[self.pos_hand_cards:self.pos_hand_cards +
                   self.nr_cards] = 0.
        for card in cards:
            self.state[self.pos_hand_cards + card_to_index(card)] = 1.

    def set_trumpf(self, trumpf):
        self.state[self.pos_trumpf + self.trumpfs.index(trumpf)] = 1.

    def generate_player_per_stich(self, start_range, end_range):
        player_collector = []
        pos_players_in_stich = []

        for player in range(start_range, end_range):
            player_collector.append(player * self.nr_cards)
            if len(player_collector) == 4 or player == end_range:
                pos_players_in_stich.append(player_collector)
                player_collector = []

        return pos_players_in_stich
Example #5
0
def test_count_rules_with_last(trumpf):
    deck = Deck()
    points = count_stich(cards=deck.cards, trumpf=trumpf, last=True)
    assert points == 157
Example #6
0
def test_count_rules(trumpf):
    deck = Deck()
    points = count_stich(cards=deck.cards, trumpf=trumpf, last=False)
    assert points == 152
Example #7
0
 def __init__(self, players):
     self.players = players
     self.deck = Deck()
Example #8
0
class InputHandler:
    deck = Deck()
    trumpfs = [trumpf.name for trumpf in list(Trumpf)]

    nr_cards = 36
    nr_player = 4
    nr_trumpf_modes = 6

    pos_hand_cards = 0
    pos_table = 1 * nr_cards
    pos_player_played_card = [
        2 * nr_cards, 3 * nr_cards, 4 * nr_cards, 5 * nr_cards
    ]
    pos_trumpf = 6 * nr_cards

    network_size = 6 * nr_cards + nr_trumpf_modes
    input_size = 3 * network_size
    output_size = 3 * nr_cards

    def __init__(self):
        self.state = None
        self.network_preparation = None
        self.reset()

    def reset(self):
        self.state = np.zeros(self.input_size, dtype='float32')
        self.network_preparation = np.zeros(self.network_size, dtype='float32')

    def update_state_stich(self, game_state, cards, player_id):
        table_list = analyse_table(game_state['table'])
        self.set_player(table_list, player_id)

    def update_state_choose_card(self, game_state, cards, player_id):
        table_list = analyse_table(game_state['table'])
        self.set_hand(cards)
        self.set_table(table_list)
        self.set_player(table_list, player_id)
        self.set_trumpf(trumpf=game_state['trumpf'])
        self.state[self.pos_hand_cards:self.input_size - 1] = 0.

        amount_hand_cards = len(cards)
        if 10 > amount_hand_cards > 6:
            self.set_state(0)
        elif 0 < amount_hand_cards < 4:
            self.set_state(2)
        elif 3 < amount_hand_cards < 7:
            self.set_state(1)

        self.network_preparation[self.pos_hand_cards:self.network_size -
                                 1] = 0.

    def set_player(self, table, current_player_id):
        self.network_preparation[
            self.pos_player_played_card[0]:self.
            pos_player_played_card[len(self.pos_player_played_card) - 1] +
            self.nr_cards] = 0.
        player_id = 0
        for id, card in table:
            if id != current_player_id:
                self.network_preparation[self.pos_player_played_card[player_id]
                                         + card_to_index(card)] = 1.
                player_id += 1

    def set_table(self, table):
        self.network_preparation[self.pos_table:self.pos_table +
                                 self.nr_cards] = 0.
        for _, card in table:
            self.network_preparation[self.pos_table + card_to_index(card)] = 1.

    def set_hand(self, cards):
        self.network_preparation[self.pos_hand_cards:self.pos_hand_cards +
                                 self.nr_cards] = 0.
        for card in cards:
            self.network_preparation[self.pos_hand_cards +
                                     card_to_index(card)] = 1.

    def set_trumpf(self, trumpf):
        self.network_preparation[self.pos_trumpf +
                                 self.trumpfs.index(trumpf)] = 1.

    def set_state(self, network_number):
        network_preparation_list = self.network_preparation[
            0:self.network_size].tolist()
        for index in range(0, len(network_preparation_list)):
            self.state[network_number * self.network_size +
                       index] = network_preparation_list[index]
Example #9
0
def test_deck_count():
    deck = Deck()
    assert len(deck.cards) == 36
Example #10
0
def test_deck_unique():
    deck = Deck()
    deck_set = set(deck.cards)
    assert len(deck.cards) == len(deck_set)
Example #11
0
def test_deck_sort():
    deck = Deck()
    sorted(deck.cards)
    assert Card.names[deck.cards[0].value] == '6'
    assert Card.names[deck.cards[-1].value] == 'Ass'
Example #12
0
def test_deck_min():
    deck = Deck()
    card = min(deck.cards)
    assert card.value == 6
Example #13
0
def test_deck_max():
    deck = Deck()
    card = max(deck.cards)
    assert card.value == 14
Example #14
0
def test_split_card_values_by_suit():
    deck = Deck()
    suit_card_values = split_card_values_by_suit(deck.cards)
    assert len(suit_card_values) == 4
    assert 6 in suit_card_values[0][1]
Example #15
0
def test_split_cards_by_suit():
    deck = Deck()
    suit_cards = split_cards_by_suit(deck.cards)
    assert len(suit_cards) == 4
    assert Card(suit=Suit.ROSE, value=6) in suit_cards[0][1]