angryatti

BlackJack Game - Python

Jan 14th, 2023 (edited)
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.17 KB | None | 0 0
  1. # H.A. First-release
  2.  
  3. import random
  4. from collections import namedtuple
  5. Deck = namedtuple('Deck','DeckAndType')
  6.  
  7. highcards = "JQKA"
  8. decks = list()
  9.  
  10.  
  11. def init1():
  12.     for j in range(0,4,1):
  13.         for i in range (2,11,1):
  14.            if j ==0:
  15.               temp = Deck(str(i)+" h")
  16.            elif j==1:
  17.               temp = Deck(str(i)+" c")
  18.            elif j==2:
  19.               temp=  Deck(str(i)+" d")
  20.            else:
  21.               temp =  Deck(str(i)+" s")    
  22.            decks.append(temp.DeckAndType)
  23.     pass
  24.  
  25. def init2():
  26.    for z in highcards:
  27.       temp= Deck(z+" h")
  28.       decks.append(temp.DeckAndType)
  29.       temp = Deck(z+" c")
  30.       decks.append(temp.DeckAndType)
  31.       temp = Deck(z+" d")
  32.       decks.append(temp.DeckAndType)
  33.       temp= Deck(z+" s")
  34.       decks.append(temp.DeckAndType)
  35.    pass
  36. selectedCards = list()
  37.  
  38. def selected():
  39.    index = random.randint(0,len(decks)-1)
  40.    selectedCards.append(str(decks[index]))
  41.    decks.pop(index)
  42.    index = random.randint(0,len(decks)-1)
  43.    selectedCards.append(str(decks[index]))
  44.    decks.pop(index)
  45.    pass
  46.  
  47. selectedPlayerCards = list()
  48.  
  49. def selectedPlayer():
  50.    index = random.randint(0,len(decks)-1)
  51.    selectedPlayerCards.append(str(decks[index]))
  52.    decks.pop(index)
  53.    index = random.randint(0,len(decks)-1)
  54.    selectedPlayerCards.append(str(decks[index]))
  55.    decks.pop(index)
  56.    pass
  57.  
  58. init1()
  59. init2()
  60.  
  61. random.shuffle(decks)
  62. #print(decks)
  63. selected()
  64. selectedPlayer()
  65. #print(decks)
  66. valueP =0
  67. valueD =0
  68.  
  69. print ('Player Has: '+selectedPlayerCards[0]+' '+selectedPlayerCards[1])
  70. print ('Dealer has: '+selectedCards[0]+' ??')
  71.  
  72. checkValueP1 =  selectedPlayerCards[0].split(" ")[0]
  73. checkValueP2 =  selectedPlayerCards[1].split(" ")[0]
  74. checkValueD1 =  selectedCards[0].split(" ")[0]
  75. checkValueD2 =  selectedCards[1].split(" ")[0]
  76. valueP =0
  77. valueD =0
  78.  
  79. def checkVal(check):
  80.    if check == 'A':
  81.       valueP=11
  82.    elif check == 'K' or check =='Q' or check == 'J':
  83.       valueP=10
  84.    else:
  85.       valueP = check
  86.    return valueP
  87.  
  88.  
  89.  
  90.  
  91. valueP=int(checkVal(checkValueP1))+int(checkVal(checkValueP2))
  92. if valueP==22:
  93.    valueP=12
  94. valueD=int(checkVal(checkValueD1))+int(checkVal(checkValueD2))
  95. if valueD==22:
  96.    valueD=12
  97.  
  98.  
  99. print('You have: ',valueP)
  100. if (valueP ==21):
  101.    print ("Player Win, player has Blackjack")
  102.    exit()
  103.  
  104.  
  105. if (valueD ==21):
  106.    print ("Dealer Win, Dealer has Blackjack")
  107.    exit()
  108.  
  109. if valueD == 21 and valueP ==21:
  110.    print("Tie, player and dealer have BlackJack")
  111.  
  112.  
  113. value=input('C(call), S(Stay)')
  114.  
  115. x = 2
  116.  
  117.  
  118. if value=="C":
  119.    while True:
  120.       index = random.randint(0,len(decks)-1)
  121.       selectedPlayerCards.append(str(decks[index]))
  122.       decks.pop(index)
  123.       if checkVal(selectedPlayerCards[x].split(" ")[0]) == 11 and valueP+11>21:
  124.          valueP = valueP+1
  125.       valueP = valueP+int(checkVal(selectedPlayerCards[x].split(" ")[0]))
  126.       if valueP == 21:
  127.          print('Player draws: '+ selectedPlayerCards[x] )
  128.          print("Player has 21.")
  129.          if len(selectedPlayerCards)<3:
  130.             print("You have got a BlackJack")
  131.          value="S"
  132.          break
  133.       if valueP >21:
  134.          print('Player draws: '+ selectedPlayerCards[x] )
  135.          print('Player has',valueP)
  136.          print('Too much! The dealer wins')
  137.          break
  138.       print('Player draws: '+ selectedPlayerCards[x] )
  139.       print('Player has',valueP)
  140.       x = x+1
  141.    
  142.       value = input('C(call),S(Stay)')
  143.       if value == "S":
  144.          break
  145.  
  146. z =2
  147. if value=="S":
  148.    print ('Dealer has',selectedCards[0],selectedCards[1])
  149.    print ('Dealer has:',valueD)
  150.    while (valueD<17):
  151.       index = random.randint(0,len(decks)-1)
  152.       selectedCards.append(str(decks[index]))
  153.       decks.pop(index)    
  154.       if checkVal(selectedCards[z].split(" ")[0]) == 11 and valueD+11>21:
  155.          valueD = valueD+1
  156.       valueD = valueD+int(checkVal(selectedCards[z].split(" ")[0]))
  157.       print('Dealer draws:', selectedCards[z])
  158.       z = z+1
  159.       print ('Dealer has:',valueD)
  160.    if valueD > valueP and valueD <=21:
  161.       print('Dealer Wins')
  162.    elif valueD == valueP:
  163.       print('Tie')
  164.    else:
  165.       print('Player Wins')
  166.  
  167.  
  168. print ('Remaining cards: '+str(len(decks)))
Add Comment
Please, Sign In to add comment