Exemple #1
0
    def test_deck_random_shuffle(self) -> None:
        deck_1, deck_2 = Deck(), Deck()

        deck_1.shuffle()
        deck_2.shuffle()

        # This is technically a flaky test since both decks
        # could be identical, but the odds of this are 1 in 52!.
        self.assertNotEqual(deck_1.cards, deck_2.cards)
Exemple #2
0
    def test_deck_reshuffle_is_possible(self) -> None:
        deck_1, deck_2 = Deck(), Deck()
        seed = random.randint(0, 100000)

        deck_1.shuffle(seed)
        deck_2.shuffle(seed)

        # Shuffling twice should yield a different deck.
        # This is technically a flaky test but the odds of this test
        # failing despite functioning as intended is 1 in 52!.
        deck_2.shuffle(seed)
        self.assertNotEqual(deck_1.cards, deck_2.cards)
Exemple #3
0
    def test_deck_shuffle_with_seed(self) -> None:
        deck_1, deck_2, deck_3 = Deck(), Deck(), Deck()
        seed_1, seed_2 = random.randint(0, 100000), random.randint(0, 100000)
        deck_1.shuffle(seed_1)
        deck_2.shuffle(seed_1)
        deck_3.shuffle(seed_2)

        # Identical seeds must lead to identical shuffles.
        self.assertEqual(deck_1.cards, deck_2.cards)

        # Different seeds should lead to different shuffles.
        # This is technically a flaky test but the odds of this test
        # failing despite functioning as intended is 1 in 52!.
        self.assertNotEqual(deck_1.cards, deck_3.cards)
    def test_get_foak_probability():

        foak_count = 0
        iterations = 100000

        for i in range(0, iterations):
            deck = Deck(custom_suits={"𓆏": "GREEN", "𓃰": "GREY"})

            deck.double_cards()
            deck.shuffle()
            deck.remove_cards(lambda_statement=lambda x: x.rank != "A",
                              index=13)

            if deck.evaluator.has_four_of_a_kind():
                foak_count += 1

        print(
            "The probability of having a four-of-a-kind given this custom deck is around {} %."
            .format(foak_count / iterations * 100))
    def test_get_straights_probability():

        for i in range(3, 9):
            straight_count = 0
            straight_length = i
            iterations = 50000

            for j in range(0, iterations):
                deck = Deck(custom_suits={"𓆏": "GREEN", "𓃰": "GREY"})

                deck.double_cards()
                deck.shuffle()
                deck.remove_cards(lambda_statement=lambda x: x.rank != "A",
                                  index=13)

                if deck.evaluator.has_straight(straight_length):
                    straight_count += 1

            print(
                "The probability of this deck containing a {}-card straight is around {} %."
                .format(straight_length, straight_count / iterations * 100))
    def test_get_diminishing_straights_probabibilty():
        """
        Given a custom deck, calculate the probability of the deck having a 3-card straight.
        Then remove the lowest cards, one by one, and calculate the probabilities again, etc.
        """

        straight_length = 3
        max_iterations = 50000

        straight_count_frequencies = dict()

        for i in range(3, 13):
            straight_count_frequencies[i] = 0

        for i in range(max_iterations):

            deck = Deck(custom_suits={"𓆏": "GREEN", "𓃰": "GREY"})

            deck.double_cards()
            deck.shuffle()
            deck.remove_cards(lambda_statement=lambda x: x.rank != "A",
                              index=13)

            hand = deck.get_sample_hand(12)

            while len(hand.cards) >= straight_length:
                if hand.evaluator.has_straight(straight_length):
                    straight_count_frequencies[len(hand.cards)] += 1

                hand.remove_lowest_ranked_card()

        for i in sorted(straight_count_frequencies.keys()):
            print(
                "The probability of a hand containing a {}-card straight given its {} highest cards is around {} %."
                .format(straight_length, i,
                        straight_count_frequencies[i] / max_iterations * 100))
Exemple #7
0
player.money = Decimal(money)
player.start_money = player.money
bet = 0

# Building the Deck with the number of packs of cards specified
while True:
    try:
        packs = int(
            input("How many packs of cards should make up your deck? [1-10] "))
        if packs < 0:
            raise ValueError()
        break
    except ValueError:
        print("{} is not a valid number of packs. They will throw you out of Vegas for that kinda crap".format(packs))
deck = Deck(packs)
deck.shuffle()

# Setting the play variable and the game counter
play_another = 'y'
while play_another != 'n':
    # clear screen between rounds
    clearscreen()

    if (deck.cards_left() < 10):
        print(
            "There were only {} cards left in the deck, so the dealer reshuffled.".format(
                deck.cards_left()))
        deck = Deck(packs)
        deck.shuffle()
    player_hand = Hand()
    dealer_hand = Hand()
Exemple #8
0
class Game:
    def __init__(self):
        self.scores = {}
        self.player_names = []
        self.hands = {}
        self.green_deck = Deck('green')
        self.green_deck.shuffle()
        self.red_deck = Deck('red')
        self.red_deck.shuffle()

    def play(self):
        self.ascii_art_title()
        self.set_num_players()
        self.set_points_threshold()
        self.name_players()
        self.deal_hands()
        playing = True
        round = 0
        judge = self.player_names[0]

        while playing:
            print('*****************************************')
            print(judge.capitalize() + ' is the judge for this round.')
            # choose a new green card
            green_card = self.green_deck.deal()
            print('\nThe green card is ' + green_card.get_value())
            print('Description: ' + green_card.get_description())

            self.show_scores()
            choices = self.pick_cards(judge, green_card)
            self.judge_round(judge, green_card, choices)

            winner = self.check_win()
            if winner is not None:
                playing = False
                print('\n' + winner.capitalize() + ' JUST WON THE GAME!')
                self.show_scores()
                self.ascii_art_end()

            round += 1
            judge = list(self.scores.keys())[round % int(self.num_players)]

    def ascii_art_title(self):
        print(
            '     _                _\n' +
            '    / \\   _ __  _ __ | | ___  ___\n' +
            '   / _ \\ | \'_ \\| \'_ \\| |/ _ \\/ __|\n' +
            '  / ___ \\| |_) | |_) | |  __/\\__ \\\n' +
            ' /_/   \\_\\ .__/| .__/|_|\\___||___/\n' +
            '         |_| _ |_|\n' +
            '            | |_ ___\n' +
            '            | __/ _ \\\n' +
            '            | || (_) |\n' +
            '     _       \\__\\___/ _\n' +
            '    / \\   _ __  _ __ | | ___  ___\n' +
            '   / _ \\ | \'_ \\| \'_ \\| |/ _ \\/ __|\n' +
            '  / ___ \\| |_) | |_) | |  __/\\__ \\\n' +
            ' /_/   \\_\\ .__/| .__/|_|\\___||___/\n' +
            '         |_|   |_|\n'
        )
        print('by SARAH RAINES')

    def set_num_players(self):
        print('\nSET UP A NEW GAME:')
        prompt = \
            'Please select the number of players between 3-6. '
        prompt_again = \
            'Not a valid input. Please enter a number between 3-6. '

        self.num_players = input(prompt).strip()
        while self.num_players not in ['3', '4', '5', '6']:
            self.num_players = input(prompt_again).strip()

    def set_points_threshold(self):
        points_prompt = \
            'Please select a winning points threshold between 1-100. '
        points_prompt_again = \
            'Not a valid input. Please enter a number between 1-100. '

        self.points_threshold = input(points_prompt).strip()
        while not self.points_threshold.isdigit():
            self.points_threshold = input(points_prompt_again).strip()
        while int(self.points_threshold) > 100 or int(self.points_threshold) < 1:
            self.points_threshold = input(points_prompt_again).strip()

    def name_players(self):
        name_prompt = \
            'Please enter a name for Player '
        name_repeat_prompt = \
            'Cannot use the same name for two players.\n' +  \
            'Please enter a new name for Player '

        for i in range(int(self.num_players)):
            player_name = input(name_prompt + str(i+1) + ' ').strip().lower()
            while player_name == '':
                player_name = \
                    input(name_prompt + str(i+1) + ' ').strip().lower()
            while player_name in self.player_names:
                player_name = \
                    input(name_repeat_prompt + str(i+1) + ' ').strip().lower()
            self.player_names.append(player_name)

    def deal_hands(self):
        for player in self.player_names:
            self.scores[player] = 0
            dealt_hand = self.red_deck.deal_hand()
            new_hand = Hand(player.lower(), dealt_hand)
            self.hands[player] = new_hand

    def pick_cards(self, judge, green_card):
        choices = {}
        for hand in self.hands.items():
            player_name = hand[0]
            # select a card for every non-dealer hand
            if player_name != judge:
                chosen_card = hand[1].choose_card(green_card)
                choices[player_name] = chosen_card
                # deal a new card to every non-dealer hand
                card_drawn = self.red_deck.deal()
                hand[1].add_card(card_drawn)

        return choices

    def judge_round(self, judge, green_card, choices):
        round_winner = ''
        card_winner = ''
        print('-----------------------------------------')
        print('Time to judge, ' + judge.capitalize() + '!')
        print(
            'Select a card that best fits the adjective ' +
            green_card.get_value() +
            '\n'
        )
        
        for choice in choices.items():
            print(
                choice[0].capitalize()
                + ' selected the card '
                + choice[1].get_value()
            )
            print('Description: ' + choice[1].get_description())
            print('Type ' + choice[0] + ' to select this card.\n')

        non_judges = self.player_names.copy()
        non_judges.remove(judge)

        prompt = 'Please choose [ ' + ' / '.join(non_judges) + ' ] '
        prompt_again = \
            'Not a valid input. Please enter one of [ ' + \
            ' / '.join(non_judges) + \
            ' ] '

        round_winner = input(prompt).strip().lower()
        while round_winner not in non_judges:
            round_winner = input(prompt_again).strip().lower()

        card_winner = choices[round_winner].get_value()
        self.scores[round_winner] += 1
        print(
            round_winner.capitalize() +
            ' won the round with the card ' +
            card_winner +
            '!'
        )

    def check_win(self):
        for score in self.scores.items():
            if score[1] >= int(self.points_threshold):
                return(score[0])
        return None

    def show_scores(self):
        print('\nSCORES:')
        for score in self.scores.items():
            print(score[0].capitalize() + ': ' + str(score[1]))

    def ascii_art_end(self):
        print(
            ' _____ _   _ _____   _____ _   _ ____\n' +
            '|_   _| | | | ____| | ____| \\ | |  _ \\\n' +
            '  | | | |_| |  _|   |  _| |  \\| | | | |\n' +
            '  | | |  _  | |___  | |___| |\\  | |_| |\n' +
            '  |_| |_| |_|_____| |_____|_| \\_|____/\n'
        )