Task In this task, we will write a program test.9.py, which uses classes and objects to deal a hand of cards, score it a
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Task In this task, we will write a program test.9.py, which uses classes and objects to deal a hand of cards, score it a
import random
suits = ('hearts','diamonds','spades','clubs')
ranks =
('Ace','2','3','4','5','6','7','8','9','10','Jack','Queen','King')
class Card:
def __init__(self,rank,suit):
self.rank = rank
self.suit = suit
def getRank(self):
return self.rank
def getSuit(self):
return self.suit
def __str__(self):
return self.rank + " of " +
self.suit
class Deck:
def __init__(self):
self.deck = []
for rank in ranks:
for suit in suits:
card =
Card(rank,suit)
self.deck.append(card)
def shuffle(self):
random.shuffle(self.deck)
def dealCard(self):
return self.deck.pop(0)
def deal(n):
deck = Deck()
deck.shuffle()
cards = []
for i in range(n):
card = deck.dealCard()
cards.append(card)
return cards
def evaluate(hand):
handRank = [card.getRank() for card in hand]
occurence = {}
unqranks = set(handRank)
for rank in unqranks:
occurence[rank] =
handRank.count(rank)
points = 0
for rank in occurence:
if occurence[rank] == 4:
points += 100
elif occurence[rank] == 3:
points += 10
elif occurence[rank] == 2:
points += 1
return points
def main():
n = input("Number of cards: ")
if not n.isdigit():
input("Not a number, Please try
Again!")
return
n = int(n)
if (n>52) or (n<0):
input("Deck size out of bound!")
return
cards = deal(n)
for card in cards:
print("\t"+str(card))
score = evaluate(cards)
print("\t-----------> Score:",score)
main()
Task In this task, we will write a program test.9.py, which uses classes and objects to deal a hand of cards, score it according to the number of pairs, three-of-a-kind, and four-of-a-kind sets, and then show the hand with a graphical interface using a custom widget. Number of cards: 8 Deal Score: 21 6 of spades 6 of hearts ace of hearts ace of clubs jack of diamonds Jack of hearts. 6 of diamonds ace of diamonds Evaluating a hand of cards We consider an imaginary game in which each hand of cards is scored according to the number of pairs, three-of-a-kind, and four-of-a-kind sets it contains: . Four of a kind (c.g. 74 7♥ 7♣ 7❤): +100 points Three of a kind (e.g. 8 8 8): +10 points r(e.g. 94 94): point For example, the following hand of 10 cards: SA 5♣ 5+ 7♥7♦ J♦ AAVAA♦ evaluates as:
For example, the following hand of 10 cards: J♦ evaluates as: 10+1+0+100= 111 Step-by-step implementation: 1. Using the provided classes Card and Deck, write a function deal (n) that creates a randomly shuffled deck and deals a hand of n cards, which are returned as a list. 2. Write a function evaluate (hand), which, given a list of card objects, evaluates it according to the rules described in the previous section and returns the score. (Exercise 6 from Unit 5 can be helpful for implementing this.) 3. Write a text user interface that repeatedly asks the user how many cards should be dealt, creates a hand of the requested size and evaluates it. The program should check that the user input is an integer (use isdigit) and is in the range 0 ≤ns 52. Example: Number of cards: 5 10 of hearts 6 of spades 8 of diamonds. ace of clubs. jack of hearts Number of cards: 7 2 of diamonds 10 of diamonds. 10 of spades 10 of clubs. king of diamonds ace of clubs 9 of diamonds ---> Score: 0 SA 585* A A A A
4. Make a widget CardsFrame that is a specialized version of Frame, which holds a list of buttons with card names on them. Its ___init_____ function should receive a list of Card objects as a parameter, specifying which cards should be shown: 2 of spades ace of hearts. ace of diamonds 2 of hearts queen of hearts You don't need to specify the ['command' ] options for the buttons, thus clicking a button will do nothing. 5. Make a Tkinter interface for the program, using the enhanced Entry and CardsFrame widgets. When the user presses the button 'Deal, a new hand is generated, CardsFrame should be updated (you can destroy the old widget replacing it with a new one), and the score of the new hand should be shown in the corresponding label: Number of cards: S Deal Score: 2 2 of spades ace of hearts ace of diamonds 2 of hearts queen of hearts Number of cards: 12 Score: 23 king of clubs 4 of spades ace of diamonds ace of hearts jack of diamonds. 4 of clubs Deal
queen of hearts Score: 23 king of clubs 4 of spades ace of diamonds ace of hearts jack of diamonds 4 of clubs 5 of spades 5 of diamonds 5 of clubs jack of clubs king of spades king of hearts Score: 200 10 of spades 8 of spades 10 of clubs 5 of clubs 5 of spades 10 of hearts 5 of hearts 10 of diamonds 5 of diamonds. Number of cards: 12 Number of cards: Deal Deal