【问题标题】:OO design of a game : moving player between rooms游戏的面向对象设计:在房间之间移动玩家
【发布时间】:2014-10-20 15:21:52
【问题描述】:

我是 OO 设计的新手,在设计游戏时遇到了一些问题:

游戏有一系列房间。 每个房间都有一组玩家和一个库存。 每个 Inventory 都有一个 Items 集合。

所以目前,房间可以控制玩家。但是通过这种设计,我不确定是否可以在房间之间移动玩家,这正是我想要做的。

然后我想在 Player 类中有一个Room currentRoom,但我觉得我也会遇到这个问题。另外,玩家没有房间,所以这看起来不太OO。

关于如何设计我的游戏的任何提示?谢谢!

【问题讨论】:

  • 你应该有一个 Room 类,并根据需要从 Room 类创建尽可能多的对象,即。房间的数量。然后,您应该让 Player 对象根据需要创建任意数量。然后在您的房间对象中,您应该有某种列表 当前在该房间中的玩家...
  • 这个之前已经建模过了。这是一个非常古老的在线游戏平台如何做到的示例:hayseed.net/MOO/manuals/ProgrammersManual.html#SEC6
  • 当玩家从他们所在的房间移动到另一个房间时,将玩家从他们所在的房间中移除并将他们添加到新房间。
  • 区分玩家作为智能代理(人类、计算机)和玩家作为棋子。
  • 玩家可能没有房间,但他确实有一个位置,在这种情况下,那个位置就是一个房间,所以currentRoom 并不是不合适的。这将允许您将玩家放在棋盘级别的列表中,然后您可以检查该列表以获取房间的当前居住者。

标签: java oop


【解决方案1】:

+1:“带有当前项目的集合”软件设计模式非常普遍。


你的游戏中有几个房间。

The `Game` has a collection of `Rooms`.

然后,Game“管理”或“拥有”Rooms

当一个对象“管理”或“拥有”其他对象时,负责分配和释放(内存中对象的“创建”和“销毁”)。


但是:

Each `room` has a collection of `Players` and an `Inventory`.

等等。你忘了:

The `Game` has a collection of `Players`.

还有:

Each `room` has a collection of `Players`.

等等,Game 也有相同的 Players 集合。

小心“有”这个词。

在 O.O.P. 中,许多对象可以与其他对象相关联, 但是,只有一个对象可以是另一个对象的“管理者”(同时“相关”)。

Room (s) 和 Game 两者都有一些关系, 或与Player (s) 关联,但只有一个对象, 可以是他们的“经理”。

因为Player 始终是Game 的一部分,但是, 可以留下一个当前的Room...

...那么房间可以引用Players的同一个集合, 比Game,但不“管理”它们。


所以,我们把前面的声明改成:

The `Game` manages a collection of `Players`.
Each `Room` relates to a collection of `Players`.

现在:

Each `room` has (a collection of | ) an `Inventory`.

然后,每个Room“管理”或“拥有”Inventory

让我们用Items替换Inventory这个词:

Each `room` has a collection of `item`s, called an `Inventory`

所以:

The `Game` manages a collection of `Rooms`.
The `Game` manages a collection of `Players`.
Each `Room` relates to a collection of `Players`.
Each `Player` relates to a single, current `Room`.

问题在于“有”这个词。有时意味着联想, “管理”/“拥有”对象,有时意味着“关联但不管理”一个对象。


最后:

Each `Room` manages to a collection of `Items`, also called `Inventory`.

但是,如果一个Player可以带着Items,和他/她一起, 并更改Room(s),并且每个Player,可能会删除一个Item, 进入Room,就像放下枪,拿起斧头一样。

然后事情会变得有点混乱。

假设item“可以位于”,而不是room“具有”。

因此,每个player 都可以与Items 的集合相关,并且, 每个room 可以与Items 的集合相关, 并且,ItemsGame“管理”。

Each `Player` can relate to a collection of `Item` (s).
Each `Room` can relate to a collection of `Item` (s).
Each `Item`, maybe related to a `Room`,
can be located in a `Room`, but, not always.
Each `Item`, maybe related to a `Player`, but, not always.
Each `Item` is part of an universe called the `Game`.
So, the `Game` "manages" all the `Item` (s).

干杯。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多