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 基本的面向对象分析和设计的更多相关文章

  1. 解析UML的面向对象分析与设计

    经常听到有朋友抱怨,说学了UML不知该怎么用,或者画了UML却觉得没什么作用.其实,就UML本身来说,它只是一种交流工具,它作为一种标准化交流符号,在OOA&D过程中开发人员间甚至开发人员与客 ...

  2. UML和模式应用学习笔记-1(面向对象分析和设计)

    UML和模式应用学习笔记-1(面向对象分析和设计) 而只是对情节的记录:此处的用例场景为:游戏者请求掷骰子.系统展示结果:如果骰子的总点数是7,则游戏者赢得游戏,否则为输 (2)定义领域模型:在领域模 ...

  3. 《UML和模式应用》读书笔记(一)面向对象分析和设计简单示例

    在开始进行对象分析和设计之前,先通过“扔骰子”这个软件(游戏者扔两个骰子,如果总是是7,则赢,否则输),来简单分析下这个过程. 1:用例 需求分析,可能包括人们如何应用的场景或情节,这些都可以被编写成 ...

  4. 面向对象分析与设计—OOD部分

    第三部分 面向对象设计 3.1 面向对象设计(OOD)的定义? 在面向对象分析阶段,已经针对用户需求建立起用面向对象概念描述的系统分析模型.在设计阶段,要考虑为实现系统而采用的计算机设备.操作系统.网 ...

  5. .NET应用架构设计—面向对象分析与设计四色原型模式(彩色建模、领域无关模型)(概念版)

    阅读目录: 1.背景介绍 2.问自己,UML对你来说有意义吗?它帮助过你对系统进行分析.建模吗? 3.一直以来其实我们被一个缝隙隔开了,使我们对OOAD遥不可及 4.四色原型模式填补这个历史缝隙,让我 ...

  6. 面向对象分析与设计—OOA部分

    第二部分 面向对象分析 2.1 面向对象分析(OOA)的定义? OOA——面向对象的分析,就是运用面向对象方法进行系统分析,对问题域(问题所涉及的范围)和系统责任(所开发的系统应具备的职能)进行分析与 ...

  7. python基础-第七篇-7.2面向对象(进阶篇)

    进入到今天的探索前,我先对上节内容进行一下回顾: 面向对象是一种编程方式,此编程方式的实现是基于对类和对象的使用 类是一个模板,模板中包装了多个函数可供使用 对象是基于类创建的,实例用于调用被包装在类 ...

  8. python基础之递归,声明式编程,面向对象(一)

    在函数内部,可以调用其他函数,如果一个函数在内部调用自身本身,这个函数就是递归函数.递归效率低,需要在进入下一次递归时保留当前的状态,解决方法是尾递归,即在函数的最后一步(而非最后一行)调用自己,但是 ...

  9. python 基础之第十一天(面向对象)

    #############面向对象##################### 类: In [1]: class MyClass(object): ##用class定义一个类 ...: def psta ...

随机推荐

  1. Centos7 安装 jdk 1.8

    Centos7 安装 jdk 1.8 1.下载安装包 链接: https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloa ...

  2. IKanalyzer分词器分词并且统计词频

    <dependency> <groupId>com.janeluo</groupId> <artifactId>ikanalyzer</artif ...

  3. 2018-2019-1 20165234 实现mypwd

    实现mypwd(选做,加分) 1 学习pwd命令 2 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 实现mypwd 4 测试mypwd 提交过程博客的链接

  4. SQL Server with ties 语句

    With ties 语句是与top.order by 语句联合使用的语句:我们在实际查询过程中会遇到这样的情况,比如查询考试为前三名的学生信息,发现有并列第三的情况,如果我们只是top 3 发现并列第 ...

  5. java 利用jsoup 爬取知乎首页问题

    今天学了下java的爬虫,首先要下载jsoup的包,然后导入,导入过程:首先右击工程:Build Path ->configure Build Path,再点击Add External JARS ...

  6. github密钥

    官网英文资料:https://help.github.com/articles/connecting-to-github-with-ssh/ 1.生成SSH keys文件id_rsa.pub ssh- ...

  7. CentOS7 nginx启动脚本

    vi /lib/systemd/system/nginx.service [Unit] Description=nginx After=network.target [Service] Type=fo ...

  8. UOJ#185. 【ZJOI2016】小星星 容斥原理 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ185.html 题解 首先暴力DP是 $O(3^nn^3)$ 的,大家都会. 我们换个方向考虑. 假设我们 ...

  9. mybatis代码生成器——MyBatis Generator

    1.maven依赖 a.加入依赖 <!-- mybatis生成工具 --> <dependency> <groupId>org.mybatis.generator& ...

  10. POJ 1985 Cow Marathon (模板题)(树的直径)

    <题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...