Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- from time import sleep as s
- pointref = {'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'J':10,'Q':10,'K':10}
- def draw(deck):
- card = deck.pop(deck.index(random.choice(deck)))
- return (card,deck)
- def getPointTotal(hand):
- points = 0
- checkAces = 0
- for i in hand:
- if i[0:2] == '10':
- points += 10
- elif i[0] == 'A':
- checkAces += 1
- points += 11
- else:
- points += pointref.get(i[0])
- while points > 21 and checkAces > 0:
- points -= 10
- checkAces -= 1
- return points
- def check(inp):
- while True:
- if inp == 'y' or inp == 'n':
- return inp
- else:
- inp = input('Please try again. (y/n) ')
- def pull(deck,myhand,p = 'n'):
- card, deck = draw(deck)
- myhand.append(card)
- if p != 'n':
- print(p, 'drew', card + '.')
- return myhand
- def postDraw(myhand,opphand):
- mypoints = getPointTotal(myhand)
- opoints = getPointTotal(opphand[1:len(opphand)])
- if mypoints <= 21:
- print('Your hand is:', myhand, 'and is worth', mypoints, 'points.')
- s(0.5)
- print('The dealer has', opphand[1:len(opphand)], 'and one face down card. Their face up cards are worth', opoints, 'points.')
- s(0.5)
- if mypoints == 21:
- print('You have a blackjack! No need to play on.')
- return (21,'n')
- elif mypoints > 21:
- print('You bust! You score 0 points.')
- return (0,'n')
- elif mypoints < 21:
- hit = input('Would you like to draw another card? (y/n) ')
- hit = check(hit)
- if hit == 'y':
- print('You take another card.')
- elif hit == 'n':
- print('You pass with', mypoints, 'points.')
- return (mypoints,hit)
- def dealer(myhand,opphand,deck,hit):
- mypoints = getPointTotal(myhand)
- opoints = getPointTotal(opphand[1:len(opphand)])
- if mypoints == 21:
- print('The dealer passes.')
- return (21,False)
- elif mypoints > 21:
- print('The dealer busts.')
- return(0,False)
- elif mypoints <= opoints + 2:
- print('The dealer takes a card.')
- return (mypoints,True)
- elif mypoints <= 14:
- print('The dealer takes a card.')
- return (mypoints,True)
- elif len(opphand) == 2 and hit == 'n' and mypoints <= 17:
- print('The dealer takes a card.')
- return (mypoints,True)
- elif len(opphand) == 3 and hit == 'n' and mypoints <= 18:
- print('The dealer takes a card.')
- return (mypoints,True)
- elif len(opphand) > 2 and mypoints <= 16:
- print('The dealer takes a card.')
- return (mypoints,True)
- elif len(opphand) > 3 and mypoints <= 17:
- print('The dealer takes a card.')
- return (mypoints,True)
- return (mypoints,False)
- def game():
- pscore = 0
- dscore = 0
- playerwins = 0
- dealerwins = 0
- game = 'y'
- while game == 'y':
- deck = ['A♥️','2♥️','3♥️','4♥️','5♥️','6♥️','7♥️','8♥️','9♥️','10♥️','J♥️','Q♥️','K♥️','A♣️','2♣️','3♣️','4♣️','5♣️','6♣️','7♣️','8♣️','9♣️','10♣️','J♣️','Q♣️','K♣️','A♦️','2♦️','3♦️','4♦️','5♦️','6♦️','7♦️','8♦️','9♦️','10♦️','J♦️','Q♦️','K♦️','A♠️','2♠️','3♠️','4♠️','5♠️','6♠️','7♠️','8♠️','9♠️','10♠️','J♠️','Q♠️','K♠️']
- play = True
- init = True
- phand = []
- dhand = []
- phit = 'y'
- dhit = True
- print('The current score is:')
- print('You:', playerwins)
- print('Dealer:', dealerwins)
- s(0.5)
- while play == True:
- if init == True:
- phand = pull(deck,phand)
- dhand = pull(deck,dhand)
- phand = pull(deck,phand)
- dhand = pull(deck,dhand)
- pscore = getPointTotal(phand)
- dscore = getPointTotal(dhand)
- init = False
- if dscore <= 21 and dscore != 0 and phit == 'y':
- pscore, phit = postDraw(phand,dhand)
- s(0.5)
- if phit == 'y':
- phand = pull(deck,phand,'You')
- pscore = getPointTotal(phand)
- s(0.5)
- if pscore <= 21 and pscore != 0 and dhit == True:
- dscore, dhit = dealer(dhand,phand,deck,phit)
- s(0.5)
- print(dhit)
- if dhit == True:
- dhand = pull(deck,dhand,'The dealer')
- dscore = getPointTotal(dhand)
- s(0.5)
- if pscore == 0 or dscore == 0:
- phit == 'n'
- dhit = False
- if phit == 'n' and dhit == False:
- play = False
- print('Your final hand is', str(phand) + ', worth', pscore, 'points.')
- s(0.5)
- print('The dealer finishes with the hand', str(dhand) + ', worth', dscore, 'points.')
- s(0.5)
- if pscore > dscore:
- playerwins += 1
- print("You win!")
- elif dscore > pscore:
- dealerwins += 1
- print('The dealer wins.')
- elif pscore == dscore:
- print("It's a tie! Nobody wins.'")
- s(0.5)
- game = input('Play again? (y/n) ')
- game = check(game)
- s(0.5)
- print('The final score is:')
- print('You:', playerwins)
- print('Dealer:', dealerwins)
- s(0.5)
- print('Thanks for playing!')
- game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement