Blackjack In Python

Blackjack in python

This first thing we need to do is create a deck of cards. In order to make a deck of cards we must first understand what a deck of cards is.

Python

A pack is made up of 52 cards (excluding jokers)

  1. I made blackjack to the best of my ability in Python. I'm pretty happy with it seeing that I only started coding a few months ago. Anyways try it out and give me any feedback in the comments ty.
  2. Learn Python - BlackJackIn this video: - Functions - Nested for loops - While loops - If statements - Comments - Welcome to Coding4Chicks -I am a prog.

Each card in the deck has three attributes:

  • Its name
    • 1,2,3,4,5,6,7,8,9,10,jack,queen,king
  • Its value:
    • 1,2,3,4,5,6,7,8,9,10,11,12,13
  • Its suit:
    • Spades, Diamonds,Hearts,Clubs.

Blackjack In Python With Classes

A standard deck of playing cards has four suits (Hearts, Diamonds, Spades and Clubs) and thirteen values (2 through 10, Jack, Queen, King and Ace) which makes a total of 52 cards per deck. A Python Blackjack Game. 14 Years Ago Ene Uran. Just a simple console blackjack game. The bread and butter code was written with lots of comments, so you can improve on it. Really, the most important part is figuring out when an ace is 11 in value and when it is a 1 in value, so you don't bust. A toolkit for developing and comparing reinforcement learning algorithms.

In Python it is easy to create this full deck of cards using 2 lists and a couple of for loops:

Here we have created a list of 52 cards, with each card consisting of a tuple of 2 values:

(name,suit)

Once we have created the deck of cards it is useful to create a few helper functions to deal with the pack.

  • getRealName – This returns the card’s full name in a human readable form
  • getValue – This return’s the card’s value, useful for calculations

Creating the mainScreen

In order to create the main game screen we first need to come up with a plan! You can have a look at my plan to the right of the screen.

From the plan I can see that I am going to need:

Functions

  • A MainScreen() function
  • A function to display whose turn it is
  • A function to display each player’s hand
  • A function to get input from the player
  • A function to calculate & display who is the winner / loser.
  • A function to calculate a player’s score.

Variables

Coding Blackjack In Python

  • playerTurn variable to keep track of whose turn it is.
  • A round variable to see if it is round one or 2.

Data Structures

  • A list for each player to contain the cards that they have been dealt.

Dec 28th, 2011
Never
Blackjack
Not a member of Pastebin yet?Sign Up, it unlocks many cool features!
  1. Class based Blackjack game
  2. ''
  3. importrandomas r
  4. RULES =''--------------------------------
  5. dealer gives 2 cards to player and himself, only one visible for himself
  6. card values are number value, face cards = 10, ace = 1 or 11
  7. player can hit or stand or split
  8. only split if same value card
  9. split gives player 2 hands, original bet must be matched on 2nd hand
  10. can split 3 times on all cards but aces
  11. can only split once if aces, not blackjack if 10 on both
  12. if pointvalue > 21 = BUST
  13. dealer must hit if <= 16
  14. dealer must stay if >= 17
  15. If player beats dealer, gets double money back, or 1:1
  16. If player get blackjack on first turn, 1.5:1
  17. Player can place double original bet, but must take only one more card
  18. These rules are not very professional will fix later... were just notes for me to write the game :D
  19. deck =['Ace of Spades','2 of Spades','3 of Spades','4 of Spades','5 of Spades','6 of Spades',
  20. '7 of Spades','8 of Spades','9 of Spades','10 of Spades','Jack of Spades','Queen of Spades',
  21. 'King of Spades','Ace of Hearts','2 of Hearts','3 of Hearts','4 of Hearts','5 of Hearts',
  22. '6 of Hearts','7 of Hearts','8 of Hearts','9 of Hearts','10 of Hearts','Jack of Hearts',
  23. 'Queen of Hearts','King of Hearts','Ace of Clubs','2 of Clubs','3 of Clubs','4 of Clubs',
  24. '5 of Clubs','6 of Clubs','7 of Clubs','8 of Clubs','9 of Clubs','10 of Clubs',
  25. 'Jack of Clubs','Queen of Clubs','King of Clubs','Ace of Diamonds','2 of Diamonds','3 of Diamonds',
  26. '4 of Diamonds','5 of Diamonds','6 of Diamonds','7 of Diamonds','8 of Diamonds','9 of Diamonds',
  27. '10 of Diamonds','Jack of Diamonds','Queen of Diamonds','King of Diamonds'
  28. def checkpoints(hand_list):
  29. pointdict ={
  30. 'Ace of Spades':'ace','2 of Spades':2,'3 of Spades':3,'4 of Spades':4,'5 of Spades':5,'6 of Spades':6,'7 of Spades':7,'8 of Spades':8,'9 of Spades':9,'10 of Spades':10,'Jack of Spades':10,'Queen of Spades':10,'King of Spades':10,
  31. 'Ace of Hearts':'ace','2 of Hearts':2,'3 of Hearts':3,'4 of Hearts':4,'5 of Hearts':5,'6 of Hearts':6,'7 of Hearts':7,'8 of Hearts':8,'9 of Hearts':9,'10 of Hearts':10,'Jack of Hearts':10,'Queen of Hearts':10,'King of Hearts':10,
  32. 'Ace of Clubs':'ace','2 of Clubs':2,'3 of Clubs':3,'4 of Clubs':4,'5 of Clubs':5,'6 of Clubs':6,'7 of Clubs':7,'8 of Clubs':8,'9 of Clubs':9,'10 of Clubs':10,'Jack of Clubs':10,'Queen of Clubs':10,'King of Clubs':10,
  33. 'Ace of Diamonds':'ace','2 of Diamonds':2,'3 of Diamonds':3,'4 of Diamonds':4,'5 of Diamonds':5,'6 of Diamonds':6,'7 of Diamonds':7,'8 of Diamonds':8,'9 of Diamonds':9,'10 of Diamonds':10,'Jack of Diamonds':10,'Queen of Diamonds':10,'King of Diamonds':10
  34. ##############################################
  35. for card in hand_list:
  36. possible_points =[]
  37. if item 'ace':
  38. possible_points.append(1)
  39. else:
  40. possible_points[i] +=1
  41. possible_points.append(possible_points[-1]+10)
  42. for item in points:
  43. try:
  44. possible_points[i] += item
  45. continue
  46. iflen(possible_points)0:
  47. else:
  48. possible_points.sort()
  49. class BlackjackGame:
  50. print'Starting up Game'
  51. deck =['Ace of Spades','2 of Spades','3 of Spades','4 of Spades','5 of Spades','6 of Spades',
  52. '7 of Spades','8 of Spades','9 of Spades','10 of Spades','Jack of Spades','Queen of Spades',
  53. 'King of Spades','Ace of Hearts','2 of Hearts','3 of Hearts','4 of Hearts','5 of Hearts',
  54. '6 of Hearts','7 of Hearts','8 of Hearts','9 of Hearts','10 of Hearts','Jack of Hearts',
  55. 'Queen of Hearts','King of Hearts','Ace of Clubs','2 of Clubs','3 of Clubs','4 of Clubs',
  56. '5 of Clubs','6 of Clubs','7 of Clubs','8 of Clubs','9 of Clubs','10 of Clubs',
  57. 'Jack of Clubs','Queen of Clubs','King of Clubs','Ace of Diamonds','2 of Diamonds','3 of Diamonds',
  58. '4 of Diamonds','5 of Diamonds','6 of Diamonds','7 of Diamonds','8 of Diamonds','9 of Diamonds',
  59. '10 of Diamonds','Jack of Diamonds','Queen of Diamonds','King of Diamonds'
  60. temp =raw_input('How many decks does this dealer get? ')
  61. temp =raw_input('Invalid input.nHow many decks does this dealer get? ')
  62. print'Too many... not realistic enough... i'll just go with 5...'
  63. self.dealerdeck= deck * 5
  64. whileTrue:
  65. temp =raw_input('Would you like to play a round?(y/n) ')
  66. while temp.lower()notin'yn'or temp.lower()notin'ny':
  67. temp =raw_input('Invalid InputnWould you like to play a round?(y/n) ')
  68. returnFalse
  69. returnTrue
  70. iflen(self.dealerdeck)>10:
  71. dealerhand =[]
  72. z = r.randint(0,len(self.dealerdeck)-1)
  73. hand.append(y)
  74. z = r.randint(0,len(self.dealerdeck)-1)
  75. dealerhand.append(y)
  76. else:
  77. self.dealerdeck=self.getdealerdeck()
  78. def play(self):
  79. tempyn =raw_input('Would you like to read the rules?(y/n) ').lower()
  80. tempyn =raw_input('Invalid inputnWould you like to read the rules?(y/n) ').lower()
  81. print RULES
  82. tempyn =raw_input('Would you like to play a hand?(y/n) ').lower()
  83. tempyn =raw_input('Invalid inputnWould you like to play a hand?(y/n) ').lower()
  84. self.dealerdeck,self.playerhand,self.dealerhand=self.gethand()
  85. else: break
  86. def playround(self):
  87. print'Your hand has:',self.playerhand[0],'and',self.playerhand[1]
  88. print'You can see the dealer has:',self.dealerhand[1]
  89. if21in checkpoints(self.playerhand):
  90. #bet = bet + bet*1.5
  91. elif21in checkpoints(self.dealerhand):
  92. print'Dealer gets blackjack'
  93. splitpossible =False
  94. x ='You may; hit(H), stand(S), doubledown(DD), surrender(I'm French)'
  95. x +=', split(split)'
  96. print x
  97. playerchoice =raw_input('Please choose: ').lower()
  98. while playerchoice notin['stand','hit','doubledown','surrender','h','s','dd','i'm french']:
  99. playerchoice =raw_input('InvalidnPlease choose again: ').lower()
  100. whileTrue:
  101. self.playerhand.append(self.dealerdeck.pop(r.randint(0,len(self.dealerdeck)-1)))
  102. if checkpoints(self.playerhand)[0]>21:
  103. elif checkpoints(self.playerhand)[0]21:
  104. print'Blackjack, let's see if the dealer also gets it...'
  105. while temp !='h'and temp !='s':
  106. temp =raw_input('Invalid...nHit or Stay? (h/s)').lower()
  107. break
  108. print'Bust - Dealer wins'
  109. print'Dealer has',self.dealerhand[0]
  110. self.dealerhand.append(self.dealerdeck.pop(r.randint(0,len(self.dealerdeck)-1)))
  111. if checkpoints(self.dealerhand)[0]>21:
  112. else:
  113. playerpoints = checkpoints(self.playerhand)
  114. for x inrange(len(playerpoints)):
  115. playerpoints.pop(x)
  116. if dealerpoints[x]>21:
  117. if playerpoints[-1] dealerpoints[-1]:
  118. elif dealerpoints[-1]> playerpoints[-1]:
  119. else:
  120. if splitpossible True:
  121. while playerchoice notin['stand','hit','doubledown','surrender','h','s','dd','i'm french','split']:
  122. playerchoice =raw_input('InvalidnPlease choose again: ')
  123. ------------------------------------
  124. ----------------------------------
  125. -------------
  126. -------------
  127. Dmaster_773
  128. :)
  129. /
  130. ''
  131. lol = BlackjackGame()
  132. lol.getdealerdeck()
RAW Paste Data