程序设计思想与方法ython讲义(第六至十三章).ppt

程序设计思想与方法ython讲义(第六至十三章).ppt

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
程序设计思想与方法ython讲义(第六至十三章)

def printReport(self): 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 printLine(self, label, wins, shuts, n): template = Player %s: %4d %5.1f%% %11d %s if wins == 0: # Avoid division by zero! shutStr = ----- else: shutStr = %4.1f%% % (float(shuts)/wins*100) print template % (label, wins, float(wins)/n*100, shuts, shutStr) 全局函数 def printIntro(): print This program simulates games of racquetball between two print players called A and B. The ability of each player is print indicated by a probability print the player wins the point when serving. Player A always print has the first serve.\n def getInputs(): 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 Main函数 def main(): printIntro() probA, probB, n = getInputs() # Play the games stats = SimStats() for i in range(n): theGame = RBallGame(probA, probB) theGame.play() stats.update(theGame) stats.printReport() main() raw_input(\nPress Enter to quit) 编程实例:Dice Poker 游戏规则: 玩家开始时有$100. 每轮花$10进行游戏. 先投掷一手5个骰子. 然后有两次机会重掷部分或全部骰子. 最后根据上表结帐. 两对 $5 三同 $8 一对加三同 $12 四同 $15 顺子(1-5或2-6) $20 五同 $30 图形界面要求 显示余额 玩家破产时自动终止,玩家也可选择退出 提示程序状态及用户如何响应的信息 模型-视图设计方法 将复杂程序分解为模型和视图。 模型是程序的核心,体现了任务是如何由计算机来完成的 视图是与用户的交互界面。 分开的好处:对同一模型,可以设计不同的用户界面。例如,命令行界面或图像界面。 模型的设计 面向对象程序设计中首先考虑的是对象的抽取 程序涉及到骰子和钱,是否这两者都要抽取为对象? 骰子和钱都只需要用一个数字表示,不像是一个对象的好的候选者 但游戏感兴趣的是5个骰子,掷5个骰子并分析它们的组合情况 可将5个骰子组成的集合封装成一个类Dice Dice类 保存5个骰子的状态 行为: 构造器:初始化Dice对象集合体 rollAll:对5个骰子赋随机值 roll:对部分骰子赋随机值,其他不变 values:返回骰子当前值 score:返回骰子的得分(金额) 游戏过程的设计 游戏过程也作为一个对象 对应的类为PokerApp PokerA

文档评论(0)

qiwqpu54 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档