Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python # Deck.py from random import randrange from Card import Card class Deck(object): #------------------------------------------------------------ def __init__(self): post: Creates a 52 card deck in standard order

image text in transcribedimage text in transcribed

Python

# Deck.py

from random import randrange from Card import Card class Deck(object):

#------------------------------------------------------------

def __init__(self):

"""post: Creates a 52 card deck in standard order""" list 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) cards[i] = cards[pos] cards[pos] = card

---------------------------------------------------------------------------------------------------------------------------------------------------

# Card.py # updated for Bridge with Ace being the greatest # Card.py 

class Card(object): SUITS = 'cdhs'

SUIT_NAMES = ['Clubs', 'Diamonds', 'Hearts', 'Spades']

RANKS = list(range(1,14))

RANK_NAMES = ['Ace', 'Two', 'Three', 'Four', 'Five', 'Six','Seven', 'Eight', 'Nine', 'Ten','Jack', 'Queen', 'King']

def __init__(self, rank, suit): #print(self.RANKS) self.rank_num = rank self.suit_char = suit

def suit(self): return self.suit_char

def rank(self): return self.rank_num

def suitName(self): index = self.SUITS.index(self.suit_char) return self.SUIT_NAMES[index]

def rankName(self): index = self.RANKS.index(self.rank_num)

return self.RANK_NAMES[index]

def __str__(self): return self.rankName() + ' of ' + self.suitName()

Programming Exercises 1. Modify the Deck class to keep track of the current size of the deck using an instance variable. Does this change the run-time efficiency of the size operation? Do a bit of research to answer this question. the shuffling code in the Deck class. class to include the operations addTop, addBottom, and addRandom (the last 2. Look into the functions provided by the Python random module to simplify 3. Suppose we want to be able to place cards back into a deck. Modify the Deck one inserts the card at a random location in the deck)

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

Database Principles Programming And Performance

Authors: Patrick O'Neil, Elizabeth O'Neil

2nd Edition

1558605800, 978-1558605800

More Books

Students also viewed these Databases questions

Question

On what three main factors does the theory of constraints focus?

Answered: 1 week ago

Question

Discuss the characteristics of neuralnet package in R.

Answered: 1 week ago