Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON UPDATING BLACKJACK: Here is the first object.py file import random CARDS_CHARACTERS = {Spades: , Hearts: , Diamonds: , Clubs: } class Card: def __init__(self,

PYTHON UPDATING BLACKJACK:

Here is the first object.py file

import random CARDS_CHARACTERS = {"Spades": "", "Hearts": "", "Diamonds": "", "Clubs": ""}

class Card: def __init__(self, rank, suit): self.suit = suit self.rank = rank @property def value(self): if self.rank == "Ace": return 11 elif self.rank == "Jack" or self.rank == "Queen" or self.rank == "King": return 10 else: return int(self.rank) def displayCard(self): return (self.rank+" of "+self.suit)

class Deck: def __init__(self): self._deck = [] suit = ["Spades ", "Hearts ", "Diamonds ", "Clubs "] rank = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"] for i in range(len(suit)): for j in range(len(rank)): self._deck.append(Card(rank[j], suit[i])) @property def count(self): return len(self._deck) def shuffle(self): random.shuffle(self._deck)

def dealCard(self): card = self._deck[-1] self._deck.pop() return card

class Hand: def __init__(self): self.__cards = []

@property def count(self): return len(self.__cards) @property def points(self): total = 0 for card in self.__cards: total += card.value return total

def addCard(self, card): self.__cards.append(card) def displayHand(self): for card in self.__cards: print(card.displayCard())

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

# test deck print("DECK") deck = Deck() print("Deck created.") deck.shuffle() print("Deck shuffled.") print("Deck count:", deck.count) print() ## # test hand print("HAND") hand = Hand() for i in range(4): hand.addCard(deck.dealCard()) ## hand.displayHand() print() ## print("Hand points:", hand.points) print("Hand count:", hand.count) print("Deck count:", deck.count)

if __name__ == "__main__": main()

Here is the prompt:

image text in transcribedimage text in transcribedimage text in transcribed

blackjack.py code:

from objects import Card, Deck, Hand

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

def main(): print("BLACKJACK!") print("Blackjack payout is 3:2")

# initialize starting money money = 100 print("Starting Balance:", money)

blackjack = Blackjack(money) # start loop again = 'y' while again.lower() == 'y':

print("Setting up a round...") blackjack.setupRound()

print("Playing Player Hand...") blackjack.play_playerHand()

print() again = input("Play again? (y): ").lower() print() if again != "y": break

print("Bye!")

# if started as the main module, call the main function if __name__ == "__main__": main()

Please add comments on why you made the decisions you did so I can compare them to mine. Thank you! :)

Step 1: Update classes in objects.py: You will update the classes in the objects.py in the following way: 1. Card Class: a. You will make the Card class comparable so that when a hand is displayed, the cards in the hand are shown in a sorted order (what set of special methods will you need to add?). Remember that when comparing two cards, you want to compare their suit attributes first and then the rank attributes. Note that the suits follow this order: Spades > Hearts > Diamonds > Clubs. (Luckily, these are alphabetically ordered as well!) The ranks follow this order: Ace > King > Queen > Jack > 10 >9 >8>7>6>5 >4>3 > 2. There are many ways to implement this. b. You will make the card class printable. You should replace the displayCard method with one of the special methods. (Which one?) 2. Deck and Hand Classes: a. Make the Deck and Hand classes iterable, by adding a Generator function as we discussed in the class. One possible way is to have a dictionary, say RANKORDER defined whose keys are the possible ranks ("Ace", "King", ... "4","3", "2"), and values are some integers that match the desired ordering of the ranks. E.g. RANKORDER = { "2":2,"3": 3, ... "10":10,"Jack":11,"Queen":12, "King": 13, "Ace": 14} Clearly RANKORDER["2"] >> Ln: 162 Cok: 0 The main function has a list called testcardsList, comprising of test cards. When the sort method is called on this list and the result printed, it should show them in sorted order as seen in the sample output. Also the cards displayed in the sorted hand towards the end of the main function should appear is correct sort order. Step 2: Add code to Blackjack class in blackjack.py file: You are given a file blackjack.py which contains partially implemented Blackjack class which implements the rules of the game we are developing (as described in the Appendix) and a main function that uses this class to play the game. You don't need to change the code in the main function. But you will implement four of the methods of the Blackjack class. Here's the structure of the file: Class Browser - blackjackFinalPhase3 blackjackFinalPhase3.py class Blackjack def _init_c...) def display Cards(...) def handle_winner(...) def getBet(...) def setupRound(...) def play_playerHand..) def play_dealerHand (...) def playOneRound(...) def main(...) Some of the methods of the class are already implemented (marked in green below), some you will implement in this phase (marked in red), and the rest will be completed in the next phase (marked in grey) to complete the implementation of the game. 1. _init__(self) (Implemented) The constructor of the class takes one required argument called startingBalance that specifies the money the player has available for betting at the start of the game. The constructor also initializes all the attributes: deck, player Hand, dealer Hand, money and bet appropriately. 2. displayCards (self, hand, title): (Implemented) Method to print the title and display the cards from the hand in a sorted order. 3. handle_winner (self, winner, message): (To be implemented in Phase 2) Method to handle given winner printing the message and updating self.money accordingly. This method should: a. Print the player's hand points (e.g. "YOUR POINTS: 18") b. Print the given message Step 1: Update classes in objects.py: You will update the classes in the objects.py in the following way: 1. Card Class: a. You will make the Card class comparable so that when a hand is displayed, the cards in the hand are shown in a sorted order (what set of special methods will you need to add?). Remember that when comparing two cards, you want to compare their suit attributes first and then the rank attributes. Note that the suits follow this order: Spades > Hearts > Diamonds > Clubs. (Luckily, these are alphabetically ordered as well!) The ranks follow this order: Ace > King > Queen > Jack > 10 >9 >8>7>6>5 >4>3 > 2. There are many ways to implement this. b. You will make the card class printable. You should replace the displayCard method with one of the special methods. (Which one?) 2. Deck and Hand Classes: a. Make the Deck and Hand classes iterable, by adding a Generator function as we discussed in the class. One possible way is to have a dictionary, say RANKORDER defined whose keys are the possible ranks ("Ace", "King", ... "4","3", "2"), and values are some integers that match the desired ordering of the ranks. E.g. RANKORDER = { "2":2,"3": 3, ... "10":10,"Jack":11,"Queen":12, "King": 13, "Ace": 14} Clearly RANKORDER["2"] >> Ln: 162 Cok: 0 The main function has a list called testcardsList, comprising of test cards. When the sort method is called on this list and the result printed, it should show them in sorted order as seen in the sample output. Also the cards displayed in the sorted hand towards the end of the main function should appear is correct sort order. Step 2: Add code to Blackjack class in blackjack.py file: You are given a file blackjack.py which contains partially implemented Blackjack class which implements the rules of the game we are developing (as described in the Appendix) and a main function that uses this class to play the game. You don't need to change the code in the main function. But you will implement four of the methods of the Blackjack class. Here's the structure of the file: Class Browser - blackjackFinalPhase3 blackjackFinalPhase3.py class Blackjack def _init_c...) def display Cards(...) def handle_winner(...) def getBet(...) def setupRound(...) def play_playerHand..) def play_dealerHand (...) def playOneRound(...) def main(...) Some of the methods of the class are already implemented (marked in green below), some you will implement in this phase (marked in red), and the rest will be completed in the next phase (marked in grey) to complete the implementation of the game. 1. _init__(self) (Implemented) The constructor of the class takes one required argument called startingBalance that specifies the money the player has available for betting at the start of the game. The constructor also initializes all the attributes: deck, player Hand, dealer Hand, money and bet appropriately. 2. displayCards (self, hand, title): (Implemented) Method to print the title and display the cards from the hand in a sorted order. 3. handle_winner (self, winner, message): (To be implemented in Phase 2) Method to handle given winner printing the message and updating self.money accordingly. This method should: a. Print the player's hand points (e.g. "YOUR POINTS: 18") b. Print the given message

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

More Books

Students also viewed these Databases questions

Question

How do we perceive high-frequency sounds (above 4000 Hz)?

Answered: 1 week ago

Question

1. Effort is important.

Answered: 1 week ago