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

PYTHON 1. Construct an object class named Card that will represent individual pl

ID: 3778988 • Letter: P

Question

PYTHON

1. Construct an object class named Card that will represent individual playing cards from a standard 52- card deck (no Jokers or special cards). Each Card object will have two instance variables to represent the value and suit of the playing card. The values you choose are up to you. Include the following methods in the Card class:

a. getValue() : Return the value of the card
b. getSuit() : Return the suit of the card
c. __str__() : Return a string representing the card value and suit

A. Poker Hands

There are many variants of the gambling card-game "Poker". The classic 5-card draw variant involves ranking a hand consisting of 5 cards from a 52-card deck of cards and then awarding the accumulated "pot" of money to the person with the highest ranking hand.

Write a Python program that will simulate "dealing" 100,000 5-card hands and then report the frequency of occurrence of "natural" Poker hands. A "natural hand" is one that is dealt "as-is" without replacing any cards.

You should first consult the following wikipedia page for a full description of the ranking of 5-card poker hands:

http://en.wikipedia.org/wiki/List_of_poker_hands

Requirements:
You must do the following:

Construct an object class named Carddeck that will simulate a deck of playing cards. The Carddeck class should include at least one instance variable: a list of 52 Card objects.

The Carddeck class must include the following methods:

__repr__() : Return a string with the deck displayed as a list

shuffle() : Shuffle the deck (use .shuffle() from the random module)

dealcards(n) : Return the "next" n Card objects from the deck in a list. Note that after

the last card in the deck has been dealt, a new shuffled deck should be created automatically! If n is greater than the number of cards remaining in the deck, the new deck should not include the cards at the end that have just been dealt.

Construct a third object class named Pokerhand that will represent a 5-card poker hand of "cards" dealt from a Carddeck object. The Pokerhand class must include a single instance variable: a list containing 5 Cards.

The Pokerhand class must include the following methods:

newHand(deck):get5newcardsfromthespecifieddeck

__repr__ : Return the hand-list as a string

rank() : Return the rank of the poker hand as an integer value as follows:

0 : High card
1 : One pair
2 : Two pair
3 : Three of a kind 4 : Straight

5 : Flush
6 : Full house
7 : Four of a kind 8 : Straight flush

Note that this may be somewhat challenging to construct. It will be much simpler if you use the Python container classes set and dict to evaluate the number of different suits and values in the hand. For example, if there is exactly 1 suit, then the hand must be some sort of 'flush'...

6. Write a non-pure function named main() that will take no arguments and do the following

Instantiate a single Carddeck object and shuffle it.

Instantiate a single Pokerhand object.

Use a loop to "deal" 100,000 5-card Pokerhands and use a dictionary to count the frequency

of occurrence of each of the 9 possible poker hand rankings

Display the resulting counts using the hand "names" (strings) in order of least frequent to most

Example:

Straight Flush: 8

Fourofakind: 33

Full house: 163

Flush:206

Straight:407
Three of a kind Two pair:2244
One pair:4850
High card : 49819

Explanation / Answer

Executable Code

#Class MyCard.
class MyCard:

    #Constructor.
    def __init__(self, myrank, mysuit):
        self.myrank = myrank
        self.mysuit = mysuit

    #Function to print.
    def __str__(self):
        return self.myrank + self.mysuit

    #Function to get myrank.
    def getmyRank(self):
        return self.myrank

    #Function to get mysuit.
    def getmySuit(self):
        return self.mysuit

#Class MyCardDeck.
class MyCardDeck(MyCard):

    #For random
    import random

    #Variables
    myvalues={2:"2",3:"3",4:"4",5:"5",6:"6",7:"7",8:"8",9:"9",10:"T",11:"J",12:"Q",13:"K",14:"A"}
    mysuits={1:"C",2:"S",3:"D",4:"H"}
    myindex=0
    mycard=" "

    #Constructor.
    def __init__(self,myvalue="",mysuit=""):      
        self.myvalue=myvalue
        self.mysuit=mysuit

    #Function to return string with the deck displayed as a list.
    def __repr__():
        i1=0
        j1=0
        for i1 in mysuits:
            for j1 in myvalues:
                mycard[i1*j1]=mycard[i1*j1] + mysuits[i1],",", myvalues[j1]
                return mysuits[i1],",", myvalues[j1]

    #Function myshuffle().
    def myshuffle():
        i1=0
        for i1 in len(self.mycard):
            self.mycard[i1]=random.myshuffle()
            self.mycard[i1+1].myindex=0

    #Function mydealcard().
    def mydealcard():
        for i1 in len(self.mycard):
            return self.mycard[i1+1]

    #Function mydealhand().
    def mydealhand():
        return MyPokerHand(list(self.mycard[0], self.mycard[1], self.mycard[2], self.mycard[3], self.mycard[4]))

#Class MyPokerHand.
class MyPokerHand(MyCardDeck):
    a=MyCardDeck()
    myranking={0:'High card',
                 1:'One pair',
                 2:'Two pair',
                 3:'Three of a kind',
                 4:'Straight',
                 5:'Flush',
                 6:'Full house',
                 7:'Four of a kind',
                 8:'Straight flush'}

    #Constructor.
    def __init__(self,clist):
        self.clist=clist

    #Function returns all the elements of 5 hand
    def __repr__(self):
        for i1 in self.clist:
            return i1

    #Function myrank().
    def myrank():
        maxvalue = max(mydealhand())
        minvalue = min(mydealhand())
        if len(self.clist) < 5:
            print ("length isn't five")
        myrank = 0
        for i1 in clist:
            if i1.a.myvalue in myvalues:
                if len(i1.a.mydealhand()) == 5:
                    return myranking[1]
                elif len(i1.a.mydealhand()) == 3:
                    if not 3 in i1.a.mydealhand():
                        return myranking[2]
                    else:
                         return myranking[3]
                elif len(i1.a.mydealhand()) == 2:
                    if 2 in len(i1.a.mydealhand()):
                        return myranking[6]
                    else:
                        return myranking[7]
                elif len(i1.a.mydealcard().mysuit) ==1 and maxvalue - minvalue == 4 :
                    return myranking[4]
                elif len(set(i1.a.mydealcard().mysuit)) == 1:
                    return myranking[5]
                else:
                    return myranking[8]

#Main().
def main():
    myobj = MyCardDeck()
    myranking={0:'High card',
                 1:'One pair',
                 2:'Two pair',
                 3:'Three of a kind',
                 4:'Straight',
                 5:'Flush',
                 6:'Full house',
                 7:'Four of a kind',
                 8:'Straight flush'}        
    for i1 in range(100,000):
            print (myranking[i1],":",max(myobj.mydealhand().myrank()))

if __name__ == '__main__':
    main()