Advertisement
Guest User

Untitled

a guest
Mar 6th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.48 KB | None | 0 0
  1. from random import shuffle
  2. import os
  3.  
  4.  
  5. def shuffled_shoe():
  6.     shoe = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'A', 'J', 'Q', 'K']*4
  7.     shuffle(shoe)
  8.     return shoe
  9.  
  10.  
  11. def deal_card(shoe, person, number):
  12.     for _ in range(number):
  13.         person.append(shoe.pop())
  14.  
  15.  
  16. def deal_hand(shoe, player, dealer):
  17.     deal_card(shoe, player, 2)
  18.     deal_card(shoe, dealer, 2)
  19.  
  20.  
  21. def score(person):
  22.     non_aces = [c for c in person if c != 'A']
  23.     aces = [c for c in person if c == 'A']
  24.     total = 0
  25.     for card in non_aces:
  26.         if card in 'JQK':
  27.             total += 10
  28.         else:
  29.             total += int(card)
  30.     for card in aces:
  31.         if total <= 10:
  32.             total += 11
  33.         else:
  34.             total += 1
  35.     return total
  36.  
  37.  
  38. def display_info(player, dealer, player_stands):
  39.     os.system('cls' if os.name == 'nt' else 'clear')
  40.     print("Your cards [{}] ({})".format("][".join(player), score(player)))
  41.     if player_stands:
  42.         print("Dealer cards: [{}] ({})".format("][".join(dealer), score(dealer)))
  43.     else:
  44.         print(f"Dealer cards: [{dealer[0]}] [?]")
  45.  
  46.  
  47. def hit_or_stand():
  48.     while True:
  49.         print("What do you choose?")
  50.         print("[1] Hit")
  51.         print("[2] Stand")
  52.         ans = input("> ")
  53.         if ans in '12':
  54.             return ans
  55.  
  56.  
  57. # ------------BUG-ZONE-------------
  58.  
  59.  
  60. def player_play(shoe, player, dealer, player_stands, player_plays, dealer_plays):
  61.     while not player_stands:
  62.         if hit_or_stand() == '2':
  63.             player_plays = False
  64.             player_stands = True
  65.             dealer_plays = True
  66.             display_info(player, dealer, player_stands)
  67.         elif not player_stands:
  68.             deal_card(shoe, player, 1)
  69.             display_info(player, dealer, player_stands)
  70.             still_playing = (False if score(player) > 21 else True)
  71.             results(player, dealer, player_stands, still_playing)
  72.     return (player_stands, player_plays, dealer_plays)
  73.  
  74.  
  75. def dealer_play(shoe, dealer):
  76.     while score(dealer) <= 16:
  77.         deal_card(shoe, dealer, 1)
  78.     return False
  79.  
  80.  
  81. def results(player, dealer, player_stands, still_playing):
  82.     if score(player) == 21:
  83.         print("Blackjack! You won")
  84.         still_playing = False
  85.     elif score(player) > 21:
  86.         print("Busted! You lost!")
  87.         still_playing = False
  88.     elif player_stands:
  89.         if score(dealer) > 21:
  90.             print("Dealer busted! You won")
  91.         elif score(player) > score(dealer):
  92.             print("You beat the dealer! You won!")
  93.         elif score(player) < score(dealer):
  94.             print("Dealer has beaten you. You lost!")
  95.         else:
  96.             print("Push. Nobody wins or losses.")
  97.         still_playing = False
  98.     return still_playing
  99.  
  100.  
  101. def main():
  102.     shoe = shuffled_shoe()
  103.     player = []
  104.     dealer = []
  105.     player_plays = True
  106.     still_playing = True
  107.     player_stands = False
  108.     dealer_plays = False
  109.     deal_hand(shoe, player, dealer)
  110.     while still_playing:
  111.         display_info(player, dealer, player_stands)
  112.         still_playing = results(player, dealer, player_stands, still_playing)
  113.         while player_plays:
  114.             (player_stands, player_plays, dealer_plays) = player_play(shoe, player, dealer, player_stands, player_plays, dealer_plays)
  115.         while dealer_plays:
  116.             dealer_plays = dealer_play(shoe, dealer)
  117.         still_playing = results(player, dealer, player_stands, still_playing)
  118.  
  119.  
  120. if __name__ == '__main__':
  121.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement