SHARE
TWEET

Untitled

a guest Jun 17th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from random import shuffle as rsh
  2.  
  3. values = {'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'10':10,'J':10,'Q':10,'K':10,'A':11}
  4.  
  5. class Player:
  6. '''
  7. Class Player
  8. =======
  9. Input: Name, Balance
  10. Output(print): Player Info
  11. =======
  12. Methods:
  13. Win/Lose
  14. Input: Amount
  15. Output: Balance
  16.  
  17. '''
  18. def __init__(self,name,money):
  19.     self.name = name
  20.     self.balance = money
  21.     self.bet=0
  22.  
  23. def __str__(self):
  24.     return 'Player Infon---------nName: {}nBalance: DKK
  25. {}'.format(self.name,self.balance)
  26.  
  27. def win(self):
  28.     self.balance+=self.bet
  29.  
  30. def lose(self):
  31.     self.balance-=self.bet
  32.  
  33.  
  34. class Deck:
  35. '''
  36. Class Deck
  37. =======
  38. Input: nothing
  39. Output(print): deck, number of cards
  40. =======
  41. Methods:
  42. Create/Show/Shuffle
  43. Input: nothing
  44. Output: deck
  45.  
  46. '''
  47. def __init__(self,):
  48.     #start with an empty list
  49.     self.deck = []
  50.  
  51. def __str__(self):
  52.     return ''
  53.  
  54. def create(self):
  55.     for suit in ['D','C','H','S']:
  56.         for rank in [2,3,4,5,6,7,8,9,10,'J','Q','K','A']:
  57.             deck=self.deck
  58.             deck.append(suit+str(rank))
  59.  
  60. def showme(self):
  61.     return 'Cards: {} , Total: {}'.format(self.deck,len(self.deck))    
  62.  
  63. def shuffle(self):
  64.     rsh(self.deck)
  65.  
  66. def deal(self):
  67.     card = self.deck.pop()
  68.     return card
  69.  
  70.  
  71.  
  72. class Hand:
  73. '''
  74. Class Hand
  75. '''
  76.  
  77. def __init__(self,name):
  78.     self.hand = []
  79.     self.value = 0
  80.     self.aces = 0
  81.     self.name = name
  82.  
  83.  
  84. """ def showme(self):
  85.     if len(self.hand) == 0:
  86.         return '{} has the following cards: 0'.format(self.name)
  87.     else:
  88.         return '{} has the following cards:
  89. {}'.format(self.name,self.hand) """
  90.  
  91. def add_card(self,newcard):
  92.     self.hand.append(newcard)
  93.     self.value+=values[newcard[1:]]
  94.     if newcard[1:]=='A':
  95.         self.aces+=1
  96.  
  97. def adjust_for_ace(self):
  98.     while self.value >21 and self.aces:
  99.         self.value-=10
  100.         self.aces-=1
  101.  
  102.  
  103.  
  104. #function for take bet
  105. def take_bet(player):
  106. while True:
  107.     try:
  108.         chips=int(input('How many chips do you want to bet? :'))  
  109.  
  110.         if player.balance < chips:
  111.             print('Sorry you dont have the amount! Your balance is
  112. {}'.format(player.balance))
  113.         else:
  114.             player.bet = chips
  115.             break
  116.     except:
  117.         print('Only integer allowed')
  118.  
  119.  
  120.  
  121. #function hit
  122. def hit(deck,hand):
  123. hand.add_card(deck.deal())
  124. hand.adjust_for_ace()
  125.  
  126.  
  127. playing = True
  128. #function hit or stand
  129. def hit_or_stand(deck,hand):
  130. global playing
  131. while playing:
  132.     x = input("Would you like to Hit or Stand? Enter 'h' or 's' : ")
  133.     if x[0].lower() == 's':
  134.         print('nPlayer stands. Dealer is playing')
  135.         playing = False
  136.     elif x[0].lower() == 'h':
  137.         hit(deck,hand)
  138.     else:
  139.         print('Please, try again!')
  140.         continue
  141.     break
  142.  
  143.  
  144. #show functions
  145. def show_some(playerhand,dealerhand):
  146. print("Dealer's hand:")
  147. print('<hidden>')
  148. print(dealerhand.hand[1])
  149. print("nPlayer's hand:", *playerhand.hand, sep='n')
  150.  
  151. def show_all(playerhand,dealerhand):
  152. print("nDealers's hand:", *dealerhand.hand, sep='n')
  153. #print("Total:" + sum())
  154. print("nPlayer's hand:", *playerhand.hand, sep='n')
  155.  
  156. #scenarios
  157. def player_busts(player):
  158. print('{} busts!'.format(player.name))
  159. player.lose()
  160.  
  161. def player_blackjack(player):
  162. print("{} got blackjack!".format(player.name))
  163. player.win()
  164.  
  165. def dealer_blackjack(player,dealer):
  166. print("{} got blackjack!".format(dealer.name))
  167. player.lose()
  168.  
  169. def player_wins(player):
  170. print('{} wins!'.format(player.name))
  171. player.win()
  172.  
  173. def dealer_busts(player,dealer):
  174. print('{} busts'.format(dealer.name))
  175. player.win()
  176.  
  177. def dealer_wins(player,dealer):
  178. print('{} wins'.format(dealer.name))
  179. player.lose()
  180.  
  181. def tie_game():
  182. print('Dealer and Player tie!')
  183.  
  184. def insert_money():
  185. while True:
  186.     try:
  187.         ampl = int(input('Insert the amount of DKK for converting
  188. chips!n1 DKK = 1 chip : '))
  189.         if ampl > 0:
  190.             return ampl
  191.         else:
  192.             print('Only positive Integer!')
  193.  
  194.     except:
  195.         print('Please input integer number!')
  196.  
  197. #starting with 2 card on hand
  198. def starting_cards(playerhand,dealerhand):
  199. for i in range(2):
  200.     playerhand.add_card(deck.deal())
  201.     dealerhand.add_card(deck.deal())
  202.  
  203.  
  204. #Game is Starting
  205. namepl = input("What's your name: ")
  206. if namepl =='' or namepl == ' ':
  207. namepl = 'Anonymus'
  208.  
  209. #creating players
  210. player = Player(namepl,insert_money())
  211. dealer = Player('Dealer',1000000)
  212. while True:
  213. print('nWelcome to BlackJack! Get as close to 21 as you can without
  214. going over!nDealer stops when reaching 17. Aces count as 1 or 11.')
  215. #creating deck
  216. deck = Deck()
  217. deck.create()
  218. for i in range(0,3):
  219.     deck.shuffle()
  220. print('nYour name is: {}'.format(namepl))
  221.  
  222.  
  223.  
  224. # creating player's hand
  225. player_hand = Hand(namepl)
  226. dealer_hand = Hand('Dealer')
  227.  
  228.  
  229. starting_cards(player_hand,dealer_hand)
  230. take_bet(player)
  231. show_some(player_hand,dealer_hand)
  232.  
  233. while playing:
  234.     hit_or_stand(deck,player_hand)
  235.     show_some(player_hand,dealer_hand)
  236.  
  237.     if player_hand.value > 21:
  238.         show_all(player_hand,dealer_hand)
  239.         player_busts(player)
  240.         break
  241.  
  242. if player_hand.value == 21:
  243.     player_blackjack(player)
  244.  
  245. if dealer_hand.value == 21:
  246.     dealer_blackjack(player,dealer)    
  247.  
  248. if player_hand.value < 21:
  249.  
  250.     while dealer_hand.value < 17:
  251.         hit(deck,dealer_hand)
  252.  
  253.     show_all(player_hand,dealer_hand)
  254.  
  255.     if dealer_hand.value > 21:
  256.         dealer_busts(player,dealer)
  257.  
  258.     elif dealer_hand.value > player_hand.value:
  259.         dealer_wins(player,dealer)
  260.  
  261.     elif dealer_hand.value < player_hand.value:
  262.         player_wins(player)
  263.  
  264.     else:
  265.         tie_game()
  266.  
  267. print("nTotal money {} DKK!".format(player.balance))
  268.  
  269. # Ask to play again
  270. new_game = input("Would you like to play another hand? Enter 'y' or 'n'
  271. ")
  272.  
  273. if new_game[0].lower()=='y':
  274.     playing=True
  275.     continue
  276. else:
  277.     print("Thanks for playing!")
  278.     break
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top