Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN PYTHON -Create a class that represents a deck of playing cards. The data for

ID: 3765978 • Letter: I

Question

IN PYTHON -Create a class that represents a deck of playing cards. The data for this object will be the cards themselves. The methods this class will have are the following deal - a method that returns the value of the card on top of the deck. Once a card is dealt it cannot be dealt again until the deck is shuffled. shuffle - a method that returns to the deck all dealt cards (for a total of 52, no Jokers) and places it in a random order. fan - fan is a method that will simply list the cards in the deck from the top card to the card on the bottom of the deck. isOrdered - a method that returns True if the deck is in order and False if it is not. If an ordered deck has a few cards dealt off of the top it is still in order. You do not need a full deck to be in order. Order - a method that sorts the deck or puts the cards in order with the 2 of clubs beings lowest and the ace of spades being highest (while there is no real ranking of suits we’ll go with the standard poker/bridge ranking of clubs(lowest), diamonds, hearts, spades(highest). We will also count the Ace as a high card and not a low card. The Ace of a particular suit should be the highest card when a suit is sorted (2 thru A). To make it simple, a sorted deck is the following 2C, 3C, 4C, 5C, 6C, 7C, 8C, 9C, 10C, JC, QC, KC, AC, 2D, 3D, 4D, 5D, 6D, 7D, 8D, 9D, 10D, JD, QD, KD, AD, 2H, 3H, 4H, 5H, 6H, 7H, 8H, 9H, 10H, JH, QH, KH, AH, 2S, 3S, 4S, 5S, 6S, 7S, 8S, 9S, 10S, JS, QS, KS, AS

Explanation / Answer

woking python class:

import string
import random

SUITS = ['S', 'C', 'D', 'H']
LONGSUITS = ['spades', 'clubs', 'diamonds', 'hearts']

RANKS = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']
LONGRANKS = ['two', 'three', 'four', 'five', 'six', 'seven', 'eight',
'nine', 'ten', 'jack', 'queen', 'king', 'ace']
ALL_CARDS = ['2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', '10C', 'JC',
'QC', 'KC', 'AC', '2D', '3D', '4D', '5D', '6D', '7D', '8D',
'9D', '10D', 'JD', 'QD', 'KD', 'AD', '2H', '3H', '4H', '5H',
'6H', '7H', '8H', '9H', '10H', 'JH', 'QH', 'KH', 'AH', '2S',
'3S', '4S', '5S', '6S', '7S', '8S', '9S', '10S', 'JS', 'QS',
'KS', 'AS']


class Deck():

def __init__(self):
self.deck = ALL_CARDS[:]
# Use shuffle method to scramble cards
self.shuffle()

def deal(self):
"""card from top of the deck..
"""
return self.deck.pop(0)

def shuffle(self):
"""to shuffle the deck.
"""
random.shuffle(self.deck)

def display(self):
return ", ".join(self.deck)

using the class in python code to get the result:

import deck
obj=deck.Deck()
obj.deck
obj.shuffle()
obj.deck

Code compiled on ideone on Python