Python language : Starting with your solution to the previous task (or re-typing it all if you
Question:
Python language :
Starting with your solution to the previous task (or re-typing it all if you are keen!), extend the Deck class by adding the shuffleONE(self, no) method.
Shuffling is a procedure used to randomize a deck of playing cards to provide an element of chance in card games. We are going to implement a number of methods to simulate the card shuffling.
The shuffleONE(self, no) takes an integer as a parameter and performs a shuffling of the playing cards. At the beginning, we have the initialized "Deck" as below.
Deck: [AS ...... KD]
We pop out a number "no" of cards and push them into an empty stack s1. Then we pop out the remaining cards and push them into another stack s2. For example, shuffleONE(12) is called, s1 and s2 contain the following cards:
s1: [KD ...... JS] s2: [TD ...... AS]
Hence, we pop out one card from "s1" and push it back into "Deck". We next pop out one card from "s2" and push it back into "Deck". Repeat these two steps until either "s1" or "s2" is empty. Finally, we pop out the remaining cards from either "s1" or "s2" and push them into "Deck" one by one. The result is shown as below.
Deck: [JS AS JH AH ...... KC 3C KD 3D 4S 4H ...... TC TD]
Write the Stack class, PokerCard class, and Deck class in the answer box below. Note - keep a copy of your solution to this task because you will be extending it step by step in subsequent tasks.
For example:
Test | Result |
---|---|
Gdeck = Deck() Gdeck.shuffleONE(0) print(Gdeck) | AS AH AC AD 2S 2H 2C 2D 3S 3H 3C 3D 4S 4H 4C 4D 5S 5H 5C 5D 6S 6H 6C 6D 7S 7H 7C 7D 8S 8H 8C 8D 9S 9H 9C 9D TS TH TC TD JS JH JC JD QS QH QC QD KS KH KC KD |
Gdeck = Deck() Gdeck.shuffleONE(12) print(Gdeck) | JS AS JH AH JC AC JD AD QS 2S QH 2H QC 2C QD 2D KS 3S KH 3H KC 3C KD 3D 4S 4H 4C 4D 5S 5H 5C 5D 6S 6H 6C 6D 7S 7H 7C 7D 8S 8H 8C 8D 9S 9H 9C 9D TS TH TC TD |
class PokerCard:
def __init__(self,item):
self.card = item
self.value = self.__evaluate(item)
def __evaluate(self,item):
try:
val = int(item[0])
return val
except ValueError:
val = item[0]
if val.upper() == 'A':
return 11
elif val.upper() == 'T':
return 10
elif val.upper() == 'J':
return 12
elif val.upper() == 'Q':
return 13
elif val.upper() == 'K':
return 14
else:
return None
def __str__(self):
return self.card
class Stack:
def __init__(self):
self.stack = []
def push(self,item):
self.stack.append(item)
def pop(self):
if(self.size() > 0):
return self.stack.pop()
else:
return None
def size(self):
return len(self.stack)
class Deck:
def __init__(self):
self.cards = Stack()
cardlist = ['AS', 'AH', 'AC', 'AD', '2S', '2H', '2C', '2D', '3S', '3H', '3C', '3D', '4S',
'4H', '4C', '4D', '5S', '5H', '5C', '5D', '6S', '6H', '6C', '6D', '7S', '7H','7C',
'7D', '8S', '8H', '8C', '8D', '9S', '9H', '9C', '9D', 'TS', 'TH', 'TC', 'TD', 'JS',
'JH', 'JC', 'JD', 'QS', 'QH', 'QC', 'QD', 'KS', 'KH', 'KC', 'KD']
for i in range(52):
self.cards.push(cardlist[i])
def __str__(self):
rec_in_line = self.cards.size()/4
deckStr = ''
reverse_deck = Stack()
for i in range(self.cards.size()):
reverse_deck.push(self.cards.pop())
while(reverse_deck.size()>0):
for j in range(rec_in_line):
item = reverse_deck.pop()
deckStr = deckStr + item + ' '
self.cards.push(item)
deckStr = deckStr + ''
return deckStr
def shuffleOne(self, no): ???