Advertisement
Guest User

blackjack game

a guest
Feb 19th, 2019
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.33 KB | None | 0 0
  1. # Blackjack
  2. import random
  3. from card_deck import Deck
  4. from time import sleep
  5. from sys import exit
  6.  
  7. d = Deck()
  8. deck_of_cards = d.deck_return()
  9. print('*' * 30)
  10. print('*  Welcome to BlackJack!  *')
  11. print('*    Made by: p0seidon    *')
  12. print('*' * 30)
  13.  
  14.  
  15. def shuffleDeck(deck):
  16.     random.shuffle(deck)
  17.     random.shuffle(deck)
  18.     random.shuffle(deck)
  19.     return deck
  20.  
  21.  
  22. def dealCard(shuffled_deck):
  23.     return random.sample(shuffled_deck, k=2)
  24.  
  25.  
  26. def removeFromDeck(hand1, hand2):
  27.     global deck_of_cards
  28.     for x in hand1:
  29.         deck_of_cards.remove(x)
  30.  
  31.     for x in hand2:
  32.         deck_of_cards.remove(x)
  33.  
  34.  
  35. def hit(deck_to_hit):
  36.     return random.choice(deck_to_hit)
  37.  
  38.  
  39. def endGame():
  40.     print('Thanks for playing!\n'
  41.           'Better luck next time!')
  42.     sleep(3)
  43.     exit()
  44.  
  45.  
  46. def restartGame():
  47.     restart = ''
  48.     restart_list = ['y', 'n']
  49.     while restart not in restart_list:
  50.         restart = input('Would you like to play again? [y/n]: ')
  51.  
  52.     return restart
  53.  
  54.  
  55. def resetGame():
  56.     global deck_of_cards
  57.     print('Loading new deck.\n'
  58.           'Please wait...')
  59.     sleep(3)
  60.     deck_of_cards = d.deck_return()
  61.     return deck_of_cards
  62.  
  63.  
  64. play_again = 'y'
  65. while play_again == 'y':
  66.     # shuffles and deals 2 cards to dealer and player
  67.     ready_deck = shuffleDeck(deck_of_cards)
  68.     dealer_hand = dealCard(ready_deck)
  69.     player_hand = dealCard(ready_deck)
  70.  
  71.     # removes the cards in players hand from the deck, making the game a little more life-like
  72.     removeFromDeck(dealer_hand, player_hand)
  73.  
  74.     # checks to see if dealer won in the first hand
  75.     if sum(dealer_hand) == 21:
  76.         print('Dealer\'s first hand is', dealer_hand, 'totaling 21.\n'
  77.                                                       'You lose!')
  78.         play_again = restartGame()
  79.         if play_again == 'y':
  80.             deck_of_cards = resetGame()
  81.             continue
  82.         elif play_again == 'n':
  83.             endGame()
  84.  
  85.     # check to see if player won or busted in the first hand
  86.     if sum(player_hand) == 21:
  87.         print('You win with the initial hand of', player_hand, '\n'
  88.                                                                'CONGRATULATIONS!')
  89.         play_again = restartGame()
  90.         if play_again == 'y':
  91.             deck_of_cards = resetGame()
  92.             continue
  93.         elif play_again == 'n':
  94.             endGame()
  95.     elif sum(player_hand) > 21:
  96.         print('Looks like you busted in your first hand consisting of', player_hand, '\n'
  97.                                                                                      'Better luck next time!')
  98.         play_again = restartGame()
  99.         if play_again == 'y':
  100.             deck_of_cards = resetGame()
  101.             continue
  102.         elif play_again == 'n':
  103.             endGame()
  104.  
  105.     # shows only one of the dealers card at random and shows the player his hand
  106.     print('DEBUG: dealers hand:', dealer_hand)
  107.     # print('Dealer has', dealer_hand[random.randint(0, 1)], 'the other card is hidden')
  108.     print('You have', player_hand)
  109.  
  110.     # sums the cards in players hand and dealers hand
  111.     player_sum = sum(player_hand)
  112.     dealer_sum = sum(dealer_hand)
  113.  
  114.     # player logic
  115.     while player_sum < 21:
  116.         choice = input('Would you like to hit or stay (h/s)?: ')
  117.         if choice == 'h':
  118.             new_player_card = hit(ready_deck)
  119.             player_hand.append(new_player_card)
  120.             print('Current hand:', player_hand)
  121.             player_sum += new_player_card
  122.             if player_sum == 21:  # player wins on hit
  123.                 print('You win!\n'
  124.                       'Winning hand:', player_hand)
  125.                 play_again = restartGame()
  126.                 if play_again == 'y':
  127.                     deck_of_cards = resetGame()
  128.                 elif play_again == 'n':
  129.                     endGame()
  130.             elif player_sum > 21:  # player busts on hit
  131.                 print('looks like you went overboard. Your final hand was:', player_hand)
  132.                 play_again = restartGame()
  133.                 if play_again == 'y':
  134.                     deck_of_cards = resetGame()
  135.                     continue
  136.                 elif play_again == 'n':
  137.                     endGame()
  138.         elif choice == 's':
  139.             print('You locked in at', player_sum)
  140.             sleep(2)
  141.             break
  142.  
  143.     # dealer logic
  144.     while dealer_sum < 17:
  145.         print('Dealers hand:', dealer_hand)
  146.         print('Dealer hits..')
  147.         sleep(1)
  148.         new_dealer_card = hit(ready_deck)
  149.         dealer_hand.append(new_dealer_card)
  150.         sleep(0.5)
  151.         dealer_sum += new_dealer_card
  152.         if dealer_sum > 21:  # dealer busts on hit
  153.             print('Dealer has busted!\n'
  154.                   'You Win!')
  155.             print('Dealers losing hand:', dealer_hand)
  156.             play_again = restartGame()
  157.             if play_again == 'y':
  158.                 deck_of_cards = resetGame()
  159.                 continue
  160.             elif play_again == 'n':
  161.                 endGame()
  162.         elif 17 < dealer_sum <= 21:  # dealer stays
  163.             print('Dealers hand:', dealer_hand)
  164.             print('Dealer stays..')
  165.             sleep(1)
  166.             break
  167.  
  168.     # compares player sum with dealer sum to determine who won
  169.     if player_sum > dealer_sum:
  170.         print('You win!\n'
  171.               'Your winning hand:', player_hand, '\n'
  172.                                                  'Dealers losing hand:', dealer_hand)
  173.         play_again = restartGame()
  174.         if play_again == 'y':
  175.             deck_of_cards = resetGame()
  176.             continue
  177.         elif play_again == 'n':
  178.             endGame()
  179.     elif dealer_sum > player_sum:
  180.         print('Looks like you were unlucky this time...Dealer won..\n'
  181.               'Dealer winning hand:', dealer_hand)
  182.         play_again = restartGame()
  183.         if play_again == 'y':
  184.             deck_of_cards = resetGame()
  185.             continue
  186.         elif play_again == 'n':
  187.             endGame()
  188.     elif player_hand == dealer_hand:
  189.         print('Its a tie!')
  190.         sleep(0.5)
  191.         print('Your hand:', player_hand)
  192.         sleep(0.5)
  193.         print('Dealers hand:', dealer_hand)
  194.         sleep(0.5)
  195.         play_again = resetGame()
  196.         if play_again == 'y':
  197.             deck_of_cards = resetGame()
  198.             continue
  199.         elif play_again == 'n':
  200.             endGame()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement