Microsoft Word - Assignment 3_Baccarat_draft2 Assignment 3 – The Card Game Baccarat Due Date: Friday, November 22nd 2019 at 23:55 Percentage overall grade: 5% Penalties: No late assignments allowed...

1 answer below »
the pdf has instructions to complete the two python code outlines.sample output is also provided for testing


Microsoft Word - Assignment 3_Baccarat_draft2 Assignment 3 – The Card Game Baccarat Due Date: Friday, November 22nd 2019 at 23:55 Percentage overall grade: 5% Penalties: No late assignments allowed Maximum marks: 100 Assignment Specifications You are tasked with creating a single-player version of the card game Baccarat. In your version, the single player will play against the computer, who will always be the dealer. Your game will be played using a standard deck of 52 cards. A deck of 52 cards has four suits: club, diamond, heart, and spade. Each suit has 13 ranks: 2 through 10, Jack, Queen, King, and Ace. In your program, you will initially read in each card as a string with exactly 2 characters in it. The first character represents the rank of the card (A,2,3,4,5,6,7,8,9,T,J,Q,K) and the second character represents the suit (C,D,H,S). So for example, the ten of hearts would be 'TH'. Rules for this version of Baccarat: In Baccarat, each card is assigned a point value based on rank alone. If the rank is numeric and less than 10, the value is its rank; however, the 10 has a value of 0. (e.g. two of spades has a value of 2, ten of hearts has a value 0). If is it a Jack, Queen, or King, its value is also 0. The Ace always has a value of 1. At the beginning of a round, the dealer deals a card (face up) to the player, and a card (face up) to the dealer. The dealer then deals a second card (face up) to the player, and a second card (face up) to the dealer. The value of each hand is determined by summing the point values of the two cards in the hand, and taking the rightmost digit. For example, a hand consisting of 3 of hearts and 4 of spades has a value of 7. But a hand consisting of 8 of diamonds and 7 of clubs has a value of 5 (because 8+7 = 15). Note that because of this rule, the highest possible hand value is 9. If the player’s hand and/or the dealer’s hand equals 8 or 9 at this point, a "natural" has occurred and the round ends. In this case, the winner of the round is the person whose hand has the highest value; if both hands have the same value, the round ends in a tie. For example, if the player has a 4 and 5, the summed value is 4+5 = 9. If the dealer has an 8 and King, the summed value is 8+0 = 8. In this example, both hands are naturals, but the player wins the round because his/her hand has the higher value. If neither hand is a natural, the round continues. The player is dealt a third card only if the current value of his/her hand is 0-5. Otherwise, if the current value of the player’s hand is 6 or 7, the player must stand (i.e. will NOT get a third card). If the player stands, then the dealer is dealt a third card only if the current value of the dealer’s hand is 0-5. Otherwise, if the current value of the dealer’s hand is 6 or 7, the dealer must stand (i.e. will NOT get a third card). However, if the player did receive a third card, the following rules must be applied to determine if the dealer also gets a third card or not: a) If the dealer’s current hand is 2 or less, the dealer gets a third card. b) If the dealers’ current hand is 3, the dealer only gets a third card if the player’s third card was an 8. c) If the dealer’s current hand is 4, the dealer only gets a third card if the player’s third card was 2, 3, 4, 5, 6, or 7. d) If the dealer’s current hand is 5, the dealer only gets a third card if the player’s third card was 4, 5, 6, or 7. e) If the dealer’s current hand is 6, the dealer only gets a third card if the player’s third card was 6 or 7. f) If the dealer’s current hand is 7, then the dealer must stand (i.e. does NOT get a third card) After both the player and dealer have been given the chance to get a third card according to the rules above, the new hand totals are calculated and, again, only the rightmost digit is considered. (For example, if the player has 3 cards: 5, 6, 9 then the player’s new total is 0.) The round must end at this point by declaring a winner (the one with the highest hand total) or a tie. If a hand total at this point is 8 or 9, it is still declared as a "natural". Task 1: Card class Download and save a copy of playing_cards.py skeleton code from eClass. Complete the Card class according to the description below. Card(rank, suit) – creates a card which is described by its rank and suit. The private attributes in the __init__ method have already been assigned values for you. It is your job to assert that the values for the input parameters rank and suit are valid before being used to initialize the attributes. A valid rank is one of the following one-character strings: 'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K' and a valid suit is one of the following one-character strings: 'S', 'H', 'D', 'C'. isFaceCard() – returns True if the Card instance is either a Jack, Queen, or King (of any suit); returns False otherwise. isAce() – returns True if the Card instance is an Ace (of any suit); returns False otherwise. isNumeric() – returns True if the Card instance has a numeric rank (i.e. 2-10); returns False otherwise. getRank() – returns the rank of the Card instance as a one-character string. getSuit() – returns the suit of the Card instance as a one-character string. __str__() – this method has been completed for you. It returns the string representation of the Card instance. Test your Card class thoroughly before moving on. You may wish to use assertions to verify expected behaviour, like in Lab 7 (Linked Lists). Place these tests under if __name__ == "__main__": in playing_cards.py to allow the marker to see your tests. Task 2: Deck class Complete the Deck class in playing_cards.py, according to the description below. Notice that, with the exception of the shuffle behaviour, the deck acts very much like a queue. Where appropriate, model this class after either the bounded queue or the circular queue implemented in Lab 6 (Queues) – whichever will meet the time efficiencies specified below. Deck(capacity) – creates an empty deck that is able to hold a maximum of capacity cards, where capacity should be an integer greater than zero. Ensure that the capacity is valid. addCard(card) – modifies the deck by adding a new card (provided as input) to the bottom of the deck. Nothing is returned. Raise an exception if the deck is full. This method should have an O(1) time efficiency. dealCard() – modifies the deck by removing the card from the top of the deck, and returns that top card. Raise an exception if the deck is empty. This method should have an O(1) time efficiency. deckSize() – returns the integer number of cards in the deck. shuffle() – modifies the deck by randomly changing the order of the existing cards in the deck. Nothing is returned. __str__() – returns the string representation of all of the cards in the deck, with a hyphen ('-') in between each 2-character card. Test your Deck class thoroughly before moving on. Again, place these tests under if __name__ == "__main__": in playing_cards.py to allow the marker to see your tests. You now have a playing_card module that you can use for many different card game programs! Task 3: Player class Download and save a copy of baccarat.py from eClass. Complete the Player class according to the description below. Player() – creates a new player who has no cards. Nothing is returned. updateHand(card) – adds card to the player’s hand. The point value of the player’s hand is updated accordingly. Nothing is returned. clearHand() – reset the hand so that the player has no cards. The point value of the player's hand is updated accordingly. Nothing is returned. getHand() – returns the cards currently in the player’s hand. getHandValue() – returns the point value of the player’s hand, according to the rules of Baccarat. You may create additional helper methods that are called within the Player class; however, these additional helper methods do not form part of the public interface and cannot be used outside of this class. Be sure to test this Player class thoroughly before moving on. Place these tests in a new Python file called baccarat_tests.py to allow the marker to see your tests. Task 4: Table class Complete the Table class in baccarat.py, according to the description below. Table() – creates a new Baccarat table, with one player and one dealer. The table should also create a deck of 52 cards, as read in from
Answered Same DayNov 20, 2021

Answer To: Microsoft Word - Assignment 3_Baccarat_draft2 Assignment 3 – The Card Game Baccarat Due Date:...

Yogesh answered on Nov 22 2021
153 Votes
baccarat.py
from playing_cards import Card, Deck
class Player:
def __init__(self):
self.__hand = []
self.__handValue = 0
def updateHand(self, card):
r = card[0]
s = card[1]
c = Card(r, s)
self.__hand.append(c.__str__())
val = 0
if c.isAce():
val = 1
if c.isFaceCard():
val = 0
if c.isNumeric():
if c.getRank() == "T":
val = 0
else:
val = c.getRank()
self.__handValue = self.__handValue + int(val)
if len(str(self.__handValue)) == 2:
handvalue = str(self.__handValue)
self.__handValue = int(handvalue[1])
def clearHand(self):
self.__hand.clear()
self.__handValue = 0
def getHand(self):
return self.__hand
def getHandValue(self):
return self.__handValue
class Table:
def __init__(self):
self.table = [[], []]
self.discard_pile = []
self.d = Deck("52")
def populateDeck(self):
global f
file = input("Enter the input filename: ")
# file = "shuffleddeck.txt"
try:
f = open(file, "r")
except IOError:
print("\nPlease enter a valid filename.\n")
self.populateDeck()
data = f.readlines()
for line in data:
card = line[0] + line[1]
self.d.addCard(card)
if len(self.d.deck) != 52:
raise Exception("\nDeck does not have 52 cards in it...\n")
deck_set = set(self.d.deck)
unique_deck = list(deck_set)
if len(self.d.deck) != len(unique_deck):
raise Exception("\nDeck does not have 52 unique cards...\n")
def cardsRemaining(self):
return self.d.deckSize()
def deal(self, toWho):
if toWho % 2 == 0: # player
crdp = self.d.dealCard() # crdp is card for player
self.table[0].append(crdp)
return crdp
else: # dealer
crdd = self.d.dealCard() # crdd is card for dealer
self.table[1].append(crdd)
return crdd
def displayTable(self):
p = Player()
for elp in self.table[0]:
p.updateHand(elp)
# scrop is score of player
scrop = p.getHandValue()
p.clearHand()
for eld in self.table[1]:
p.updateHand(eld)
# scrod is score of dealer
scrod = p.getHandValue()
p.clearHand()
print("Player: " + str(self.table[0]).replace("[", "").replace("]", "").replace("'", "").replace(",", "") +
" --> Score = " + str(scrop))
print("Dealer: " + str(self.table[1]).replace("[", "").replace("]", "").replace("'", "").replace(",", "") +
" --> Score = " + str(scrod))
def checkTableValues(self):
p = Player()
for elp in self.table[0]:
p.updateHand(elp)
# scrop is score of player
scrop = p.getHandValue()
p.clearHand()
for eld in self.table[1]:
p.updateHand(eld)
# scrod is score of dealer
scrod = p.getHandValue()
p.clearHand()
return (scrop, scrod)
def...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here