【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 ...
随机推荐
- curl 模拟 GET\POST 请求,以及 curl post 上传文件
curl GET 请求 curl命令 + 请求接口的地址. curl localhost:9999/api/daizhige/article 如上,我们就可以请求到我们的数据了,如果想看到详细的请求信 ...
- C++设计模式——桥接模式
问题描述 现在要去画一个图形,图形有长方形.圆形和扇形等等:而图形又可以加上不同的颜色,然后,我们就可以画出红色的长方形,绿色的长方形:红色的圆形,绿色的圆形等等.而这种图形的形状在变化,图形的颜色也 ...
- ROS-PCL点云库的安装
网上各种说安装点云库还慢,中间出现一些没有依赖库的情况,但在我这里都是很顺利,难道是一开始安装了ROS的原因?不是很清楚,暂且将过程写下来. 一,下载PCL $ git clone https://g ...
- VUE中总的逻辑关系和移动端mint-ui的应用接触
1.mint-ui官网:https://mint-ui.github.io/#!/zh-cn 可以点击开始使用,里边有详细的讲解.安装mint-ui: 官网是: 但是应用没有装成功,不知为何,可能是我 ...
- 【自然语言处理】LDA
飞机票 飞机票 步骤: 1. 离线求的模型 2. 用模型对新文本预测topic,取topic概率带到阈值(例如0.2)且topN个topic,例如doc1 :topic1:0.5, topic2:0. ...
- 记录一下,PC端vue开发常用框架,已经用过elementUI和iview 接下来尝试另一个Muse-UI 喜欢它的点击效果
官网地址: https://muse-ui.org/#/zh-CN/installation
- 饮冰三年-人工智能-Python-24 Django ORM增删改查
一:首先使用默认的sqlite3创建表 1:现在在models.py中添加表模型 from django.db import models # Create your models here. cla ...
- [iOS11] contentInsetAdjustmentBehavior 问题, push back时, 界面会上下移动.
https://stackoverflow.com/questions/45573829/weird-uitableview-behaviour-in-ios11-cells-scroll-up-wi ...
- [转] 扩展微信小程序框架功能
通过第三方 JavaScript 库,扩展微信小程序框架功能. 扩展微信小程序框架功能(1)——Promise ES6 对 Promise 有了原生的支持,但微信开发者工具更新版本(0.11.1122 ...
- ubuntu chrome 无法从该网站添加应用,拓展程序或脚本
昨天装好ubuntu 18.04 lts版本后,下载了chrome( 版本 68.0.3440.106)和SwitchyOmega,本来计划离线安装,结果提示“无法添加来自此网站的应用.扩展程序和应用 ...