在OOD大富翁游戏? [英] Monopoly game in OOD?

查看:180
本文介绍了在OOD大富翁游戏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现通过CodingHorror这个有趣的博客帖子:我最喜欢的面试问题 。简而言之,他谈到设计大富翁游戏的面向对象的设计挑战,如何将游戏规则建模的重点。例如,如果一个玩家拥有波罗的海大道,可她的房子补充呢?

I found this interesting blog post via CodingHorror: My Favorite Interview Question. In a nutshell, he talks about the object-oriented design challenges of designing the game of Monopoly, with an emphasis on how to model the rules of the game. For example, "If a player owns Baltic Avenue, can she add a house to it?"

有趣的是,附近的帖子底部,然后他写道:

Interestingly, near the bottom of the post, he then writes:

您大概可以节省大量的面试时间。相反,这一切的喧闹,要求应聘者描述时,他们实际上已经使用了框架之外的策略,访客,和Command模式。)

You can probably save yourself a lot of interview time. Instead of all this hoopla, ask the candidate to describe when they have actually used the Strategy, Visitor, and Command patterns outside of a framework.)

...这可能意味着你可以使用设计模式的游戏规则(见上文)进行建模。有没有人做过呢?专卖设计使用设计模式的游戏?如果是这样,它是怎么工作的?

...which probably means that you can use design patterns to model the rules of the game (see above). Has anybody ever done this? Designed the game of Monopoly using design patterns? If so, how did it work out?

推荐答案

下面是我如何将设计专卖。我已经采取了假设一个动态类型语言,因为这让一切更容易的自由。特别是红宝石

Here's how I would design Monopoly. I've taken the liberty of assuming a dynamically-typed language since that makes everything easier. Ruby specifically.

您有一个简单的游戏对象,这主要是各地的大小40的阵列的包装,再加上一些方便的方法。在游戏对象还跟踪可用房屋数量酒店和机遇与公益金卡两个堆栈。一些方便的方法,如 current_turn next_turn 提供 - 都返回一个播放器对象; next_turn!递增之交指数,如果有必要包装为0。

You have a simple Game object that's mostly a wrapper around an Array of size 40, plus some convenience methods. The Game object also tracks the number of available houses and hotels and the two stacks of Chance and Community Chest cards. A few convenience methods like current_turn and next_turn! are provided — both return a Player object; next_turn! increments the turn index, wrapping to 0 if necessary.

玩家可以在陆地上的所有位置必须从属性的超继承。在属性类定义如一些共同的东西租所有者设置房子购买?升级?。在租金所有者属性可能是。在设置属性返回一个阵列包含组中的所有属性。在设置属性可能大小不等,从1到4。房子产权重组presents酒店5 房子。

All locations the player can land on must inherit from a superclass of Property. The Property class defines a few common things like rent, owner, set, houses, purchasable?, and upgradeable?. The rent and owner properties may be nil. The set property returns an Array containing all properties within the group. The set property may vary in size from 1 to 4. The houses property represents a hotel as 5 'houses'.

游戏对象有一个阵列 播放器的对象,每个像位置(0到39的整数)场,(没有上限 - 银行技术上从来没有'缺钱), get_out_of_jail_frees in_jail?(因为位置不够了这一点)。在游戏对象还有一个指标来跟踪该轮到谁了。

The Game object has an Array of Player objects, each with fields like position (an integer from 0 to 39), money (no upper bound — the bank technically never 'runs out of money'), get_out_of_jail_frees, and in_jail? (since position is insufficient for this). The Game object also has an index to track whose turn it is.

特定属性的规则均设有各自的子类中的codeD。所以,举例来说,租金的一个的实施铁路是:

Property-specific rules are all encoded within their respective subclasses. So, for instance, the implementation of rent on a Railroad would be:

def rent
  owned_count = self.set.select { |rr| rr.owner == self.owner }.size
  return 25 * 2 ** (owned_count - 1)
end

机遇和公益金卡可以与一群倒闭,需要一个游戏,一个玩家对象作为参数来简单地实现。例如:

Chance and Community Chest cards can be simply implemented with a bunch of closures that takes a game and a player object as parameters. For instance:

# Second place in a beauty contest
COMMUNITY_CHEST_CARDS << lambda do |game, player|
  player.money += 10
end

# Advance token to Boardwalk
CHANCE_CARDS << lambda do |game, player|
  game.advance_token!(player, 39)
end

# Advance token to nearest railroad, pay double
CHANCE_CARDS << lambda do |game, player|
  new_position = [5, 15, 25, 35].detect do |p|
    p > player.position
  end || 5
  game.advance_token!(player, new_position)
  # Pay rent again, no-op if unowned
  game.properties[new_position].pay_rent!(player)
end

等。在 advance_token!方法显然处理之类的东西传递去了。

And so on. The advance_token! method obviously handles things like passing go.

显然,还有更多的细节 - 这是一个相当复杂的游戏,但希望这给你正确的想法。这当然会是绰绰有余了面试。

Obviously, there are more details — it's a fairly complicated game, but hopefully this gives you the right idea. It'd certainly be more than sufficient for an interview.

众议院规则可以打开或关闭通过增加一个 house_rules 阵列来切换的游戏对象。这将允许 FreeParking 属性像这样实现的:

House rules could be switched on or off by adding a house_rules Array to the Game object. This would allow the FreeParking property to be implemented like this:

class Game
  def house_rules
    @house_rules ||= []
  end

  def kitty
    # Initialize the kitty to $500.
    @kitty ||= 500
  end

  def kitty=(new_kitty)
    @kitty = new_kitty
  end
end

class FreeParking < Property
  def rent
    if self.game.house_rules.include?(:free_parking_kitty)
      # Give the player the contents of the kitty, and then reset it to zero.
      return -(_, self.game.kitty = self.game.kitty, 0)[0]
    else
      return 0
    end
  end
end

这篇关于在OOD大富翁游戏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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