I'm suppose to make a program that uses a Card class to simulate the random dealing of a single Blackjack hand by a Blackjack dealer who follows the standard rules. As it simulates the the single hand, it should randomly draw a new card (by randomly choosing a suit and value), print the name of the card, print the current total the dealer has and keep going until the standard rules say that the dealer should stop. Then it should print the final value of the randomly simulated hand. If the dealer busts it should say so and print a value of 0. This is what I have so far:
from random import randrange
class Card:
def __init__(self, rank, suit):
# rank is an integer in the range 1 to 13
# suit is a single character string
# "d" for diamonds
# "c" for clubs
# "h" for hearts
# "s" for spades
self.rank = rank
self.suit = suit
def getRank(self):
return self.rank
def getSuit(self):
return self.suit
def BJValue(self):
# returns the BlackJack value of the card
if self.rank > 10:
return 10
else:
return self.rank
def __str__(self):
# returns a string representation of the card, for example
# "Ace of Spades" or "Ten of Clubs"
if self.rank == 1:
rankString = "Ace"
elif self.rank == 2:
rankString = "Two"
elif self.rank == 3:
rankString = "Three"
elif self.rank == 4:
rankString = "Four"
elif self.rank == 5:
rankString = "Five"
elif self.rank == 6:
rankString = "Six"
elif self.rank == 7:
rankString = "Seven"
elif self.rank == 8:
rankString = "Eight"
elif self.rank == 9:
rankString = "Nine"
elif self.rank == 10:
rankString = "Ten"
elif self.rank == 11:
rankString = "Jack"
elif self.rank == 12:
rankString = "Queen"
elif self.rank == 13:
rankString = "King"
if self.suit == "d":
suitString = "Diamonds"
elif self.suit == "c":
suitString = "Clubs"
elif self.suit == "h":
suitString = "Hearts"
elif self.suit == "s":
suitString = "Spades"
return rankString + " of " + suitString
# This function simulates the dealing of a single hand.
def dealHand(Card):
total = Card
if Card == 1:
haveAce = True
else:
haveAce = False
while total < 17:
# This randomly generates a card.
# Note that 11 is Jack, 12 is Queen, 13 is King
# Here we care about distinguishing facecards, because
# we want to generate the right probability
# distribution of cards.
card = randrange(1,14)
if card == 1:
haveAce = True
total = total + BJValue(card)
if haveAce:
total = adjustForAce(total)
return total
# This returns the Blackjack value of a card. All facecards are 10.
def BJValue(card):
if card > 10:
return 10
else:
return card
# This is slightly tricky. It checks if total+10 is between 17 and 21.
def adjustForAce(total):
if 16 < total + 10 < 22:
return total + 10
else:
return total
def main():
for Card in range(self.rank):
for Card in range(self.suit):
busts = 0
for i in range(Card):
points = dealHand(Card)
if points > 21:
busts = busts + 1
print "Dealer busted."
print "Drew card: ",
print "Final value of hand: ",
if __name__ == '__main__':
main()