Contents
- counter.py
- deck.py
counter.py 1/2
[top][prev][next]
# By CSCI111
class Counter:
"""Represents a counter that has a fixed range. It starts at some low value
and increments by 1. When it reaches the high value, it wraps back around to
the minimum value. """
def __init__(self, start, stop, currValue):
self.start = start
self.stop = stop
self.currValue = currValue
def __str__(self):
stringRep = "Current value: " + str(self.currValue)
return stringRep
def increment(self, incValue = 1):
"""Increment the counter by the given amount, wrapping around to low
again, if necessary. Returns number of times had to wrap around."""
# Not the most efficient solution, but it's easy to see that it is
# correct.
num_wraps = 0
for x in xrange(incValue):
if self.currValue < self.stop:
self.currValue += 1
else:
self.currValue = self.start
num_wraps += 1
return num_wraps
def getCurrentValue(self):
'Returns the current value of this Counter object'
return self.currValue
def getUpperLimit(self):
'Returns the upper limit of this Counter object'
return self.stop
def getLowerLimit(self):
'Returns the lower limit of this Counter object'
return self.start
def setValue(self, newValue):
"""
precondition: newValue is an integer between the lower and upper
limit, inclusive.
Sets the Counter's current value, if the newValue is within the valid
range. """
if newValue <= self.stop and newValue >= self.start:
self.currValue = newValue
else:
# Error message
print newValue, "is not within valid range: ", self.start, self.stop
def main():
counter = Counter(1, 12, 2)
print counter
for x in xrange(12):
counter.increment(13)
print counter
main()
deck.py 2/2
[top][prev][next]
from card import *
from random import shuffle
class Deck:
""" A class to represent a deck of playing cards."""
def __init__(self):
# the instance variable that represents the cards
self.cards = []
# executes 4 times (once for each suit)
for suit in ["clubs","hearts","diamonds","spades"]:
# executes 13 times (once for each rank)
for rank in range(2,15):
# the body of this loop gets executed a total of 52
# times
self.cards.append(Card(rank, suit))
def __str__(self):
"Returns a string representation of the cards in the deck"
# accumulate the cards in the deck in a string
result = ""
for c in self.cards:
# Either of the following two lines do the same thing,
# have the same result:
#result += c.__str__() + "\n"
result += str(c) + "\n"
return result
# TODO: Complete remaining functionality
def shuffle(self):
"Shuffle the cards that are currently in the deck"
shuffle(self.cards) # from random module
def numCardsRemaining(self):
return len(self.cards)
def draw(self):
cardDrawn = self.cards.pop(0)
return cardDrawn
def deal(self, numCards):
hand =[]
for x in xrange( numCards ):
card = self.draw()
hand.append(card)
return hand
def main():
d = Deck()
print "*"*8, "Before shuffling:", "*"*8
print d
d.shuffle()
print "*"*8, "After shuffling:", "*"*8
print d
print "Number of cards remaining", d.numCardsRemaining()
print "Draw the top card:", d.draw()
print "Number of cards remaining", d.numCardsRemaining()
hand = d.deal(5)
print "My hand contains:"
for card in hand:
print card
# Since I am probably going to import this script into another script,
# I only want to call main() when it's *not* imported
if __name__ == "__main__":
main()
Generated by GNU enscript 1.6.4.