import random class Card: ''' The definition of the Card class: attributes: suit, value Methods: Accessors: getValue, getSuit ''' def __init__(self,suit,value): ''' :param suit: Defines the suit of the card :param value: Defines the value of hte card :return: Returns either one of the paramters. ''' self._suit = suit self._value = value #Override repr method def __repr__(self): return str(self._value) + " of " + str(self._suit) #getSuit def getSuit(self): return self._suit def getValue(self): #Dictionary containing values of cards cardValues = {"Ace": 1, "Two":2, "Three":3,"Four": 4 , "Five": 5, "Six":6, "Seven": 7, "Eight": 8, "Nine": 9, "Ten": 10, "Jack":10, "Queen":10, "King":10} return cardValues[self._value] class Deck: ''' The definition of the Deck class attributes: cards Methods: Accessors: Mutators: shuffle, dealCard ''' def __init__(self): self._cards = [] suits = ["Hearts" , "Diamonds" , "Spades" , "Clubs"] values = ["Ace" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" , "Ten" , "Jack" , "Queen" , "King"] #The creation of the deck using a for loop. for suit in suits: for val in values: card = Card(suit , val) self._cards.append(card) #shuffles the deck def shuffle(self): shuffle(self._cards) #Deals a card def dealCard(self): return self._cards.pop() # def __repr__(self): return str(self._cards) def main(): playerMoney = float(input('How much money would you like to start with (Numbers only): ')) computerMoney = playerMoney #Will double if hard mode is chosen pot = 0.0 #Temporary variable for betting deck = Deck() #Definition deck.shuffle() #Shuffles the deck playerHand = [] #Initializes Player hand computerHand = [] #Initializes computer hand startNumCards = 2 #Number of cards that each player starts with in Blackjack computerBet = 0.0 hardMode = input('Would you like to play hard mode (yes or no)?: ') if hardMode.lower() == "yes": hardMode = True else: hardMode = False if not hardMode: #Adding of cards to each of the players' hands for i in range(startNumCards): playerHand.append(deck.dealCard()) computerHand.append(deck.dealCard()) done = False #Loop that runs the game while not done: pot = 0 playerBet = float(input("What do you want to bet: (numbers only) ")) playerSum = 0 #Prints player hand and value of hand. print("Your hand contains: ") for i in range(len(playerHand)): print("|" + str(playerHand[i]) , end='|')#The printing of the Hand for elt in playerHand: #Traverses the hand and assesses the value of each card playerSum += elt.getValue() computerSum = 0 for elt in computerHand: #Traverses the hand and asesses the value of each card computerSum += elt.getValue() print() #Computer hit decision while computerSum <= 17: computerHand.append(deck.dealCard()) computerSum = 0 for elt in computerHand: #Traverses the hand and asesses the value of each card computerSum += elt.getValue() if computerSum == 18: computerBet = computerMoney * (1/4) #Bets 1/4 of total money elif computerSum == 19: computerBet = computerMoney * (1/3) #Bets 1/3 of total money elif computerSum == 20: computerBet = computerMoney * (1/2) #Bets 1/2 of total money elif computerSum == 21: computerBet = computerMoney #Bets all money pot = computerBet + playerBet print("The sum of your cards is", playerSum) print() #Player hit decision doneHit = False playerHitDecision = input('Would you like to hit? (Yes or No): ') if playerHitDecision.lower() == "yeah" or playerHitDecision.lower() == "yes": playerHand.append(deck.dealCard()) while not doneHit: for i in range(len(playerHand)): print("|" + str(playerHand[i]) , end='|')#The printing of the Hand playerSum = 0 for elt in playerHand: #Traverses the hand and asesses the value of each card playerSum += elt.getValue() print("The sum of your hand is ", playerSum) playerHitDecision = input('Would you like to hit? (Yes or No) : ') if playerHitDecision == "yeah" or playerHitDecision.lower() == "yes": playerHand.append(deck.dealCard()) else: doneHit = True if playerHitDecision.lower() == "no": print("You ended up with a final hand value of: ", playerSum) if playerSum > 21: print("You busted!") print('You lose, and the computer\' hand was: ' , computerHand , "and the total was: ", computerSum) done = True #Win checks if playerSum > computerSum and playerSum <= 21 and computerSum < 21: print('The computer\'s hand was: ' , computerHand , 'with a sum of: ' , computerSum) print('You won by' , playerSum - computerSum) #If you both bust elif playerSum > 21 and computerSum > 21: print("Both you and the computer busted, so the dealer wins!. ") print("You finished with a value of", playerSum, "and a hand of", playerHand) print("The computer finished with a value of", computerSum, " and a hand of", computerHand) computerMoney += pot playerMoney -= playerBet #If you lose elif playerSum < computerSum and computerSum <= 21: print("The computer hand was", computerHand, "with a sum of:", computerSum) print("You lost by", computerSum-playerSum) computerMoney += pot playerMoney -= playerBet #if you don't bust and the computer busts. elif playerSum <= 21 and computerSum > 21: print("The computer hand was", computerHand, "with a sum of:", computerSum) print('You won because the computer busted') computerMoney -= computerBet playerMoney += playerBet #if you both are tied elif playerSum == computerSum and playerSum <= 21 and computerSum <= 21: print("The computer hand was", computerHand, "with a sum of:", computerSum) print('You lost because the dealer wins all ties') computerMoney += pot playerMoney -= playerBet #If you bust and computer doesn't elif playerSum > 21 and computerSum <= 21: print("The computer hand was", computerHand, "with a sum of:", computerSum) print('You lost because you busted') computerMoney += pot playerMoney -= playerBet print() print("You have: $" ,playerMoney , "and the computer has: $" , computerMoney) if playerMoney <= 0 or computerMoney <= 0: done = True else: computerMoney *= 2 #Doubles computer's starting money for hard mode #Adding of cards to each of the players' hands for i in range(startNumCards): playerHand.append(deck.dealCard()) done = False #Loop that runs the game while not done: pot = 0 playerBet = float(input("What do you want to bet:(numbers only) ")) playerSum = 0 #Prints player hand and value of hand. print("Your hand contains: ") for i in range(len(playerHand)): print("|" + str(playerHand[i]) , end='|')#The printing of the Hand for elt in playerHand: #Traverses the hand and assesses the value of each card playerSum += elt.getValue() computerSum = 0 for elt in computerHand: #Traverses the hand and asesses the value of each card computerSum += elt.getValue() print() #Computer hit decision computerSum = random.randint(19,21) if computerSum == 19: computerBet = computerMoney * (1/4) #Bets 1/3 of total money elif computerSum == 20: computerBet = computerMoney * (1/3) #Bets 1/2 of total money elif computerSum == 21: computerBet = computerMoney * (1 / 2) #Bets all money pot = computerBet + playerBet print("The sum of your cards is", playerSum) print() #Player hit decision doneHit = False playerHitDecision = input('Would you like to hit? (Yes or No): ') if playerHitDecision.lower() == "yeah" or playerHitDecision.lower() == "yes": playerHand.append(deck.dealCard()) while not doneHit: for i in range(len(playerHand)): print("|" + str(playerHand[i]) , end='|')#The printing of the Hand playerSum = 0 for elt in playerHand: #Traverses the hand and asesses the value of each card playerSum += elt.getValue() print("The sum of your hand is ", playerSum) playerHitDecision = input('Would you like to hit? (Yes or No) : ') if playerHitDecision == "yeah" or playerHitDecision.lower() == "yes": playerHand.append(deck.dealCard()) else: doneHit = True if playerHitDecision.lower() == "no": print("You ended up with a final hand value of: ", playerSum) if playerSum > 21: print("You busted!") print('You lose, and the computer\'s total was: ', computerSum) done = True #Win checks if playerSum > computerSum and playerSum <= 21 and computerSum < 21: print('The computer\'s sum was: ' , computerSum) print('You won by' , playerSum - computerSum) #If you both bust elif playerSum > 21 and computerSum > 21: print("Both you and the computer busted, so the dealer wins!. ") print("You finished with a value of", playerSum, "and a hand of", playerHand) print("The computer finished with a value of", computerSum) computerMoney += pot playerMoney -= playerBet #If you lose elif playerSum < computerSum and computerSum <= 21: print("The computer\'s sum was:", computerSum) print("You lost by", computerSum-playerSum) computerMoney += pot playerMoney -= playerBet #if you don't bust and the computer busts. elif playerSum <= 21 and computerSum > 21: print("The computer's sum was:", computerSum) print('You won because the computer busted') computerMoney -= computerBet playerMoney += pot * (3 / 4) #if you both are tied elif playerSum == computerSum and playerSum <= 21 and computerSum <= 21: print("The computer's sum was:", computerSum) print('You lost because the dealer wins all ties') computerMoney += pot playerMoney -= playerBet #If you bust and computer doesn't elif playerSum > 21 and computerSum <= 21: print("The computer's sum was", computerSum) print('You lost because you busted') computerMoney += pot playerMoney -= playerBet print() print("You have: $" ,playerMoney , "and the computer has: $" , computerMoney) if playerMoney <= 0 or computerMoney <= 0: done = True main()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question