Пример #1
0
  def play(self, hand_size=3):
    """Starts a card game between two players.

    Args:
      hand_size: An int representing the number of cards a player will draw.
    """
    self.playing = True

    while self.playing:
      cards = Deck()
      cards.shuffle_deck()

      p1 = Player(input("\nPLAYER 1 NAME:  "))
      p2 = Player(input("\nPLAYER 2 NAME:  "))

      for _ in range(hand_size):
        p1.draw_card(cards.deck)
        p2.draw_card(cards.deck)

      p1.sort_hand(cards.suits, cards.suit_cards)
      p2.sort_hand(cards.suits, cards.suit_cards)

      p1.score_hand(cards.suits, cards.suit_cards)
      p2.score_hand(cards.suits, cards.suit_cards)

      self.show_hands(p1, p2)
      self.declare_winner(p1, p2)

      play_again = input("\nPlay again? [y/N]  ")

      if not strtobool(play_again):
        self.playing = False
Пример #2
0
 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
Пример #3
0
    def __init__(self, purse):

        #store the new deck
        self.game_deck = Deck()

        #initialize player with a purse
        self.Player = Player(purse)

        #initialize computer player with a purse
        self.Computer = Player(purse)
Пример #4
0
 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
Пример #5
0
    def __init__(self):

        self.__deck = Deck()
        self.__a_Team = []
        self.__b_Team = []

        # players 1-3 on team A
        # players 4-6 on team B
        self.__current_player_id = 1
        self.__score = [0, 0]

        # create players
        for i in range(1, 4):
            self.__a_Team.append(Player(i, i))
            self.__b_Team.append(Player(i, i + 3))

        # deal cards to players
        i = 0
        for card in self.__deck.get_cards():
            if i < 3:
                self.__a_Team[i].receive_card(card)
            else:
                self.__b_Team[i - 3].receive_card(card)
            i = (i + 1) % 6
Пример #6
0
class blackjack_game:
    def __init__(self, purse):

        #store the new deck
        self.game_deck = Deck()

        #initialize player with a purse
        self.Player = Player(purse)

        #initialize computer player with a purse
        self.Computer = Player(purse)

    def play_game(self, user_ante):
        """
        function used to implement game play.
        """

        #flag indicating game has started
        user_play = 1

        while ((self.Player.get_purse() > 0) and
               (self.Computer.get_purse() > 0)) and (user_play != 0):

            #create deck
            self.game_deck.create_deck()

            #shuffle deck
            self.game_deck.shuffle_deck()

            #add bet before game start
            self.Player.add_bet(user_ante)
            self.Computer.add_bet(user_ante)

            #deal first 2 card to player and computer
            self.deal_hand_initial(self.Player)
            self.deal_hand_initial(self.Computer)

            print("Player: ")
            self.Player.player_show_hand()
            print("CPU: ")
            self.Computer.player_show_hand()

            #print current purse and ante
            self.Player.player_stats()
            self.Computer.player_stats()

            #determine if we have a black jack winner
            if self.blackjack_payout(self.Player, self.Computer) == True:
                continue

            user_raise = int(input("Player, how much do you want to raise?"))

            #add ante to the current bet
            self.Player.add_bet(user_raise)
            self.Computer.add_bet(user_raise)

            user_choice = int(input("Player, hit (1) or stay (0)?"))

            while user_choice == 1:

                self.Player.draw_card(self.game_deck.deal_card())
                print("Player: ")
                self.Player.player_show_hand()

                user_choice = int(input("Player, hit (1) or stay (0)?"))

            #cpu keeps drawing cards until hand score > 16
            cpu_hand_score = self.calc_hand_score(self.Computer.get_hand())

            while cpu_hand_score < 16:

                #draw card
                self.Computer.draw_card(self.game_deck.deal_card())
                print("CPU: ")
                self.Computer.player_show_hand()

                #update hand score
                cpu_hand_score = self.calc_hand_score(self.Computer.get_hand())

            #calculate total value of hand
            player_hand_score = self.calc_hand_score(self.Player.get_hand())
            self.Player.set_hand_score(player_hand_score)
            self.Computer.set_hand_score(cpu_hand_score)

            #identify the winner of the round
            self.calc_winner_round()

            user_play = int(
                input("Do you want to continue? 1 = Continue, 0 = Quit"))

    def blackjack_payout(self, player, computer):
        """Calculates payout for player if the first 2 cards is 21."""
        if self.determine_black_jack(player) == True:

            if self.determine_black_jack(computer) == False:

                #add the bet to the game
                computer.add_bet(computer.get_current_bet() * 1.5)

                #add computers bet to player
                player.add_to_purse(computer.get_current_bet())

                player.reset_bet()
                computer.reset_bet()

                player.reset_hand()
                computer.reset_hand()

            return True

        else:
            if self.determine_black_jack(computer) == True:

                #add the bet to the game
                player.add_bet(computer.get_current_bet() * 1.5)

                #add players bet to computer
                computer.add_to_purse(player.get_current_bet())

                player.reset_bet()
                computer.reset_bet()

                player.reset_hand()
                computer.reset_hand()

                return True

    def determine_black_jack(self, player):
        """determines if player or computer gets 21 with first 2 cards."""
        return self.calc_hand_score(player.get_hand()) == 21

    def deal_hand_initial(self, Player):
        """Deal the first 2 cards to the player."""
        Player.set_hand(self.game_deck.deal_card())
        Player.set_hand(self.game_deck.deal_card())

    def calc_winner_round(self):
        """
        Determine the winner for each round.
        """
        player_score = self.Player.get_hand_score()
        cpu_score = self.Computer.get_hand_score()

        print("Player Hand: ", player_score)
        print("CPU hand: ", cpu_score)

        if player_score > 21:

            print("Player 1 Loss: ", self.Player.get_current_bet())

            #add to Players purse
            self.Computer.add_to_purse(self.Player.get_current_bet())

        elif cpu_score > 21:

            print("Computer Loss: ", self.Player.get_current_bet())

            #add to Players purse
            self.Player.add_to_purse(self.Computer.get_current_bet())

        #if player and cpu does not bankrupt
        else:

            if player_score > cpu_score:
                print("Computer Loss: ", player_score, cpu_score,
                      self.Computer.get_current_bet())

                #add to Players purse
                self.Player.add_to_purse(self.Computer.get_current_bet())

            else:
                print("Player 1 Loss: ", player_score, cpu_score,
                      self.Player.get_current_bet())

                #add to Players purse
                self.Computer.add_to_purse(self.Player.get_current_bet())

        #reset bet for new round
        self.Player.reset_bet()
        self.Computer.reset_bet()

        #reset hand for new round
        self.Player.reset_hand()
        self.Computer.reset_hand()

    def calc_hand_score(self, list_of_cards):

        hand_score = 0

        ace = " "

        for card in list_of_cards:

            #get value of card
            current_card = card.get_card()[3]

            #check if adding an ace makes the player bust
            if current_card == 'A':

                if hand_score + 11 < 22:

                    hand_score += 11

                    #skip everything else in the loop
                    continue

            #this case looks at all face cards including A, A is treated as 1
            if type(current_card) == str:
                #convert K,Q,J,A
                hand_score += self.facecard_to_int(current_card)
            else:
                hand_score += current_card

        return hand_score

    def facecard_to_int(self, current_card):

        face_cards = {"K": 10, "Q": 10, "J": 10, "A": 1}
        try:

            #if K,Q,J or A
            return face_cards.get(current_card)

        except:

            #if numeric card
            return current_card
Пример #7
0
 def setUp(self):
     self.sample_deck = Deck()
Пример #8
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)
Пример #9
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
Пример #10
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
Пример #11
0
 def __init__(self, channel):
     self._channel = channel
     self.deck = Deck()
     self.dealt_this_phase = False
Пример #12
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.")
Пример #13
0
def cards_for_tests():
    cards = Deck()
    return cards
Пример #14
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()
Пример #15
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()
Пример #16
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()
Пример #17
0
 def setUp(self):
     self.deck = Deck()
Пример #18
0
def run():
    print(
        "Welcome to Blackjack! Get as close to 21 as you can without going bust! Dealer hits until they reach 17 or over. Aces count as 1 or 11 depending on your current hand value.\n"
    )
    player_chips = Chips(
    )  # Initialise player's chip pool. It is outside of the main loop so it carries between hands
    while True:
        playing = True  # used for the player's turn
        card_deck = Deck()
        card_deck.shuffle_deck()  # setting up the deck

        betting = input("Player, would you like to bet on this round? (y/n) "
                        )  # asks if the player wants to bet
        if betting == "y":
            set_up_bet(player_chips)

        # intialising the player's and dealer's hands
        player = Hand()
        dealer = Hand()

        deal_cards(player, dealer, card_deck)

        # player's turn; the loop will continue until the player stands or goes bust
        while playing:
            show_some_cards(player, dealer)

            # runs if the player is bust
            if player.total_card_value > 21:
                player_busts(player_chips)
                break

            playing = hit_or_stand(
                player, card_deck)  # asks if the player wants to hit or stand

        if player.total_card_value <= 21:

            # the dealer hits until their hand value is greater than 17
            while dealer.total_card_value < 17:
                hit(dealer, card_deck)

            show_cards(player, dealer)  # shows players and dealers cards

            # checks the outcome of the game and calls the respective method (which sorts the bet outcome if there was one)
            if dealer.total_card_value > 21:
                dealer_busts(player_chips)
            elif dealer.total_card_value > player.total_card_value:
                dealer_wins(player_chips)
            elif dealer.total_card_value < player.total_card_value:
                player_wins(player_chips)
            else:
                print("Dealer and Player tie! It's a push.")

        # checks if the player wants to play another hand
        play_again = input("\nWould you like to play another hand? y/n: ")
        if play_again.lower() == "y":
            playing = True
            print("\n" * 100)
        else:
            print("Thanks for playing! You have left the table with {} chips.".
                  format(player_chips.chips_pool))
            break
Пример #19
0
 def __init__(self, num_rounds_per_episode=10):
     self.deck = Deck()
     self.reset()
     self.rounds_per_episode = num_rounds_per_episode
Пример #20
0
class Game:
    def __init__(self):

        self.__deck = Deck()
        self.__a_Team = []
        self.__b_Team = []

        # players 1-3 on team A
        # players 4-6 on team B
        self.__current_player_id = 1
        self.__score = [0, 0]

        # create players
        for i in range(1, 4):
            self.__a_Team.append(Player(i, i))
            self.__b_Team.append(Player(i, i + 3))

        # deal cards to players
        i = 0
        for card in self.__deck.get_cards():
            if i < 3:
                self.__a_Team[i].receive_card(card)
            else:
                self.__b_Team[i - 3].receive_card(card)
            i = (i + 1) % 6

    # ----------------------------------------------------------------

    # Here we return the requested player object, defaulting to the
    #   current player if none specified
    def get_player(self, player_id=None):

        if player_id is None or player_id not in range(1, 7):
            player_id = self.__current_player_id

        if player_id < 4:
            return self.__a_Team[player_id - 1]
        else:
            return self.__b_Team[player_id - 4]

    # ----------------------------------------------------------------

    # return the id of the current player
    def get_current_player_id(self):
        return self.__current_player_id

    # ----------------------------------------------------------------

    # set a new current player
    def set_current_player(self, player_id):
        if player_id > 0 and player_id < 7:
            self.__current_player_id = player_id

    # ----------------------------------------------------------------

    # This facilitates the current player asking for a card.
    # If the askee has the card, they lose said card
    # and the current player gets another turn.
    # Else, the current player loses there turn and
    # the askee is the new current player.
    def player_inquiry(self, askee_id, suit, number):

        card = Card(suit, number)
        asker = self.get_player()
        askee = self.get_player(askee_id)
        message = ""
        success = True

        if askee.has_card(card):
            askee.lose_card(card)
            asker.receive_card(card)

            # if the player has a completed suit, said player declares
            if asker.can_declare():
                self.__current_player_declares()

            message = 'You recieved the %s' % (card.get_verbose_name())

        else:
            self.set_current_player(askee_id)
            message = 'This player does not have the %s. You lose your turn.' % (
                card.get_verbose_name())

        # return message indicates success or failure
        return json.dumps({'success': success, 'message': message})

    # ----------------------------------------------------------------

    # the current player declares a suit
    def __current_player_declares(self):

        player = self.get_player()
        declaration = player.declare()
        self.__score[0 if self.__current_player_id < 4 else 1] += (
            declaration[1] + 1)

    # ----------------------------------------------------------------

    # return the status of the game as json
    def get_json(self):

        game_over = (self.__score[0] + self.__score[1]) == 12
        current_team = 0 if self.__current_player_id < 4 else 1

        return json.dumps({
            'current_player': self.get_player().get_info(),
            'current_team': current_team,
            'score': self.__score,
            'game_over': game_over
        })


# --------------------------------------------------------------------
# ---------------- end class Game ------------------------------------
# --------------------------------------------------------------------