LearnPython3theHardWay__Excercise 43 Basic Object-Oriented Analysis and Design

**ex40- 44都是类的知识 **

我们讲讲用python编程,特别是面向对象编程的一些基本步骤:

  1. 把要解决的事情写下或画出来
  2. 把解决事情的关键或步骤列举出来
  3. 为这些关键点创建关系图
  4. 创建类和测试用的例子来运行类
  5. 重复并完善代码

可以看出,这个步骤是自上而下的。下面通过实例教学,有兴趣的可以跟着书本一步步完成一个游戏

The Analysis of a Simple Game Engine

我想做一个游戏,叫““Gothons from Planet Percal #25”,一款小型太空探险游戏

1、 把要解决的事情写下或画出来

这个游戏是这样子的:“外星人入侵了一艘宇宙飞船,我们的英雄必须穿过一个迷宫般的房间打败他们,这样他才能进入一个逃生舱逃到下面的星球。游戏有点像密室逃脱,通过文本与玩家互动,有各种各样的死亡方式。进入一个房间会有房间的描述,并根据互动进入下一个房间”
接下来,我要描述各种场景:
Death —— 玩家死亡
Central Corridor —— 这是起始点 ,有一个gothon 站在那里,玩家需要说一个笑话打败它游戏才能继续运行
Laser Weapon Armory —— 一个武器库,玩家在这里可以拿到终极武器,中子弹,在逃后前炸毁飞船。但有密码需要破译
The Bridge —— 这是玩家放炸弹时与gothon战斗的场景
Escape Pod —— 逃生舱,只有玩家进入正确的逃生舱时

2、 把解决事情的关键或步骤列举出来
现在我有足够的信息可以创建实例和他们的类,首先创建列表:

  • Alien
  • player
  • ship
  • maze
  • room
  • scene
  • gothon
  • escape pod
  • planet
  • map
  • engine
  • death
  • central corridor
  • laser weapon armory
  • the bridge

3、为这些关键点创建关系图

4、创建类和测试用的例子来运行类

class Scene(object):

    def enter(self):
        pass


class Engine(object):

    def __init__(self,scene_map):
        pass

    def play(self):
        pass

class Death(Scene):

    def enter(self):
        pass

class CentralCorridor(Scene):

    def enter(self):
        pass

class LaserWeaponArmory(Scene):

    def enter(self):
        pass

class TheBridge(Scene):

    def enter(self):
        pass

class EscapePod(Scene):

    def enter(self):
        pass


class Map(object):

    def __init__(self,start_scene):
        pass

    def next_scene(self,scene_name):
        pass

    def opening_scene(self):
        pass


a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()

5、 重复和完善代码

回顾整个代码,确保代码照顾了方方面面,有需要就回到第2步,第3步,再想解决方案,不停地重复,不停的完善。


这个编码过程,是典型的自上而下的。先构思整体,再慢慢完善局部。当然也可以自下而上,把局部都弄好了,再组合成整体。这需要良好的基础。


接下来作者上代码了,我就不抄了

To plot C versus S for 0 ≤ S ≤ 200, we can use the following MatLab code: ```matlab E = 100; % Exercise price T = 1; % Expiry date r = 0.05; % Risk-free interest rate sigma = 0.3; % Volatility S = linspace(0, 200, 1000); C = zeros(size(S)); for i = 1:numel(S) C(i) = EuropeanCall(S(i), E, r, sigma, T); end plot(S, C); xlabel('S'); ylabel('C'); title('European Call Option Price'); ``` This code first defines the parameters of the option, and then generates a range of values for S between 0 and 200 using the `linspace` function. For each value of S, the code calculates the option price using the `EuropeanCall` function (which can be obtained from various sources). Finally, the code plots the option price as a function of S using the `plot` function. To plot the value of C(S, t) at different values of t between t = 0 and t = T, we can modify the above code as follows: ```matlab E = 100; % Exercise price T = 1; % Expiry date r = 0.05; % Risk-free interest rate sigma = 0.3; % Volatility S = linspace(0, 200, 1000); t = linspace(0, T, 1000); C = zeros(numel(S), numel(t)); for i = 1:numel(S) for j = 1:numel(t) C(i, j) = EuropeanCall(S(i), E, r, sigma, T - t(j)); end end surf(S, t, C); xlabel('S'); ylabel('t'); zlabel('C'); title('European Call Option Price'); ``` This code generates a grid of values for S and t using `linspace`, and then calculates the option price for each combination of S and t using a nested loop. The option price is stored in a matrix `C`, and is plotted as a surface using the `surf` function. As t approaches T, the option price converges to the intrinsic value of the option, which is max(S - E, 0) for a call option. This is because as the expiry date approaches, the option has less time to move in the money, and its time value decreases.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值