使用继承和多态来解决常见的游戏问题 [英] Using inheritance and polymorphism to solve a common game problem

查看:100
本文介绍了使用继承和多态来解决常见的游戏问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个班;让我们称之为食人魔和巫师。 (所有字段都是公开的,使得示例更容易输入。)

  public class Ogre 
{
重量
int height;
int axeLength;
}

public class Wizard
{
int age;
int IQ;
int height;
}

在每个类中,我可以创建一个名为如果一个Ogre遇见和Ogre或一个向导遇到一个向导,将决定谁将赢。这是一个例子。如果食人魔遇到食人魔,那么重的人就胜出了。但是如果重量是相同的,那么长斧的胜率就会赢了。

  public Ogre battle(Ogre o)
{
if(this.height> o.height)return this;
else if(this.height< o.height)return o;
else if(this.axeLength> o.axeLength)返回此;
else if(this.axeLength< o.axeLength)return o;
else return //默认情况
}

我们可以为向导做一个类似的方法。 >

但是如果一个向导遇到一个食人魔呢?我们当然可以做一个方法,比较,只是说高点。

 公共向导战斗(Ogre o)
{
if(this.height> o.height)return this;
else if(this.height< o.height)return o;
else return
}

我们会为与会者向导的Ogres做一个类似的。但是如果我们不得不向程序添加更多的字符类型,那么事情就会失控。



这是我被卡住的地方。一个明显的解决方案是创建一个具有共同特征的Character类。 Ogre和Wizard继承自Character,并将其扩展为包含定义每个角色的其他特征。

  public class Character 
{
int height;

public Character battle(Character c)
{
if(this.height> c.height)return this;
else if(this.height< c.height)返回c;
else return
}
}

有更好的组织课程的方法吗?我看过了策略模式和调解器模式,但我不知道他们(如果有的话)可以帮助这里。我的目标是要达到某种常见的战斗方法,所以如果一个食人魔遇见一个食人魔,那么它会使用Ogre vs Ogre的战斗,但是如果一个Ogre遇到一个巫师,那么它会使用一个更为通用的游戏。此外,如果满足的人物没有共同的特征呢?我们如何决定谁赢得了战斗?



编辑:很多很好的回应!我需要消化它们,找出哪一个最适合我的情况。

解决方案

访客模式是一种将算法与其操作的对象结构分离的方法。



对于您的示例,您可以使用

  class Character {
boolean battle(BattleVisitor visitor){
return visitor.visit(this);
}
}

class Ogre extends Character {..}
类向导扩展Character {..}
class Dwarf extends Character {..}

接口BattleVisitor {
布尔访问(Ogre字符);
布尔访问(向导字符);
布尔访问(矮人字符);
}

class OgreBattleVisitor实现BattleVisitor {
private Ogre ogre;
OgreBattleVisitor(Ogre ogre){this.ogre = ogre; }
boolean visit(Ogre ogre){
//定义战斗
}

布尔访问(向导向导){
//定义战斗
}
...
}

每当一场战斗发生:

  targetChar.battle(new OgreBattleVisitor(ogre)); 

定义向导和小矮人的访问者实现以及出现的任何内容。另请注意,我将访问方法的结果定义为 boolean (赢或输),而不是返回



因此,添加新类型时,您必须添加:




  • 一个访问者处理新类型的方法。

  • 处理新类型的争斗的实现



现在,在这里,事实证明,如果Ogre vs Wizard==Wizard vs Ogre,您将会有一些重复的代码。我不知道是否是这种情况 - 例如可能会有差异取决于谁先打。另外,您可能想要提供完全不同的算法,比起与ogre的村庄战争相比,沼泽与食人魔之战。因此,您可以创建一个新的访问者(或访问层次结构),并在需要时应用适当的访问。


I have two classes; let's call them Ogre and Wizard. (All fields are public to make the example easier to type in.)

public class Ogre
{
  int weight;
  int height;
  int axeLength;
}

public class Wizard
{
  int age;
  int IQ;
  int height;
}

In each class I can create a method called, say, battle() that will determine who will win if an Ogre meets and Ogre or a Wizard meets a Wizard. Here's an example. If an Ogre meets an Ogre, the heavier one wins. But if the weight is the same, the one with the longer axe wins.

public Ogre battle(Ogre o)
{
  if (this.height > o.height) return this;
  else if (this.height < o.height) return o;
  else if (this.axeLength > o.axeLength) return this;
  else if (this.axeLength < o.axeLength) return o;
  else return this;    // default case
}

We can make a similar method for Wizards.

But what if a Wizard meets an Ogre? We could of course make a method for that, comparing, say, just the heights.

public Wizard battle(Ogre o)
{
  if (this.height > o.height) return this;
  else if (this.height < o.height) return o;
  else return this;
}

And we'd make a similar one for Ogres that meet Wizard. But things get out of hand if we have to add more character types to the program.

This is where I get stuck. One obvious solution is to create a Character class with the common traits. Ogre and Wizard inherit from the Character and extend it to include the other traits that define each one.

public class Character
{
  int height;

  public Character battle(Character c)
  {
    if (this.height > c.height) return this;
    else if (this.height < c.height) return c;
    else return this;
  }
}

Is there a better way to organize the classes? I've looked at the strategy pattern and the mediator pattern, but I'm not sure how either of them (if any) could help here. My goal is to reach some kind of common battle method, so that if an Ogre meets an Ogre it uses the Ogre-vs-Ogre battle, but if an Ogre meets a Wizard, it uses a more generic one. Further, what if the characters that meet share no common traits? How can we decide who wins a battle?

Edit: Lots of great responses! I need to digest them and figure out which one works best for my situation.

解决方案

The visitor pattern "is a way of separating an algorithm from an object structure it operates on".

For your example, you can have

class Character {
    boolean battle(BattleVisitor visitor) {
       return visitor.visit(this);
    }
}

class Ogre extends Character {..}
class Wizard extends Character {..}
class Dwarf extends Character {..}

interface BattleVisitor {
    boolean visit(Ogre character);
    boolean visit(Wizard character);
    boolean visit(Dwarf character);
}

class OgreBattleVisitor implements BattleVisitor {
    private Ogre ogre;
    OgreBattleVisitor(Ogre ogre) { this.ogre = ogre; }
    boolean visit(Ogre ogre) {
      // define the battle 
    }

    boolean visit(Wizard wizard) {
      // define the battle 
    }
    ...
}

And whenever a fight occurs:

targetChar.battle(new OgreBattleVisitor(ogre));

Define a Visitor implementation for a Wizard and a Dwarf and whatever appears. Also note that I define the result of the visit method to be boolean (won or lost) rather than to return the winner.

Thus, when adding new types, you will have to add:

  • a method to the visitor to handle fighting the new type.
  • an implementation for handling the fights for the new type

Now, here it turns out that you will have some duplication of code in case "Ogre vs Wizard" == "Wizard vs Ogre". I don't know if this is the case - for example there might be a difference depending on who strikes first. Also, you may want to provide totally different algorithm for, let's say "Swamp battle with ogre" compared to "village battle with ogre". Thus you can create a new visitor (or a hierarchy of visitors) and apply the appropriate one whenever needed.

这篇关于使用继承和多态来解决常见的游戏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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