Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Python. Bridge card game. from Hand import * from Deck import * def main(): h1 = Hand(south) h2 = Hand(north) d = Deck() d.shuffle() #
Python. Bridge card game.
from Hand import * from Deck import *
def main():
h1 = Hand("south") h2 = Hand("north")
d = Deck() d.shuffle()
# dealing cards in order for i in range(13): h1.add(d.deal()) h2.add(d.deal())
# showing the cards h1.dump() h2.dump()
# sorting hards in hand "south" and displaying them h1.sort() h1.dump()
# sorting hards in hand "north" and displaying them h2.sort() h2.dump()
main()
# Deck.py from random import randrange from Card import Card class Deck(object): #------------------------------------------------------------ def __init__(self): """post: Creates a 52 card deck in standard order""" cards = [] for suit in Card.SUITS: for rank in Card.RANKS: cards.append(Card(rank,suit)) self.cards = cards #------------------------------------------------------------ def size(self): """Cards left post: Returns the number of cards in self""" return len(self.cards) #------------------------------------------------------------ def deal(self): """Deal a single card pre: self.size() > 0 post: Returns the next card, and removes it from self.card if the deck is not empty, otherwise returns False""" if self.size() > 0: return self.cards.pop() else: return False #------------------------------------------------------------ def shuffle(self): """Shuffles the deck post: randomizes the order of cards in self""" n = self.size() cards = self.cards for i,card in enumerate(cards): pos = randrange(i,n) cards[i] = cards[pos] cards[pos] = card
# Hand.py class Hand(object): """A labeled collection of cards that can be sorted""" #------------------------------------------------------------ def __init__(self, label=""): """Create an empty collection with the given label.""" self.label = label self.cards = [] #------------------------------------------------------------ def add(self, card): """ Add card to the hand """ self.cards.append(card) #------------------------------------------------------------ def sort(self): """ Arrange the cards in descending bridge order.""" self.cards.sort() self.cards.reverse() #------------------------------------------------------------ def dump(self): """ Print out contents of the Hand.""" print(self.label + "'s Cards:") for c in self.cards: print(" ", c)
class Card(object): '''A simple playing card. A Card is characterized by two components: rank: an integer value in the range 2-14, inclusive (Two-Ace) suit: a character in 'cdhs' for clubs, diamonds, hearts, and spades.''' #------------------------------------------------------------ SUITS = 'cdhs' SUIT_NAMES = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] RANKS = list(range(2,15)) RANK_NAMES = ['Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace'] #------------------------------------------------------------ def __init__(self, rank, suit): '''Constructor pre: rank in range(1,14) and suit in 'cdhs' post: self has the given rank and suit''' self.rank_num = rank self.suit_char = suit #------------------------------------------------------------ def suit(self): '''Card suit post: Returns the suit of self as a single character''' return self.suit_char #------------------------------------------------------------ def rank(self): '''Card rank post: Returns the rank of self as an int''' return self.rank_num #------------------------------------------------------------ def suitName(self): '''Card suit name post: Returns one of ('clubs', 'diamonds', 'hearts', 'spades') corrresponding to self's suit.''' index = self.SUITS.index(self.suit_char) return self.SUIT_NAMES[index] #------------------------------------------------------------ def rankName(self): '''Card rank name post: Returns one of ('ace', 'two', 'three', ..., 'king') corresponding to self's rank.''' index = self.RANKS.index(self.rank_num) return self.RANK_NAMES[index] #------------------------------------------------------------ def __str__(self): '''String representation post: Returns string representing self, e.g. 'Ace of Spades' ''' return self.rankName() + ' of ' + self.suitName() #------------------------------------------------------------ def __eq__(self, other): '''post returns True if two cards are equal, False otherwise''' return (self.suit_char == other.suit_char and self.rank_num == other.rank_num) #------------------------------------------------------------ def __lt__(self, other): '''post: returns True if self The game is played by two players A deck of cards is put face down and a random card is drawn from the deck both players see the trump suit and the card is put back into a random place into the deck. At each turn: players grab one card each from the top of the deck and put them face up on the table. If both cards have the same suit, then the player with the highest ranked card wins this turn, takes this pair of cards and places them into his/her pile. If cards have different suits, then the person with a trump card wins his turn, takes this pair of cards and places them into his/her pile. If the cards have different suits and none of them is a trump, then it is a tie, the cards are discarded The game ends when there is no more cards in the deck. The person with more cards in his/her pile wins Your program must print each turn, by showing the both players cards and telling who won this turn At the end, it should say who won the game and how many cards each player has
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