Question
< < < Python language >>> The previous work so far is : class Stack: def __init__(self): self.stack = [] def push(self,item): self.stack.append(item) def pop(self):
<<< Python language >>>
The previous work so far is :
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 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 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 deckS = '' 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() deckS = deckS + item + ' ' self.cards.push(item) deckS = deckS + ' ' return deckS
** Question 1:
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 Stack:
class PokerCard:
class Deck:
**Question 2:
Consider the Player class given as below:
class Player: def __init__(self): self.cards = Stack() def points_eval(self): #complete this def __str__(self): #complete this
Complete the points_eval(self) method. This is a simplified version. Points should be evaluated based on the cards in the player. Note: we always treat Ace as 11 in this question. We use a "Stack" to store poker cards owned by the player.
You should also complete the __str__(self) method. The __str__ method returns a string representation of the cards owned by the player.
Write the Stack class, PokerCard class, and Player 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() P1 = Player() P1.cards.push(Gdeck.cards.pop()) P1.cards.push(Gdeck.cards.pop()) print(P1) print(P1.points_eval()) | ['KD', 'KC'] 20 |
Gdeck = Deck() Gdeck.shuffleTHREE(12) P1 = Player() P1.cards.push(Gdeck.cards.pop()) P1.cards.push(Gdeck.cards.pop()) print(P1) print(P1.points_eval()) | ['AS', 'AH'] 22 |
class Stack:
class PokerCard:
class Player:
**Question 3 :
Consider the following code fragment:
endgame = False Gdeck = Deck() Gdeck.shuffleONE(35) Gdeck.shuffleTWO(29) Gdeck.shuffleTHREE(39) P1 = Player() P1.cards.push(Gdeck.cards.pop()) P1.cards.push(Gdeck.cards.pop()) print(P1) print(P1.points_eval()) while (endgame == False): #complete the while loop
Two cards will be popped out from the "Deck" and push into the "Players". The player can choose whether to play with one additional card or stop. The game will end automatically if the total points are greater than 21. "GAME OVER!" should be displayed then. This is a simplified version. We always treat Ace as 11.
Write the Stack class, PokerCard class, Player class, and Deck class in the answer box below.
For example:
Input | Result |
---|---|
n | ['6S', 'QS'] 16 Do you want one more card? (y/n) |
class Stack: class PokerCard: class Deck: class Player: endgame = False Gdeck = Deck() Gdeck.shuffleONE(35) Gdeck.shuffleTWO(29) Gdeck.shuffleTHREE(39) P1 = Player() P1.cards.push(Gdeck.cards.pop()) P1.cards.push(Gdeck.cards.pop()) print(P1) print(P1.points_eval()) while (endgame == False): #complete the while loop
**Question 4:
Advanced Version
Starting with your solution to the previous task (or re-typing it all if you are keen!), complete the points_eval(self)(standard version) method. Points should be evaluated based on the cards in the player. Note: Ace can be counted as 11 or 1. We will always choose the better one that makes the final points become closer to 21 but not bigger than 21. We use ADT "Stack" to implement the cards owned by the player.
You should also complete the __str__(self) method. The __str__ method returns a string representation of the cards owned by the player.
Write the Stack class, PokerCard class, and Player 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() P1 = Player() P1.cards.push(Gdeck.cards.pop()) P1.cards.push(Gdeck.cards.pop()) print(P1) print(P1.points_eval()) | ['KD', 'KC'] 20 |
Gdeck = Deck() Gdeck.shuffleTHREE(12) P1 = Player() P1.cards.push(Gdeck.cards.pop()) P1.cards.push(Gdeck.cards.pop()) print(P1) print(P1.points_eval()) | ['AS', 'AH'] 12 |
class Stack:
class PokerCard:
class Player:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started