Hi! Below I've attached my current code. I've been trying to fix it but it was not working. The correct output is below, as well as my current output.
The specifications are:
IN PYTHON:
In the code template below, you are given three classes: Card, CardSet, and Deck.
The Card class has the attributes of value and suite, which describe the cards in a standard 52-card deck. The attribute value takes in the digits 1 to 10 as well as the standard face cards, Jack, Queen, and King. The suite attribute takes in the four suites, namely clubs, spades, hearts, and diamonds. Obviously, two cards are equal if they have the same value and suite.
The CardSet class is just an ensemble of Card instances. To know which cards are in your set, just iterate through the cards via the view method. This gets a formatted list of your cards in the cards attribute. To add cards to your set, use the add_cards method.
The Deck class is a child class of the CardSet class. To initialize a deck, all 52 cards from the standard deck must be added to it. For uniformity, place each suite in ascending value -- 1 to 10, then Jack, then Queen, then King. The suites must be placed in this order: clubs - spades - hearts - diamonds.
NOTE: All the diamond-suited cards are at the top of the deck, while the clubs are at the bottom of the deck.
Current Code:
import random
class Card:
def __init__(self, value, suite):
self.value = value
self.suite = suite
def __str__(self):
return f"{self.value} of {self.suite}"
def __eq__(self, other):
"""Check if two cards are the same"""
return (self.value == other.value and self.suite == other.suite)
class CardSet:
def __init__(self):
self.cards = []
def view(self):
for card in self.cards:
print(card)
def add_cards(self, cards):
"""Add cards to your set"""
for card in cards:
self.cards.append(card)
class Deck(CardSet):
def __init__(self):
"""Initialize the 52-card set. Start from 1-11, then Jack, Queen, King, then by suite: clubs, spades, hearts, diamonds"""
cards = []
for suite in ("clubs", "spades", "hearts", "diamonds"):
for value in ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"):
card = Card(value, suite)
cards.append(card)
self.cards = cards
def count_cards(self):
""""Count the number of cards in a deck"""
return len(self.cards)
def shuffle(self, seed=None):
"""Shuffle your deck using a random seed"""
random.seed(seed)
random.shuffle(self.cards)
def peek(self, number=5):
"""Show the top n cards of the stack. This is analogous to getting the last n cards then reversing it."""
cnt = 0
if number > len(self.cards):
cnt = len(self.cards)
else:
cnt = number
for i in range(-5, -5+cnt):
print(self.cards[i])
def draw(self, cardset, number=5):
"""Transfer the top n cards of the stack to your cardset."""
cards = []
cnt = 0
if number > len(self.cards):
cnt = len(self.cards)
else:
cnt = number
for i in range(-5, -5+cnt):
cards.append(self.cards[i])
self.cards.pop()
cards.reverse()
cardset.add_cards(cards)
def add_cards(self):
pass
if __name__ == "__main__":
seed, hand, peek = input().split(",")
myDeck = Deck()
handA = CardSet()
handB = CardSet()
myDeck.shuffle(int(seed))
for x in range(1,3):
print(f"\nRound {x}:")
myDeck.draw(handA, int(hand))
myDeck.draw(handB, int(hand))
print("Hand A: ")
handA.view()
print("Hand B: ")
handB.view()
myDeck.count_cards()
if(x == 1):
print(f"\n{peek} Cards at the top: ")
myDeck.peek(int(peek))
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.