讨厌的代码...但无论如何请批评它:-) [英] Nasty code...but please critique it anyway :-)

查看:52
本文介绍了讨厌的代码...但无论如何请批评它:-)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我前一段时间发布了一条消息,要求提供项目建议,并决定使用创建冒险游戏的想法来获得
(尽管它从来没有

打算成为一个正确的游戏,而不是写作和学习的借口。

C ++)。


长话短说,我写了一大块,但意识到

它不是很好。好吧,这是我的第一个正确的 C ++程序,所以'b
没什么大不了的,但是我不想浪费更多的时间来处理一个程序,它应该从头开始重写
(我想开始阅读''加速C ++''

而不是)。什么*会有趣的是听到其他人

想到(可编辑但不正常工作)代码。


这是一个很长的程序,遍布很多文件,所以我没有在这里发布它们。

URL是
http://www.mstrorm.free-online.co.uk/

是的,我知道代码并没有特别好的布局或评论 - 但是我

希望它足够清楚。


该程序的设计是一个控制器class控制

事件的流程并监督所有事情。基类''名词''包括常见行为

并且扩展为给我们游戏位置,物品(即物理对象),

Beings(即游戏角色,进一步子类化为Player对象,

代表人类玩家)并退出。

给出一个命令如

吃巧克力



北上

控制器找到代表巧克力的Noun-subclass-object(一个

项目)或北方(一个退出) - 该对象然后执行动作吃。或北

。假设Noun-subclass支持该动作,它可以适当地响应
(例如,具有id ==" north"的Exit对象将更新

玩家的位置在其go方法中,无论出口指向何处)。

请注意,动作方法(或更具体地说,函数)是静态的

函数中保存的函数指针。我不喜欢初始化函数指针的向量

的方式,但它似乎是当时最好的方式。


批评大规模设计,或基本编程同样有用。我认为两者都有很多批评,因为你可以从我留下的评论中看到

(虽然我喜欢将名词子类化为

不同的对象类型)。


无论如何,反馈将不胜感激....


-


Michael Strorm
ms*****@yahoo.co.uk

Hi!
I posted a message a while back asking for project suggestions, and decided
to go with the idea of creating an adventure game (although it was never
intended to be a ''proper'' game, rather an excuse to write- and learn- some
C++).

To cut a long story short, I wrote a fair chunk of it, but realised that
it''s... not very good. Okay, it''s my first "proper" C++ program, so that''s
no big deal, but I don''t want to waste more time working on a program that
should be rewritten from scratch (I want to start reading ''Accelerated C++''
instead). What *would* be interesting would be to hear what other people
think about the (compilable, but not properly working) code.

It''s a long program, spread over many files, so I haven''t posted them here.
The URL is
http://www.mstrorm.free-online.co.uk/
Yes, I know the code isn''t particularly well laid out or commented- but I
hope it''s clear enough.

The design of the program is that a Controller class controls the flow of
events and oversees everything. A base-class ''Noun'' includes common behavior
and is extended to give us game locations, items (i.e. physical objects),
Beings (i.e. game characters, further subclassed for the Player object which
represents the human player) and exits.
Given a command such as
eat chocolate
or
go north
the controller finds the Noun-subclass-object representing chocolate (an
Item) or north (an Exit)- that object then does the action "eat" or "north"
respectively. Assuming the Noun-subclass supports that action, it can
respond appropriately (e.g. the Exit object with id=="north" would update
the player''s location in its "go" method to wherever the exit pointed to).
Note that action methods (or, more specifically, functions) are static
functions held in a vector of pointers. I don''t like the way that the vector
of function-pointers is initialized, but it seemed the best way at the time.

Criticisms of the large-scale design, or basic programming would be equally
useful. I think there''s plenty to criticize about both, as you can see from
the comments I left in (though I like the subclassing of Nouns into
different object types).

Anyway, feedback would be appreciated....

--

Michael Strorm
ms*****@yahoo.co.uk

推荐答案

2003年10月25日星期六15:38:05 +0100,Michael Strorm写道:
On Sat, 25 Oct 2003 15:38:05 +0100, Michael Strorm wrote:
这是一个很长的程序,遍布很多文件,所以我没有在这里发布。
网址是
http://www.mstrorm.free-online.co.uk/
是的,我知道代码并没有特别好的布局或评论 - 但我希望它足够清楚。
It''s a long program, spread over many files, so I haven''t posted them here.
The URL is
http://www.mstrorm.free-online.co.uk/
Yes, I know the code isn''t particularly well laid out or commented- but I
hope it''s clear enough.




更好地创建一个zip和/或tarball,这样下载起来会容易得多。


HTH,

M4



Better create a zip and/or tarball, that''s much easier to download.

HTH,
M4


文章< 2X **************** @ wards .force9.net>, ms*****@yahoo.co.uk

说...


[...]
In article <2X****************@wards.force9.net>, ms*****@yahoo.co.uk
says...

[ ... ]
该程序的设计是一个Controller类控制流程事件和监督一切。


IMO,你已经在你的Controller课程中投入了太多 - 搜索

Google(在新闻组中)的God class应该看一些关于如何将控制权去中心化的有趣读物。

基类名词包含常见的行为
扩展为我们提供游戏位置,物品(即物理对象),生物(即游戏角色,进一步为玩家对象分类,代表人类玩家)并退出。


IMO,Noun是如此普遍以至于没什么用处。通过

Noun.hpp,它有(例如)getLocation和setLocation - 但是

查看Locn.hpp,一个Locn _is_ a Noun。这对我来说并没有太多的感觉。

给出一个像
这样的命令吃巧克力

去控制器找到代表巧克力(
项目)或北方(退出)的名词 - 子类 - 对象 - 然后该对象执行吃操作。分别是北或北。假设Noun-subclass支持该动作,它可以适当地响应(例如,具有id ==north的Exit对象将在其go方法中更新玩家的位置无论出口指向哪里)。
The design of the program is that a Controller class controls the flow of
events and oversees everything.
IMO, you''ve put too much into your Controller class -- a search on
Google (in the newsgroups) for "God class" should turn up some
interesting reading on how to de-centralize the control a bit.
A base-class ''Noun'' includes common behavior
and is extended to give us game locations, items (i.e. physical objects),
Beings (i.e. game characters, further subclassed for the Player object which
represents the human player) and exits.
IMO, "Noun" is so general as to be of little use. Looking through
Noun.hpp, it has (for example) a getLocation and setLocation -- but
looking at Locn.hpp, a Locn _is_ a Noun. This doesn''t make a lot of
sense to me.
Given a command such as
eat chocolate
or
go north
the controller finds the Noun-subclass-object representing chocolate (an
Item) or north (an Exit)- that object then does the action "eat" or "north"
respectively. Assuming the Noun-subclass supports that action, it can
respond appropriately (e.g. the Exit object with id=="north" would update
the player''s location in its "go" method to wherever the exit pointed to).




我不认为这是一种非常好的结构化方法。一个

的人正在给他们的玩家命令,所以玩家对象应该接受命令。玩家对象应该有一个解析器

对象来弄清楚命令的含义。基于此,它应该检查它的环境并尝试找出执行

命令的方法。


我不认为一个位置来自同一个

基础作为项目是有意义的 - 除非你想(例如)允许
$ b一个玩家携带其他玩家的可能性b / b
携带一把剑,高脚杯等等,它可能没有多大意义

玩家和高脚杯也来自同一个基地。


如果我这样做,在顶层我会有一些位置和一些

众生。每个位置都包含一个出口集合,另一个项目,

和可能的一组属性(例如特定房间可能是黑暗的

所以玩家看不到在里面)。每个地点还有一个

目前在该房间内的生物。


每个人都有一个当前位置,一组物品它正在携带,

可能还有一些其他属性来描述它的力量,天赋,

技能等等。


A玩家是一个拥有命令解析器的存在,所以它可以接受

命令。这些命令直接发送给播放器,播放器然后使用它的

命令解析器来确定它是什么。基于其当前的

环境(技能,力量,环境等),它试图找出

来执行其收到的命令。例如如果你说go

north它查看了当前位置的出口集合,并且b $ b看到是否有一个北出口。如果是这样,移动到

位置(更新其当前位置并更新位置''

它们包含的内容)。


我也不会对播放器可以响应的命令进行硬编码。

相反,播放器数据文件包含告诉什么命令的数据

a player可以响应,以及该命令的效果是什么。

同样,每个项目都有一组响应它的命令(或

启用)和效果那些也是。例如,你可能会确定一个玩家可以飞行,如果他有特定类型的特殊类型的魔法,或者如果他穿着特定的斗篷。这将使一个

玩家能够响应一个上升的命令,否则将不会允许



比较容易添加(例如)你的游戏中的新食物,每个食物对力量,反应速度和

等都有不同的影响。同样添加新武器,怪物等等。


-

后来,

Jerry。


宇宙是自己想象的虚构。



I don''t think this is really a very good way of structuring things. A
person is giving a command to their player, so the player object should
receive the command. The player object should probably have a parser
object to figure out what the command means. Based on that, it should
check its environment and try to figure out a way to carry out the
command.

I don''t think it makes much sense for a location to derive from the same
base as an item -- and unless you want (for example) to allow for the
possibility of a player carrying around another player like it would
carry around a sword, goblet, etc., it probably doesn''t make much sense
for a player and a goblet to derive from the same base either.

If I were doing this, at the top level I''d have some locations and some
beings. Each location contains a collection of exits, another of items,
and possibly a set of attributes (e.g. a particular room might be dark
so the player can''t see in it). Each location will also have a
collection of beings that are within that room at the present time.

Each being has a current location, a collection of items it''s carrying,
probably some other attributes to describe its strength, talents,
skills, etc.

A player is a being that also have a command parser so it can take
orders. The commands go directly to the player, which then uses its
command parser to figure out what it is. Based on its current
environment (skills, strength, environment, etc.) it tries to figure out
a way to carry out the command its received. e.g. if you say "go
north" it looks at the collection of exits in its current location, and
sees whether there''s an exit to the north. If so, moves to that
location (updating its current location and updating the locations''
collections of what they contain).

I also wouldn''t hard-code the commands to which a player can respond.
Instead, the player data file contains data telling about what commands
a player can respond to, and what the effects of that command are.
Likewise, each item would have a set of commands that it responds to (or
enables) and the effects of those as well. Just for example, you might
decide that a player can fly if he has a talent for a particular type of
magic OR if he is wearing a particular cloak. This would enable a
player to respond to a command to go up that would otherwise not be
allowed.

This would make a relatively easy to add (for example) new food items to
your game, each with different effects on strength, reaction speed, and
so on. Likewise with adding new weapons, monsters, etc.

--
Later,
Jerry.

The universe is a figment of its own imagination.


" Martijn Lievaart" < m@rtij.nl.removefromhere.invalid>在留言中写道

news:pa **************************** @ rtij.nl.remove fromhere。无效...
"Martijn Lievaart" <m@rtij.nl.removefromhere.invalid> wrote in message
news:pa****************************@rtij.nl.remove fromhere.invalid...
更好地创建一个zip和/或tarball,这样更容易下载。
Better create a zip and/or tarball, that''s much easier to download.




抱歉 - 应该有提到整个地方有一个拉链

在那里!


-


迈克尔Strorm
ms*****@yahoo.co.uk


这篇关于讨厌的代码...但无论如何请批评它:-)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆