def main(): card = namedtuple("card", "rank suit") deck = blackjack.Deck() deck.build_deck(card) deck.shuffle() dealer = blackjack.Hand("dealer", 80, 200) player = blackjack.Hand("player", 80, 500) width = 1500 height = 1500 pygame.init() screen = pygame.display.set_mode((width, height)) hand_tuple = namedtuple("hand_tuple", "player dealer") hands = hand_tuple(player=player, dealer=dealer) pygame_tuple = namedtuple("pygame_tuple", "pygame screen") PYGAME = pygame_tuple(pygame=pygame, screen=screen) width_height = namedtuple("width_height", "width height") DISPLAY = width_height(width=width, height=height) colours = namedtuple("colours", "black green red yellow background") COLOURS = colours(black=(0, 0, 0), green=(0, 255, 0), red=(255, 0, 0), yellow=(255, 255, 0), background=(152, 198, 195)) button = namedtuple("button", " x y width height colour text text_colour") hit = button(x=80, y=700, width=100, height=80, colour=COLOURS.green, text="hit", text_colour=COLOURS.black) stand = button(x=200, y=700, width=100, height=80, colour=COLOURS.yellow, text="stand", text_colour=COLOURS.black) buttons = namedtuple("button", "hit stand") BUTTONS = buttons(hit=hit, stand=stand) game_loop(PYGAME, COLOURS, BUTTONS, DISPLAY, card, deck, hands)
def test_check_dealer_for_blackjack(self): """ Test that check_dealer_for_blackjack() function accurately recognizes when the dealer has been dealt a blackjack. """ # create dealer & hand dealer = bj.Dealer() hand = bj.Hand() dealer.hands.append(hand) # 10, ace check card1 = bj.Card('Hearts', '*', 10) card2 = bj.Card('Hearts', '*', 1) hand.cards.append(card1) hand.cards.append(card2) self.assertTrue(bj.check_dealer_for_blackjack([dealer])) # ace, 10 check hand.first_iter = True hand.blackjack = False dealer.hands[0].cards[0].value = 1 dealer.hands[0].cards[1].value = 10 self.assertTrue(bj.check_dealer_for_blackjack([dealer])) # ace, 5 check hand.first_iter = True hand.blackjack = False dealer.hands[0].cards[1].value = 5 self.assertFalse(bj.check_dealer_for_blackjack([dealer]))
def test_hand_should_contains_no_card_after_clear(self): hand = blackjack.Hand() card = blackjack.Card('club', 'an ace', 11) hand.add(card) card = blackjack.Card('spades', 'jack', 10) hand.add(card) expect(len(hand.cards)).equal(2) hand.clear() expect(len(hand.cards)).equal(0)
def setUp(self): self.hand = blackjack.Hand(blackjack.Deck()) self.dealer_hand = blackjack.DealerHand(blackjack.Deck()) self.example_hard_cards = [blackjack.Card( 13, 'Diamonds'), blackjack.Card(4, 'Clubs')] self.example_soft_cards = [blackjack.Card(1, 'Hearts'), blackjack.Card(6, 'Diamonds')] self.example_blackjack_cards = [blackjack.Card(1, 'Spades'), blackjack.Card(13, 'Hearts')]
def test_functions(self): """Check all the function""" chip = self.test_chips() chip.bet = 200 self.assertTrue(chip.bet > chip.total) decks = self.test_deck() player_hand = blackjack.Hand() player_hand.add_card(decks.deal()) player_hand.add_card(decks.deal()) dealer_hand = blackjack.Hand() dealer_hand.add_card(decks.deal()) dealer_hand.add_card(decks.deal()) blackjack.show_some(player_hand, dealer_hand) self.assertTrue(len(player_hand.cards) == 2) self.assertTrue(len(dealer_hand.cards) == 2) self.assertTrue(player_hand.value > 0) self.assertTrue(dealer_hand.value > 0)
def test_hand(self): """Check the hand class""" hand = blackjack.Hand() self.assertEqual(hand.cards, []) self.assertEqual(hand.value, 0) self.assertEqual(hand.aces, 0) deck = self.test_deck() pull_card = deck.deal() hand.add_card(pull_card) self.assertNotEqual(hand.cards, [])
def test_hand_should_have_added_card(self): hand = blackjack.Hand() expect(len(hand.cards)).equal(0) card = blackjack.Card('club', 'an ace', 11) hand.add(card) expect(len(hand.cards)).equal(1) expect(hand.cards[-1].figure=='an ace' and hand.cards[-1].color=='club').to.be(True) card = blackjack.Card('spades', 'jack', 10) hand.add(card) expect(len(hand.cards)).equal(2) expect(hand.cards[-1].figure=='jack' and hand.cards[-1].color=='spades').to.be(True)
def test_hand_should_return_correct_number_of_ace(self): hand = blackjack.Hand() expect(hand.number_of_ace()).equal(0) card = blackjack.Card('club', 'an ace', 11) hand.add(card) expect(hand.number_of_ace()).equal(1) card = blackjack.Card('spades', 'jack', 10) hand.add(card) expect(hand.number_of_ace()).equal(1) card = blackjack.Card('spades', 'an ace', 11) hand.add(card) expect(hand.number_of_ace()).equal(2)
def test_hand_should_evalute_correctly(self): hand = blackjack.Hand() card = blackjack.Card('club', 'an ace', 1) hand.add(card) expect(hand.evaluate()).equal(11) card = blackjack.Card('club', '4', 4) hand.add(card) expect(hand.evaluate()).equal(15) card = blackjack.Card('club', 'jack', 10) hand.add(card) expect(hand.evaluate()).equal(15) card = blackjack.Card('spades', 'an as', 1) hand.add(card) expect(hand.evaluate()).equal(16)
def test_ace(self): # Creating card objects for ace check ace = blackjack.Card("♣", 'A') king = blackjack.Card("♣", 'K') queen = blackjack.Card("♣", 'Q') # Creating the player's hand playerHand = [ace, king, queen] hand = blackjack.Hand([]) hand.setNumCards(3) # Hand value should be 21, this shows ace's # will switch from 11 to 1 val = hand.handValue(playerHand) actual = val expected = 21 # Check if hand is equal to 21 self.assertEqual(expected, actual, "Failed ace test")
def test_two_ace(self): # Creating card objects for hand with 2 aces aceOne = blackjack.Card("♣", 'A') aceTwo = blackjack.Card("♣", 'A') queen = blackjack.Card("♣", 'Q') nine = blackjack.Card("♣", '9') # Creating the player's hand playerHand = [aceOne, aceTwo, queen, nine] hand = blackjack.Hand([]) hand.setNumCards(4) # Hand value should be 21, this shows ace's # will switch from 11 to 1 even while 2 are in hand val = hand.handValue(playerHand) actual = val expected = 21 # Check if hand is equal to 21 self.assertEqual(expected, actual, "Failed two ace test")
def setUp(self): self.deck = blackjack.Deck() self.hand = blackjack.Hand(self.deck) self.card = blackjack.Card(12, 'Any suit')
async def play_game(channel, playing, new_game='n'): while True: # Print an opening statement await channel.send(p.messages['blackjack'][0]) # Create & shuffle the deck, deal two cards to each player deck = bj.Deck() print(deck) deck.shuffle() player_hand = bj.Hand() player_hand.add_card(deck.deal()) player_hand.add_card(deck.deal()) dealer_hand = bj.Hand() dealer_hand.add_card(deck.deal()) dealer_hand.add_card(deck.deal()) # Set up the Player's chips # if new_game != 'y': if new_game != 's': player_chips = bj.Chips() # Prompt the Player for their bet await take_bet(player_chips, channel) # function not working # Show cards (but keep one dealer card hidden) await show_some(player_hand, dealer_hand, channel) while playing: # recall this variable from our hit_or_stand function # Prompt for Player to Hit or Stand playing = await hit_or_stand(deck, player_hand, channel) # Show cards (but keep one dealer card hidden) await show_some(player_hand, dealer_hand, channel) # If player's hand exceeds 21, run player_busts() and break out of loop if player_hand.value > 21: await player_busts(player_hand, dealer_hand, player_chips, channel) break # If Player hasn't busted, play Dealer's hand until Dealer reaches 17 if player_hand.value <= 21: while dealer_hand.value < 17: hit(deck, dealer_hand) # Show all cards await show_all(player_hand, dealer_hand, channel) # Run different winning scenarios if dealer_hand.value > 21: await dealer_busts(player_hand, dealer_hand, player_chips, channel) elif dealer_hand.value > player_hand.value: await dealer_wins(player_hand, dealer_hand, player_chips, channel) elif dealer_hand.value < player_hand.value: await player_wins(player_hand, dealer_hand, player_chips, channel) else: await push(player_hand, dealer_hand, player_chips, channel) # Inform Player of their chips total await channel.send(p.messages['blackjack'][1] + str(player_chips.total)) print(p.messages['blackjack'][1], player_chips.total) # Ask to play again # new_game = input("would you like to play again? Enter 'y' or 'n'") info = await ask_new_game(channel, p.messages['blackjack'][15]) playing = info[0] new_game = info[1]