×
Reviews 4.9/5 Order Now

Complete Python Solution for Card-Game Assignment

June 15, 2024
Dr. Olivia Campbell
Dr. Olivia
🇺🇸 United States
Python
Dr. Olivia Campbell holds a Ph.D. in Computer Science from the University of Cambridge. With over 800 completed assignments, she specializes in developing complex Python applications, including fitness trackers and exercise planners. Dr. Campbell's expertise lies in algorithm design and data analysis, ensuring optimal performance and accuracy in every project she undertakes.
Key Topics
  • Instructions
    • Objective
  • Requirements and Specifications
Tip of the day
Break your NetLogo model into simple procedures using functions for better readability. Use the ask command efficiently to control agents and optimize performance by minimizing unnecessary computations in the go procedure. Always test your model with small agent populations before scaling up.
News
LFortran Advances: LFortran, an open-source Fortran compiler, achieved compatibility with the PRIMA library in March 2025, enhancing support for numerical computing.

Instructions

Objective

Write a program to create card-game in python.

Requirements and Specifications

Instructions:

Suppose we want to be able to place cards back into a deck. Modify the Deck class (will be attached below) to include the operations addTop, addBottom, and addRandom (the last one inserts the card at a random location in the deck). And write unit tests to test the methods suit, suitName and rankNameof Card class in Card.py (Will be attached below).

Source Code

from random import randrange from Card import Card class Deck: #------------------------------------------------------------ def __init__(self): """ Creates a 52 card deck in standard order """ cards = [] for suit in Card.SUITS: for rank in Card.RANKS: cards.append(Card(rank,suit)) self.cards = cards #------------------------------------------------------------ def size(self): """Cards left post: Returns the number of cards in self""" return len(self.cards) #------------------------------------------------------------ def deal(self): """ Deal a single card pre: self.size() > 0 post: Returns the next card, and removes it from self.card if the deck is not empty, otherwise returns False""" if self.size() > 0: return self.cards.pop() else: return False #------------------------------------------------------------ def shuffle(self): """Shuffles the deck post: randomizes the order of cards in self""" n = self.size() cards = self.cards for i,card in enumerate(cards): pos = randrange(i,n) card[i] = card[pos] cards[pos] = card def addTop(self, card: Card): """ Add a card at the top (beginning) of the deck (list of cards) """ self.cards.insert(0, card) def addBottom(self, card: Card): """ Add a card at the bottom (last index) of the deck (list of cards) """ self.cards.append(card) def addRandom(self, card:Card): """ Add a card at a random position of the deck """ self.cards.insert(randrange(0, len(self.cards)-1), card)

Similar Samples

Explore our samples At Programming Homework Help, we offer expertly crafted examples to guide you through your coding challenges. Each sample is designed to clarify complex concepts and enhance your learning experience. Check out our samples to see how we can help you succeed in your programming courses.