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.
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python