Python Programming: An Introduction To Computer Science: Object-Oriented Design
Python Programming: An Introduction To Computer Science: Object-Oriented Design
An Introduction To
Computer Science
Chapter 12
Object-Oriented Design
class Player:
# A Player keeps track of service probability and score
def winsServe(self):
# RETURNS true with probability self.prob
return random() <= self.prob
def incScore(self):
# Add a point to this player's score
self.score = self.score + 1
def getScore(self):
# RETURN this player's current score
return self.score
Python Programming, 2/e 56
The Complete Program
class RBallGame:
# A RBallGame represents a game in progress. A game as two players
# and keeps track of which one is currently serving.
def play(self):
# Play the game to completion
while not self.isOver():
if self.server.winsServe():
self.server.incScore()
else:
self.changeServer()
def isOver(self):
# RETURNS game is finished (i.e. one of the players has won).
a,b = self.getScores()
return a == 15 or b == 15 or \
(a == 7 and b == 0) or (b==7 and a == 0)
def changeServer(self):
# Switch which player is serving
if self.server == self.playerA:
self.server = self.playerB
else:
self.server = self.playerA
def getScores(self):
# RETURNS the current scores of player A and player B
return self.playerA.getScore(), self.playerB.getScore()
def __init__(self):
# Create a new accumulator for a series of games
self.winsA = 0
self.winsB = 0
self.shutsA = 0
self.shutsB = 0
def printReport(self):
# Print a nicely formatted report
n = self.winsA + self.winsB
print "Summary of", n , "games:"
print
print " wins (% total) shutouts (% wins) "
print "--------------------------------------------"
self.printLine("A", self.winsA, self.shutsA, n)
self.printLine("B", self.winsB, self.shutsB, n)
def getInputs():
# Returns the three simulation parameters
a = input("What is the prob. player A wins a serve? ")
b = input("What is the prob. player B wins a serve? ")
n = input("How many games to simulate? ")
return a, b, n
def main():
printIntro()
Two Pairs 5
Three of a Kind 8
Full House 12
(A Pair and a Three of a Kind)
Four of a Kind 15
Five of a Kind 30
Python Programming, 2/e 63
Program Specification
Since we want a nice graphical interface, we
will be interacting with our program through
mouse clicks.
The interface should have:
The current score (amount of money) is constantly applied.
The program automatically terminates if the player goes
broke.
The player may choose to quit at appropriate points during
play.
The interface will present visual cues to indicate what is
going on at any given moment and what the valid user
responses are.
def __init__(self):
self.win = GraphWin("Dice Poker", 600, 400)
self.win.setBackground("green3")
banner = Text(Point(300,30), "Python Poker Parlor")
banner.setSize(24)
banner.setFill("yellow2")
banner.setStyle("bold")
banner.draw(self.win)
self.msg = Text(Point(300,380), "Welcome to the dice table.")
self.msg.setSize(18)
self.msg.draw(self.win)
self.createDice(Point(300,100), 75)
self.buttons = []
self.addDiceButtons(Point(300,170), 75, 30)
b = Button(self.win, Point(300, 230), 400, 40, "Roll Dice")
self.buttons.append(b)
b = Button(self.win, Point(300, 280), 150, 40, "Score")
self.buttons.append(b)
b = Button(self.win, Point(570,375), 40, 30, "Quit")
self.buttons.append(b)
self.money = Text(Point(300,325), "$100")
self.money.setSize(18)
self.money.draw(self.win)