Exemplo n.º 1
0
class DeckTests(unittest.TestCase):

    def setUp(self):
        self.deck = Deck()

    def test_init(self):
        '''deck should have 52 cards'''
        self.assertTrue(isinstance(self.deck.cards, list))
        self.assertEqual(len(self.deck.cards), 52)

    def test_repr(self):
        """repr should return a string in the form of 'deck of cards'"""
        self.assertEqual(repr(self.deck), "Deck of 52 cards")

    def test_count(self):
        """count should return a count of the number of cards"""
        self.assertEqual(self.deck.count(), 52)
        self.deck.cards.pop()
        self.assertEqual(self.deck.count(), 51)

    def test_deal_sufficient_cards(self):
        """deal should deal the number of cards specified"""
        cards = self.deck._deal(10)
        self.assertEqual(len(cards), 10)
        self.assertEqual(self.deck.count(), 42)

    def test_deal_insufficient_cards(self):
        cards = self.deck._deal(100)
        self.assertEqual(len(cards), 52)
        self.assertEqual(self.deck.count(), 0)

    def test_deal_no_cards(self):
        self.deck._deal(self.deck.count())
        with self.assertRaises(ValueError):
            self.deck._deal(1)

    def test_deal_card(self):
        card = self.deck.cards[-1]
        dealt_card = self.deck.deal_card()
        self.assertEqual(card, dealt_card)
        self.assertEqual(self.deck.count(), 51)

    def test_deal_hand(self):
        cards = self.deck.deal_hand(20)
        self.assertEqual(len(cards),20)
        self.assertEqual(self.deck.count(),32)

    def test_shuffle_full_deck(self):
        cards = self.deck.cards[:]
        self.deck.shuffle()
        self.assertNotEqual(cards, self.deck.cards)
        self.assertEqual(self.deck.count(), 52)

    def test_shuffle_not_full_deck(self):
        self.deck._deal(1)
        with self.assertRaises(ValueError):
            self.deck.shuffle()
Exemplo n.º 2
0
class GolfGame:
    def __init__(self, num_rounds_per_episode=10):
        self.deck = Deck()
        self.reset()
        self.rounds_per_episode = num_rounds_per_episode

    def reset(self):
        self.round = -1
        self.new_hand()
        return self._get_state()

    def new_hand(self):
        self.deck.shuffle()
        self.hand = self.deck.draw_n(6)
        self.top_card = self.deck.draw()
        self.mask = [0, 0, 0, 0, 0, 0]
        flip = sample(range(0, 5), 2)
        self.mask[flip[0]] = 1
        self.mask[flip[1]] = 1

        self.round += 1

    def step(self, action):
        initial_score = self._get_hand_score()

        # Make sure we dont run out of cards
        if len(self.deck.deck) == 0:
            self.deck.shuffle()

        if action == 0:  # Put card back
            self.top_card = self.deck.draw()
        else:  # Replace card
            self.hand[action - 1] = self.top_card
            self.mask[action - 1] = 1
        next_state = self._get_state()
        reward = initial_score - self._get_hand_score()
        if all(self.mask):  # Every card is flipped over, restart
            self.new_hand()

        done = (self.round >= self.rounds_per_episode)

        return next_state, reward, done

    def _get_state(self):
        state = [self.top_card.value[1]]
        for i, card in enumerate(self.hand):
            if self.mask[i]:
                state.append(card.value[1])
            else:
                state.append(-1)
        return np.array(state)

    def _get_hand_score(self):
        hand_score = 0
        if not self.hand:
            return None
        for column in range(3):
            card1 = self.hand[column]
            card2 = self.hand[3 + column]
            if card1 != card2 and card1 != Card.Joker and card2 != Card.Joker:
                hand_score += card1.value[0] + card2.value[0]
        return hand_score
Exemplo n.º 3
0
class TestingDeckClass(unittest.TestCase):
    def setUp(self):
        self.deck = Deck()

    def test_init(self):
        """decks should have a cards attibute which is a list"""
        self.assertTrue(isinstance(self.deck.cards, list))
        self.assertEqual(len(self.deck.cards), 52)

    def test_deck_repr(self):
        """Test that repr returns a deck of 52 cards when first initialized, no more no less"""
        self.assertEqual(str(self.deck), "Deck of 52 cards")

    def test_count(self):
        """ count should return a count of the number of cards"""
        self.assertEqual(self.deck.count(), 52)
        self.deck.cards.pop()
        self.assertEqual(self.deck.count(), 51)

    def test_deal_sufficient_cards(self):
        """_deal should deal the number of cards specified"""
        cards = self.deck._deal(10)
        self.assertEqual(len(cards), 10)
        self.assertEqual(self.deck.count(), 42)

    def test_deal_insufficient_cards(self):
        """_deal should deal the number of cards left in the deck"""
        cards = self.deck._deal(100)
        self.assertEqual(len(cards), 52)
        self.assertEqual(self.deck.count(), 0)

    def test_deal_no_cards(self):
        """_deal should throw a ValueError if the deck is empty"""
        self.deck._deal(self.deck.count())
        with self.assertRaises(ValueError):
            self.deck._deal(1)

    def test_deal_card(self):
        """ deal_card whould deal a single card from the deck"""
        card = self.deck.cards[-1]
        dealt_card = self.deck.deal_card()
        self.assertEqual(card, dealt_card)
        self.assertEqual(self.deck.count(), 51)

    def test_deal_hand(self):
        """deal_hand should deal the number of cards passed"""
        cards = self.deck.deal_hand(20)
        self.assertEqual(len(cards), 20)
        self.assertEqual(self.deck.count(), 32)

    def test_shuffle_full_deck(self):
        """shuffle should shuffle the deck if the deck is full"""
        cards = self.deck.cards[:]
        self.deck.shuffle()
        self.assertNotEqual(cards, self.deck.cards)
        self.assertEqual(self.deck.count(), 52)

    def test_shuffle_not_full_deck(self):
        """shuffle should throw a ValueError if the deck isn't full"""
        self.deck._deal(1)
        with self.assertRaises(ValueError):
            self.deck.shuffle()
Exemplo n.º 4
0
class DeckTests(unittest.TestCase):
    def setUp(self):
        self.sample_deck = Deck()

    def test_init(self):
        self.assertIsInstance(self.sample_deck.cards, list)
        self.assertEqual(len(self.sample_deck.cards), 52)

    def test_repr(self):
        self.assertEqual(repr(self.sample_deck), "Deck of 52 cards")

    def test_iter(self):
        self.assertTrue(iter(self.sample_deck))

    def test_count(self):
        self.assertEqual(self.sample_deck.count(), 52)
        self.sample_deck.cards.pop()
        self.assertEqual(self.sample_deck.count(), 51)

    def test_deal_cards(self):
        self.sample_deck._deal(10)
        self.assertEqual(len(self.sample_deck.cards), 42)
        self.assertEqual(len(self.sample_deck._new_hand), 10)

    def test_deal_last(self):
        self.sample_deck._deal(56)
        self.assertEqual(len(self.sample_deck.cards), 0)
        self.assertEqual(len(self.sample_deck._new_hand), 52)

    def test_deal_no_cards(self):
        self.sample_deck.cards.clear()
        with self.assertRaises(ValueError):
            self.sample_deck._deal(2)

    def test_deal_card(self):
        result = self.sample_deck.deal_card()
        self.assertIsInstance(result, Card)
        self.assertEqual(result, self.sample_deck._new_hand[0])
        self.assertEqual(len(self.sample_deck.cards), 51)
        self.assertEqual(len(self.sample_deck._new_hand), 1)

    def test_deal_hand(self):

        result = self.sample_deck.deal_hand(15)
        self.assertIsInstance(result, list)
        self.assertEqual(len(self.sample_deck.cards), 37)
        self.assertEqual(len(self.sample_deck._new_hand), 15)

    def test_shuffle_invalid(self):
        """shuffle raises a ValueError if the card deck is not full"""
        self.sample_deck.cards.pop()
        with self.assertRaises(ValueError):
            self.sample_deck.shuffle()

    def test_shuffle_valid(self):
        """shuffle shuffles a full deck in place"""
        original = self.sample_deck.cards[:]  #making a new copy b/c lists are mutable!
        self.sample_deck.shuffle()
        result = self.sample_deck.cards
        self.assertEqual(len(result), 52)
        self.assertFalse(original == result)
Exemplo n.º 5
0
class Dealer():
    def __init__(self, channel):
        self._channel = channel
        self.deck = Deck()
        self.dealt_this_phase = False

    #region game
    async def deal_this_phase(self,
                              players,
                              state,
                              round_number,
                              community_cards=[]):
        print('Dealing.')
        #region deal
        if state == 'deal':
            # shuffle
            self.deck.shuffle(grab_discard=True,
                              community_cards=community_cards)
            await self._channel.send('Deck shuffled.', delete_after=120)
            # alert players of dealing
            await self._channel.send(
                f'Round {round_number}: Dealing to players...',
                delete_after=120)
            # deal, add cards to player hands, and dm them their cards
            for player in players:
                cards = []
                cards.append(self.deck.deal())
                cards.append(self.deck.deal())
                player.hand = cards
                await self._channel.send(
                    f'Dealing cards to {player.user.display_name}...',
                    delete_after=120)
                await player.user.send(
                    content=
                    f'{cards[0].suit}{cards[0].value} {cards[1].suit}{cards[1].value}'
                )
                print('Cards sent.')

            # cleanup
            self.dealt_this_phase = True
            await self._channel.send('Dealt.', delete_after=120)
        #endregion deal
        #region flop
        elif state == 'flop':
            # alert players
            await self._channel.send(
                f'Round {round_number}: Revealing the flop...')
            # deal
            cards = []

            self.deck.burn()  # burn one

            for i in range(0, 3):
                card = self.deck.deal()
                cards.append(card)
                community_cards.append(card)
            # reveal to players
            await self._channel.send(' '.join(
                [f'{card.suit}{card.value}' for card in community_cards]))
            # cleanup
            self.dealt_this_phase = True
            return community_cards
        #endregion flop
        #region turn
        elif state == 'turn':
            await self._channel.send(
                f'Round {round_number}: Revealing the turn...')
            self.deck.burn()
            card = self.deck.deal()
            community_cards.append(card)
            await self._channel.send(
                f"{' '.join([card.suit + card.value for card in community_cards])}"
            )
            self.dealt_this_phase = True
            return community_cards
        #endregion turn
        #region river
        elif state == 'river':
            await self._channel.send(
                f'Round {round_number}: Revealing the river...')
            self.deck.burn()
            card = self.deck.deal()
            community_cards.append(card)
            await self._channel.send(
                f"{' '.join([card.suit + card.value for card in community_cards])}"
            )
            self.dealt_this_phase = True
            return community_cards
        #endregion river
        else:
            return

    def move_blinds(self, players):
        small_index = 0
        big_index = 0
        end = len(players) - 1

        for i in range(0, len(players)):
            if players[i].is_small_blind:
                small_index = i
                players[i].is_small_blind = False
            elif players[i].is_big_blind:
                big_index = i
                players[i].is_big_blind = False

        if end == small_index:
            small_index = 0
        else:
            small_index = small_index + 1

        if end == big_index:
            big_index = 0
        else:
            big_index = big_index + 1

        players[small_index].is_small_blind = True
        players[big_index].is_big_blind = True

    #endregion game
    #region messaging
    async def send_betting_alert(self, next_bet, call, who_raised):
        check = ''
        if call == 0:
            check = "> n!holdem check\n"
        await self._channel.send(
            f"It is your turn, {next_bet.user.display_name}", delete_after=60)
        await self._channel.send(
            f"Minimum bet to stay: ${call - next_bet.bet_this_round} ({who_raised} raised)\n"
            + "Your options are:\n" +
            "> n!holdem bet [*a number (no dollar sign)*]\n" +
            "> n!holdem call\n" + f"{check}" + "> n!holdem fold",
            delete_after=60)
        await next_bet.user.send(f"It is your turn, {next_bet.user.mention}")

    async def send_betting_order(self, betting_order):
        await self._channel.send('The betting order is currently:\n',
                                 delete_after=45)
        for player in betting_order:
            blind = ''
            if player.is_small_blind:
                blind = ' (small blind)'
            elif player.is_big_blind:
                blind = ' (big blind)'
            await self._channel.send(f'{player.user.display_name}{blind}',
                                     delete_after=45)

    async def send_call_message(self, player, call, num, pot):
        await self._channel.send(
            f'{player.user.display_name} called the ${call} bet with ${num}.\n'
            +
            f"The pot is now ${pot}. ${player.bet_this_round} of that is {player.user.display_name}'s money.\n"
            + f"*{player.user.display_name} has ${player.money} left.*")

    async def send_card_reveal_messages(self):
        pass

    async def send_current_pot(self, pot):
        await self._channel.send(f"Current pot is ${pot}.", delete_after=120)

    async def send_money_message(self, player):
        await self._channel.send(
            f"{player.user.mention}, you have {player.money}.",
            delete_after=45)

    async def send_raise_message(self, player, call, num, pot):
        await self._channel.send(
            f"{player.user.display_name} raised the bet to ${call} with ${num}.\n"
            +
            f"The pot is now ${pot}. ${player.bet_this_round} of that is {player.user.display_name}'s money.\n"
            + f"{player.user.display_name} has ${player.money} left.")
Exemplo n.º 6
0
class Game:
    def __init__(self, players=None, num_rounds=9, verbose=True):
        if not players:
            players = [('AI1', PlayerType.Computer),
                       ('AI1', PlayerType.Computer)]
        self.verbose = verbose
        self.players = []
        for player in players:
            name, type_ = player
            self.players.append(Player(name, type_))
        self.round = 1
        self.num_rounds = num_rounds
        self.deck = Deck()
        self.top = None
        self.first_player = 0

    def run(self):
        while self.round <= self.num_rounds:
            if self.verbose:
                print('Round {}\n'.format(self.round))
            self._new_round()

            # Flip over initial cards
            for player in self.players:
                player.flip_cards()
                player.display_hand()

            # Go around the table until someone flips all of their cards over
            current_player = self.first_player
            playing = True
            while playing:
                if self.verbose:
                    print('{}\'s Turn'.format(
                        self.players[current_player].name))
                self.top = self.players[current_player].move(
                    self.deck, self.top, self.verbose)

                if self.players[current_player].is_done():
                    playing = False
                current_player = (current_player + 1) % len(self.players)

            # When round is over, add hand score to player scores and increment starting player
            for player in self.players:
                player.score += player.get_hand_score()
            self.first_player = (self.first_player + 1) % len(self.players)
            self.round += 1

        lowest_score = 10000
        winner = ''
        if self.verbose:
            print('Final Hands: ')
        for player in self.players:
            player.mask = [1, 1, 1, 1, 1, 1]
            if self.verbose:
                print('Player: {}'.format(player.name))
            player.display_hand()
        if self.verbose:
            print('Final Scores:')
        avg_score = 0
        for player in self.players:
            if self.verbose:
                print('\t {}: {}'.format(player.name, player.score))
            avg_score += player.score
            if player.score < lowest_score:
                winner = player.name
                lowest_score = player.score
            elif player.score == lowest_score:
                winner += ' and ' + player.name
        if self.verbose:
            print('The winner is... {}!'.format(winner))

        avg_score /= (len(self.players) * self.num_rounds)
        return avg_score

    def _new_round(self):
        self.deck.shuffle()
        for player in self.players:
            player.new_hand(self.deck.draw_n(6))
        self.top = self.deck.draw()
Exemplo n.º 7
0
class BlackJack:
    """Play black jack."""
    def __init__(self):
        """Initialize."""
        self.deck = Deck()
        self.deck.shuffle()
        self.player_hand = []
        self.dealer_hand = []
        self.player_bet = 0
        self.player_money = 0
        self.rounds_played = 0

    def checkDeck(self):
        """Check card amount in deck."""
        if len(self.deck.cards) < 20:
            self.deck.build()
            self.deck.shuffle()

    def roundBet(self):
        """Get bet for current round."""
        while True:
            if self.player_bet > 0 and self.player_bet < self.player_money:
                return self.player_bet
            else:
                continue

    def natural21(self, hand):
        """Check for a natural 21."""
        self.hand = hand
        if ((self.hand[0].rank == 14 or self.hand[1].rank == 14)
                and (self.hand[0].rank in range(10, 14)
                     or self.hand[1].rank in range(10, 14))):
            if self.dealer_hand != 21:
                self.player_bet *= 1.5
                self.endRound(self.player_hand)

    def insurance(self, ins_bet):
        """Insure round per user input."""
        self.ins_bet = ins_bet
        if self.dealer_hand[1].rank in range(10, 14):
            self.ins_bet *= 2
            self.player_money -= self.player_bet
            self.player_money += self.ins_bet
        else:
            self.player_money -= self.ins_bet

    def splitPairs(self):
        """Split pairs."""
        self.split_hand = []
        self.split_bet = self.player_bet
        self.split_hand.append(self.player_hand.pop())
        self.split_hand.append(self.deck.drawCard())
        self.player_hand.append(self.deck.drawCard())

    def doubleDown(self):
        self.player_bet += self.player_bet

    def playerHit(self, hand):
        """Append cards to player's hand."""
        self.hand = hand
        self.hand.append(self.deck.drawCard())

    def dealerHit(self):
        while self.cardSum(self.dealer_hand) < 17:
            self.dealer_hand.append(self.deck.drawCard())

    def cardSum(self, hand):
        """Count card values in hand."""
        self.hand = hand
        self.card_total = 0
        self.ace_total = 0

        for c in self.hand:
            if c.rank == 14:
                self.card_total += 11
                self.ace_total += 1
            elif c.rank in range(10, 14):
                self.card_total += 10
            else:
                self.card_total += c.rank
        while self.card_total > 21 and self.ace_total > 0:
            self.card_total -= 10
            self.ace_total -= 1
        return self.card_total

    def startRound(self):
        """Deal cards at start of round."""
        self.checkDeck()
        self.player_hand.append(self.deck.drawCard())
        self.player_hand.append(self.deck.drawCard())
        self.dealer_hand.append(self.deck.drawCard())
        self.dealer_hand.append(self.deck.drawCard())

    def endRound(self, ehand):
        """End round."""
        self.ehand = ehand
        if (self.cardSum(self.ehand) <= 21) and (self.cardSum(self.dealer_hand)
                                                 > 21):
            self.player_money += self.player_bet
        elif (self.cardSum(self.ehand) > self.cardSum(
                self.dealer_hand)) and (self.cardSum(self.ehand) <= 21):
            self.player_money += self.player_bet
        elif self.cardSum(self.ehand) == self.cardSum(self.dealer_hand):
            pass
        else:
            self.player_money -= self.player_bet