angryatti

Black Jack v2 Python

Feb 4th, 2023
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.79 KB | None | 0 0
  1. # H.A. Second-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. deckNumber=input('One or Six Deck(s)? One)1 Six)6 Default is one' )
  59.  
  60. if deckNumber=="6":
  61.    for i in range(deckNumber):
  62.       init1()
  63.       init2()
  64. else:
  65.    init1()
  66.    init2()
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. while True:
  74.  
  75.    random.shuffle(decks)
  76.    #print(decks)
  77.    selected()
  78.    selectedPlayer()
  79.    #print(decks)
  80.    valueP =0
  81.    valueD =0
  82.  
  83.    print ('Player Has: '+selectedPlayerCards[0]+' '+selectedPlayerCards[1])
  84.    print ('Dealer has: '+selectedCards[0]+' ??')
  85.  
  86.    checkValueP1 =  selectedPlayerCards[0].split(" ")[0]
  87.    checkValueP2 =  selectedPlayerCards[1].split(" ")[0]
  88.    checkValueD1 =  selectedCards[0].split(" ")[0]
  89.    checkValueD2 =  selectedCards[1].split(" ")[0]
  90.    valueP =0
  91.    valueD =0
  92.  
  93.    def checkVal(check):
  94.       if check == 'A':
  95.          valueP=11
  96.       elif check == 'K' or check =='Q' or check == 'J':
  97.          valueP=10
  98.       else:
  99.          valueP = check
  100.       return valueP
  101.  
  102.  
  103.    
  104.  
  105.    valueP=int(checkVal(checkValueP1))+int(checkVal(checkValueP2))
  106.    if valueP==22:
  107.       valueP=12
  108.    valueD=int(checkVal(checkValueD1))+int(checkVal(checkValueD2))
  109.    if valueD==22:
  110.       valueD=12
  111.  
  112.  
  113.    print('You have: ',valueP)
  114.    if (valueP ==21):
  115.       print ("Player Win, player has Blackjack")
  116.       #exit()
  117.  
  118.  
  119.    if (valueD ==21):
  120.       print ("Dealer Win, Dealer has Blackjack")
  121.       #exit()
  122.  
  123.    if valueD == 21 and valueP ==21:
  124.       print("Tie, player and dealer have BlackJack")
  125.  
  126.  
  127.    value=input('C(call), S(Stay)')
  128.  
  129.    x = 2
  130.  
  131.  
  132.    if value=="C":
  133.       while True:
  134.          index = random.randint(0,len(decks)-1)
  135.          selectedPlayerCards.append(str(decks[index]))
  136.          decks.pop(index)
  137.          if checkVal(selectedPlayerCards[x].split(" ")[0]) == 11 and valueP+11>21:
  138.             valueP = valueP+1
  139.          valueP = valueP+int(checkVal(selectedPlayerCards[x].split(" ")[0]))
  140.          if valueP == 21:
  141.             print('Player draws: '+ selectedPlayerCards[x] )
  142.             print("Player has 21.")
  143.             if len(selectedPlayerCards)<3:
  144.                print("You have got a BlackJack")
  145.             value="S"
  146.             break
  147.          if valueP >21:
  148.             print('Player draws: '+ selectedPlayerCards[x] )
  149.             print('Player has',valueP)
  150.             print('Too much! The dealer wins')
  151.             break
  152.          print('Player draws: '+ selectedPlayerCards[x] )
  153.          print('Player has',valueP)
  154.          x = x+1
  155.      
  156.          value = input('C(call),S(Stay)')
  157.          if value == "S":
  158.             break
  159.  
  160.    z =2
  161.    if value=="S":
  162.       print ('Dealer has',selectedCards[0],selectedCards[1])
  163.       print ('Dealer has:',valueD)
  164.       while (valueD<17):
  165.          index = random.randint(0,len(decks)-1)
  166.          selectedCards.append(str(decks[index]))
  167.          decks.pop(index)    
  168.          if checkVal(selectedCards[z].split(" ")[0]) == 11 and valueD+11>21:
  169.             valueD = valueD+1
  170.          valueD = valueD+int(checkVal(selectedCards[z].split(" ")[0]))
  171.          print('Dealer draws:', selectedCards[z])
  172.          z = z+1
  173.          print ('Dealer has:',valueD)
  174.       if valueD > valueP and valueD <=21:
  175.          print('Dealer Wins')
  176.       elif valueD == valueP:
  177.          print('Tie')
  178.       else:
  179.          print('Player Wins')
  180.  
  181.  
  182.       print ('Remaining cards: '+str(len(decks)))
  183.  
  184.       selectedCards.clear()
  185.       selectedPlayerCards.clear()
  186.       readExit=input('Press Q to Quit or any key to continue' )
  187.       if readExit =="Q":
  188.          break
Add Comment
Please, Sign In to add comment