Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I want this Game to run, it's a python code. I want to add the methods for the Blackjack class for now as I am

I want this Game to run, it's a python code. I want to add the methods for the Blackjack class for now as I am on phase 2 on the project it would be great if you can help me implement the blackjack class methods that I have in text form. here is my code.

#!/usr/bin/env python3 import random CARDS_CHARACTERS = {"Spades": "", "Hearts": "", "Diamonds": "", "Clubs": ""}

########################################################################## ## Definitions for the classes: Card, Deck and Hand ########################################################################## class card: def __init__(self, Spades, Hearts, Diamonds, Clubs): self.Spades = Spades self.Heats = Hearts self.Diamonds = Diamonds self.Clubs = Clubs def __init__(self, rank, suit): """ Constructor to initialize the rank and suit to specified values. """ self.suit = suit self.rank = rank @property def value(self): """ property to return the point value of the card """ if self.rank == "Ace": # Ace point is 11 return 11 elif self.rank == "Jack" or self.rank == "Queen" or self.rank == "King": # face cards are 10 return 10 else: # other are same as rank, convert rank to int return int(self.rank) def displayCard(self): """ method to return the string representing the card as of """ return "%s of %s"%(self.rank,self.suit)

class Deck: def __init__(self): """ Constructor to initialize the deck to standard deck of 52 cards """ self._deck = [] # create an empty list # create a list of possible suits and rank suit = ["Spades", "Hearts" ,"Diamonds", "Clubs"] rank = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"] # loop over the suit list for i in range(len(suit)): # loop over the rank list for j in range(len(rank)): # create a Card with jth rank and ith suit and append in deck self._deck.append(Card(rank[j], suit[i])) @property def count(self): """ property to return the number of cards in the deck """ return len(self._deck) def shuffle(self): """ method to shuffle the cards in the deck """ random.shuffle(self._deck)

def dealCard(self): """ method to remove and return the last card in deck """ card = self._deck[-1] self._deck.pop() return card

class Hand: def __init__(self): """ Constructor to initialize the empry cards list """ self.__cards = []

@property def count(self): """ property to return the number of cards in the hand """ return len(self.__cards) @property def points(self): """ property to return the total points of all cards in the hand """ total = 0 # loop to add points of all card in the list to total for card in self.__cards: total += card.value return total

def addCard(self, card): """ method to add card in the list at the end """ self.__cards.append(card) def displayHand(self): """ method to display the cards in the hand using displayCard method """ for card in self.__cards: print(card.displayCard())

class Dice: def __init__(self): self.__value = 1

@property # read-only def value(self): return self.__value def roll(self): self.__value = random.randrange(1, 7)

# make it easy to get the value

def __str__(self): self.__list = []

def addDie (self, die): self.__list.append(die)

def rollAll(self): for die in self.__list: die.roll()

def __iter__(self): for die in self.__list: yield die

def __len__(self): return len(self.__list) class Blackjack: def __init__(self, startingBalance): self.money = startingBalance self.deck = Deck() self.playerHand = Hand() self.dealerHand = Hand() self.bet = 0

def displayCards(self,hand, title): ''' Print the title and display the cards in the given hand in a sorted order''' print(title.upper()) for c in sorted(hand): print(c) print()

def handle_winner(self, winner, message): ''' print the player's hand's points print the message Update self.money according to the winner ''' pass #To be implemented in Phase 2 def getBet(self): ''' Method to update self.bet by prompting the user for the bet amount, making sure bet is less than self.money. ''' pass #To be implemented in Phase 2 def setupRound(self): ''' Setup the round by doing these steps: Call getBet to initialize self.bet, initialize self.deck to a new Deck object and shuffle it initialize self.dealerHand and self.playerHand to new Hand objects deal two cards to the playerHand, and one card to the dealerHand finally, print dealerHand and playerHand using displayCards method ''' pass # To be implemented in Phase 2

def play_playerHand(self): ''' Method to implement player playing his hand by 1. Prompting the user to indicate Hit (h) or Stand (s) 2. If user picks stand, end the player play by returning 3. If user picks hit, deal a card to the playerHand. check if with the latest addition, the hand busts (has > 21 points), if so return otherwise, prompt the player again whether to hit or stand. 4. Print playerHand points ''' pass # To be implemented in Phase 2 def play_dealerHand(self): ''' Method to play the dealer's hand. Continue to deal cards till the points of the dealerHand are less than 17. Print the dealer's hand before returning''' pass # To be implemented in Phase 3 def playOneRound(self): Method implements playing one round of the game 1. Checks if playerHand is a Blackjack, if so handles that case 2. Lets player play his hand if it busts, declares player loser 3. Else lets dealer play his hand. 4. If dealer busts, declares the player to be the winner 5. Otherwise declares the winner based on who has higher points: if Player > dealer, player is the winner else if player < dealer, dealer is the winner else it is a tie pass # To be implemented in Phase 3

def main(): print("Cards - Tester") print()

#test sorting of the cards testcardsList = [Card("Ace","Spades"), Card("Queen","Hearts"), Card("10","Clubs"), Card("3","Diamonds"), Card("Jack","Hearts"), Card("7","Spades")] testcardsList.sort() print("TEST CARDS LIST AFTER SORTING.") for c in testcardsList: print(c) print()

# test deck print("DECK") deck = Deck() print("Deck created.") deck.shuffle() print("Deck shuffled.") print("Deck count:", len(deck)) print()

# test hand hand = Hand() for i in range(10): hand.addCard(deck.dealCard())

print("SORTED HAND") for c in sorted(hand): print(c)

print() print("Hand points:", hand.points) print("Hand count:", len(hand)) print("Deck count:", len(deck))

if __name__ == "__main__": main() I want to implement methods for green text for blackjack class and make sure the code run for this part 2 phase. thanks

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Programming The Perl DBI Database Programming With Perl

Authors: Tim Bunce, Alligator Descartes

1st Edition

1565926994, 978-1565926998

More Books

Students also viewed these Databases questions

Question

=+How might these stem from country and regional cultures?

Answered: 1 week ago