【Python基础】lpthw - Exercise 43 基本的面向对象分析和设计
1. A game
from sys import exit
from random import randint
from textwrap import dedent # 使可以使用三引号型的字符串 # 具体实现了场景间的切换
class Engine(object): def __init__(self, scene_map):
self.scene_map = scene_map def play(self):
current_scene = self.scene_map.opening_scene() # 初始场景的类
last_scene = self.scene_map.next_scene('finished') while current_scene != last_scene:
next_scene_name = current_scene.enter() # 返回一个场景的名字
current_scene = self.scene_map.next_scene(next_scene_name) # 确保print出最后一个场景
current_scene.enter() # 各场景共有的父类
class Scene(object): def enter(self):
pass # 具体的每个场景,其中定义了一些说明和交互
class Death(Scene): quips = ["You died.", "Please try again.", "That's not very good."] # 类变量,可以通过类名调用 def enter(self):
print(Death.quips[randint(0,2)])
exit(1) class CentralCorridor(Scene): def enter(self):
print("""
You are met with some strange things.
This is a locked room.
It will be a good idea to leave right away.
You see a Gothon blocking the door.
Try to do something.
""")
action = input('>')
if action == 'shoot!':
return 'death'
elif action == 'tell a joke!':
return 'laser_weapon_armory'
else:
print("Nothing happened.")
return 'central_corridor' class LaserWeaponArmory(Scene): def enter(self):
print("""
This is the laser weapon armory.
Destroy their weapon!
Please enter the code.
""") code = ""
guess = input('>')
guesses = 0 while guess != code and guesses < 5:
print("That's not right!")
guesses += 1
guess = input('Try again >') if guess == code:
print("Bingo! The weapon is destroyed!")
return 'the_bridge'
else:
print("A bell rings and you are locked here to die!")
return 'death' class TheBridge(Scene): def enter(self):
print("A bridge is in front of you and you see a huge bomb. ")
action = input('>') if action == "throw it away":
print("It exploded as you aprroached it. You died.")
return 'death'
elif action == "leave it behind":
print("It seems nothing happened. You can safely pass it.")
return 'escape_pod'
else:
return 'death' class EscapeRod(Scene): def enter(self):
print("""
You finally make it to the escape rod.
There are five ships in front of you.
Which one do you want to take?
""")
num = input('>')
correct_ship = ''
if num == correct_ship:
print("You got on the ship. Everything seems getting on well. You won!")
return 'finished'
else:
print("The ship is not working well. It exploded as take off!. You died!")
return 'death' class Finished(Scene): def enter(self):
print("Congratulations! You successfully escaped from the ship!")
return 'finished' # 定义了各场景的名字,定义了进出场景的方法
class Map(object): scenes = {'central_corridor': CentralCorridor(),
'laser_weapon_armory': LaserWeaponArmory(),
'the_bridge': TheBridge(),
'escape_pod': EscapeRod(),
'death': Death(),
'finished': Finished()
} def __init__(self, start_scene):
self.start_scene = start_scene def next_scene(self, scene_name):
nextScene = Map.scenes.get(scene_name)
return nextScene # 根据传入名称返回一个场景的类 def opening_scene(self):
return self.next_scene(self.start_scene) # 根据出发点名称返回出发点场景的类 a_map = Map("central_corridor")
a_game = Engine(a_map)
a_game.play()
【Python基础】lpthw - Exercise 43 基本的面向对象分析和设计的更多相关文章
- 解析UML的面向对象分析与设计
经常听到有朋友抱怨,说学了UML不知该怎么用,或者画了UML却觉得没什么作用.其实,就UML本身来说,它只是一种交流工具,它作为一种标准化交流符号,在OOA&D过程中开发人员间甚至开发人员与客 ...
- UML和模式应用学习笔记-1(面向对象分析和设计)
UML和模式应用学习笔记-1(面向对象分析和设计) 而只是对情节的记录:此处的用例场景为:游戏者请求掷骰子.系统展示结果:如果骰子的总点数是7,则游戏者赢得游戏,否则为输 (2)定义领域模型:在领域模 ...
- 《UML和模式应用》读书笔记(一)面向对象分析和设计简单示例
在开始进行对象分析和设计之前,先通过“扔骰子”这个软件(游戏者扔两个骰子,如果总是是7,则赢,否则输),来简单分析下这个过程. 1:用例 需求分析,可能包括人们如何应用的场景或情节,这些都可以被编写成 ...
- 面向对象分析与设计—OOD部分
第三部分 面向对象设计 3.1 面向对象设计(OOD)的定义? 在面向对象分析阶段,已经针对用户需求建立起用面向对象概念描述的系统分析模型.在设计阶段,要考虑为实现系统而采用的计算机设备.操作系统.网 ...
- .NET应用架构设计—面向对象分析与设计四色原型模式(彩色建模、领域无关模型)(概念版)
阅读目录: 1.背景介绍 2.问自己,UML对你来说有意义吗?它帮助过你对系统进行分析.建模吗? 3.一直以来其实我们被一个缝隙隔开了,使我们对OOAD遥不可及 4.四色原型模式填补这个历史缝隙,让我 ...
- 面向对象分析与设计—OOA部分
第二部分 面向对象分析 2.1 面向对象分析(OOA)的定义? OOA——面向对象的分析,就是运用面向对象方法进行系统分析,对问题域(问题所涉及的范围)和系统责任(所开发的系统应具备的职能)进行分析与 ...
- python基础-第七篇-7.2面向对象(进阶篇)
进入到今天的探索前,我先对上节内容进行一下回顾: 面向对象是一种编程方式,此编程方式的实现是基于对类和对象的使用 类是一个模板,模板中包装了多个函数可供使用 对象是基于类创建的,实例用于调用被包装在类 ...
- python基础之递归,声明式编程,面向对象(一)
在函数内部,可以调用其他函数,如果一个函数在内部调用自身本身,这个函数就是递归函数.递归效率低,需要在进入下一次递归时保留当前的状态,解决方法是尾递归,即在函数的最后一步(而非最后一行)调用自己,但是 ...
- python 基础之第十一天(面向对象)
#############面向对象##################### 类: In [1]: class MyClass(object): ##用class定义一个类 ...: def psta ...
随机推荐
- 【原创】大叔问题定位分享(25)ambari metrics collector内置standalone hbase启动失败
ambari metrics collector内置hbase目录位于 /usr/lib/ams-hbase 配置位于 /etc/ams-hbase/conf 通过ruby启动 /usr/lib/am ...
- TCP 的那些事儿
TCP是一个巨复杂的协议,因为他要解决很多问题,而这些问题又带出了很多子问题和阴暗面.所以学习TCP本身是个比较痛苦的过程,但对于学习的过程却能让人有很多收获.关于TCP这个协议的细节,我还是推荐你去 ...
- poj 3278 搜索
描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immediate ...
- HDU 2196 Compute --树形dp
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- c++ ignore用法
转自 http://blog.sina.com.cn/s/blog_4b3336c50102v45n.html std::cin.ignore() can be called three diffe ...
- Angular组件——投影
运行时动态改变组件模版的内容.没路由那么复杂,只是一段html,没有业务逻辑. ngContent指令将父组件模版上的任意片段投影到子组件上. 一.简单例子 1.子组件中使用<ng-conten ...
- 单元测试如何覆盖internal的方法
在类的设计中经常会有类或者方法要设置成private或者internal等方式,在使用中这么做无可厚非,但是对单元测试的影响也颇大 对于private方法,那只有做一个副本然后改成internal或p ...
- kvm-virsh管理工具
virsh 可以进入命令行交互界面 Virsh list 显示所有虚拟机实例 #virt-manager & 启动图形界面来创建 Virsh start c1 --con ...
- AtCoder Grand Contest 030 (AGC030) C - Coloring Torus 构造
原文链接https://www.cnblogs.com/zhouzhendong/p/AGC030C.html 题解 才发现当时是被题意杀了. 当时理解的题意是“对于任意的 (i,j) ,颜色 i 和 ...
- 微服务化不同阶段 Kubernetes 的不同玩法
本文由 网易云发布. 作为容器集群管理技术竞争的大赢家,Kubernetes 已经和微服务紧密联系,采用 Kubernetes 的企业往往都开始了微服务架构的探索.然而不同企业不同阶段的微服务实践面 ...