Exemplo n.º 1
0
    def start(self) -> None:

        self.__log('Let\'s start a Jass Game :)')

        trump_chooser: Player = None
        deck = Deck(
        )  # this reuses the same cards object -> slight memory/performance gain

        try:
            while True:

                #########################
                # GIVE CARDS TO PLAYERS #
                #########################

                for player, hand in zip(self.__players,
                                        deck.shuffle().give_hands()):
                    player.give(hand)

                ################
                # CHOOSE TRUMP #
                ################

                if trump_chooser is None:
                    for p in self.__players:
                        if p.has_7_diamonds():
                            trump_chooser = p
                            self.__log(
                                f'{p} has the {Card(7, Suit.diamonds)}!')
                            break

                trump = self.choose_trump(trump_chooser=trump_chooser)

                #####################
                # PLAY THE 9 TRICKS #
                #####################

                self.play_9_tricks(
                    trump=trump,
                    trump_chooser=trump_chooser,
                )

                ###########
                # RESTART #
                ###########

                trump_chooser = self.__next_player(trump_chooser)
                self.__log_score()

                self.n += 1
                if self.n % 20 == 0:
                    self.n = 0
                    print(
                        f'Score difference: {self.teams[0].score - self.teams[1].score}'
                    )

        except GameOver as e:
            self.__log(e)
            self.__log_score()
Exemplo n.º 2
0
    def test_init(self):
        self.assertEqual(len(self.hand.cards), 9)

        with self.assertRaises(ValueError):
            Hand(Deck.cards()[:10])

        with self.assertRaises(ValueError):
            Hand(Deck.cards()[:8])
Exemplo n.º 3
0
 def test_random_choose_trump_state(self):
     deck = Deck()
     for _ in range(100):
         cards = deck.shuffle().cards()
         state = ChooseTrumpState(
             hand=cards[:9],
             can_chibre=random.random() < 0.5,
         )
         self.assertEqual(state.tensor.nelement(), state.tensor_size)
Exemplo n.º 4
0
    def test_random_play_card_state(self):
        deck = Deck()
        for _ in range(100):
            cards = deck.shuffle().cards()

            num_cards_hand = random.randint(2, 9)
            num_card_on_table = random.randint(0, 3)
            round_idx = num_cards_hand + num_card_on_table

            state = PlayCardState(
                trick_trump=random.choice(list(Suit)),
                trump_chooser_idx=random.randint(0, 3),
                player_hand=cards[:num_cards_hand],
                playable_cards=random.choices(cards[:num_cards_hand],
                                              k=random.randint(
                                                  0, num_cards_hand)),
                trick_history=cards[num_cards_hand:num_cards_hand +
                                    num_card_on_table],
                round_history=[
                    cards[round_idx + i * 4:round_idx + (i + 1) * 4]
                    for i in range(9 - num_cards_hand)
                ],
            )
            self.assertEqual(state.tensor.nelement(), state.tensor_size)
Exemplo n.º 5
0
 def setUp(self) -> None:
     self.deck = Deck()
Exemplo n.º 6
0
 def test_deck_points(self):
     cards = Deck.cards()
     for trump in list(Suit):
         random.shuffle(cards)
         self.assertEqual(sum([card.points(trump) for card in cards]), 152)
Exemplo n.º 7
0
 def test_36(self):
     self.assertEqual(len(Deck.cards()), 36)
Exemplo n.º 8
0
 def setUp(self) -> None:
     self.hand = Hand(Deck.cards()[:9])
Exemplo n.º 9
0
 def test_hands(self):
     n_cards = 0
     for hand in Deck().shuffle().give_hands():
         self.assertIsInstance(hand, Hand)
         n_cards += len(hand.cards)
     self.assertEqual(n_cards, 36)