Since init is inherited from CardGame, a new OldMaidGame object contains a new s
ID: 3619983 • Letter: S
Question
Since init is inherited from CardGame, a new
OldMaidGame object contains a new shuffled deck:
class OldMaidGame(CardGame):
def play(self, names):
# remove Queen of Clubs
self.deck.removeCard(Card(0,12))
# make a hand for each player
self.hands = []
for name in names :
self.hands.append(OldMaidHand(name))
# deal the cards
self.deck.deal(self.hands)
print "---------- Cards have been dealt"
self.printHands()
# remove initial matches
matches = self.removeAllMatches()
print "---------- Matches discarded, play begins"
self.printHands()
# play until all 50 cards are matched
turn = 0
numHands = len(self.hands)
while matches < 25:
matches = matches + self.playOneTurn(turn)
turn = (turn + 1) % numHands
print "---------- Game is Over"
self.printHands()
Some of the steps of the game have been separated into methods.
removeAllMatches traverses the list of hands and invokes removeMatches on
each:
class OldMaidGame(CardGame):
...
def removeAllMatches(self):
count = 0
for hand in self.hands:
count = count + hand.removeMatches()
return count
Count is an accumulator that adds up the number of matches in each hand and
returns the total.
Explanation / Answer
class CardGame():
[property (Deck)] _deck as Deck
def constructor():
Deck = Deck()
Deck.Shuffle()