接口允许单位使用设计模式在Java中攻击地面,空中或两者的能力 [英] Interfaces to allow unit's ability to attack ground, air, or both in Java using Design Patterns

查看:144
本文介绍了接口允许单位使用设计模式在Java中攻击地面,空中或两者的能力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作一个有抽象战斗机类的文本游戏。在Fighter中我有一个名为protected boolean isGround的变量。那么我有两个抽象类,它们扩展称为AirFighter和GroundFighter的战斗机,它基本上将isGround设置为True或False,取决于它们是否是地面单元。



然后,我想设置游戏,以便在我的战斗阶段游戏检查玩家1的速度是否比玩家2快,并且取决于谁是更快的像p1.attack(p2)一样,打印出这场战斗的统计数据,就像每个玩家的剩余健康状况一样。



我想要一些空袭者攻击空气或攻击空中和地面的方式,我想让一些地面战斗机只能攻击地面,或地面和空中。在保持松散耦合的情况下,我该怎么做?这是一个设计模式的课堂,令人沮丧的我。有没有设计模式来帮助我?



想想游戏的星际争霸,这基本上是我的游戏模拟,某些单位像海洋可以攻击地面和空气,一些空气单位只能攻击空气,有些可以做两个我想能够做到这一点,没有一个讨厌的,如果其他检查声明。

解决方案

一个简单的方法是在基础中有一个方法 Fighter 可以用来检查是否 Fighter 可以攻击另一个:

  public abstract class Fighter {
public abstract boolean isGround();
public abstract boolean canAttack(Fighter target);
}

子类可以覆盖 canAttack 做出适当的决定。例如:

  public class SurfaceToAirLauncher extends Fighter {
@Override public boolean isGround(){
/ /导弹发射器在地面上
返回true;
}
@Override public boolean canAttack(Fighter target){
//我们只能攻击空中目标
return(target!= null&" target.isGround ));
}
}

public class Dragon extends Fighter {
@Override public boolean isGround(){
//龙在空中
返回false;
}
@Override public boolean canAttack(Fighter target){
//龙可以攻击每个目标。
return(target!= null);
}
}

公共类Knight extends Fighter {
@Override public boolean isGround(){
// knights are on ground
返回真
}
@Override public boolean canAttack(Fighter target){
//骑士只能攻击龙
return(target!= null&&" target instanceof Dragon);
}
}

您可以删除 null 如果你知道目标永远不会是 null 。我把它包括在内。


Im making a text game that has an abstract Fighter class. within Fighter I have a variable called protected boolean isGround. then I have 2 abstract classes that extend Fighter called AirFighter and GroundFighter that basically set that isGround to be True or False, depending on if they are ground units or not.

I then want to set up the game so that in my battle phase the game checks if player 1's speed is faster then player 2, and depending on who is faster to then do like p1.attack(p2), and print out the stats of that battle, like the remaining health of each player or something like that.

I want a way for some airFighters to attack air, or attack air and ground, and I want some groundFighters to attack ground only, or ground and air. How would I go about doing this while staying loosely coupled? It is for a design pattern's class and its frustrating me. Is there a design pattern to help me with this?

Think of the game starcraft, that's basically what my game is mimicing, certain units like a marine can attack ground and air, some air units can only attack air, some can do both. I want to be able to do this without having some nasty if else checking statement.

解决方案

An easy way to do this is to have a method in the base Fighter that can be used to check if that Fighter can attack another:

public abstract class Fighter {
    public abstract boolean isGround ();
    public abstract boolean canAttack (Fighter target);
}

Subclasses can then override canAttack and make an appropriate decision. For example:

public class SurfaceToAirLauncher extends Fighter {
    @Override public boolean isGround () {
        // missile launcher is on the ground
        return true; 
    }
    @Override public boolean canAttack (Fighter target) {
        // we can only attack air targets
        return (target != null && !target.isGround());
    }
}

public class Dragon extends Fighter {
    @Override public boolean isGround () {
        // dragons are in the air
        return false; 
    }
    @Override public boolean canAttack (Fighter target) {
        // dragons can attack every target.
        return (target != null);
    }
}

public class Knight extends Fighter {
    @Override public boolean isGround () {
        // knights are on the ground
        return true; 
    }
    @Override public boolean canAttack (Fighter target) {
        // knights can only attack dragons
        return (target != null && target instanceof Dragon);
    }
}

You could remove the check for null if you know target will never be null. I included it for completeness.

这篇关于接口允许单位使用设计模式在Java中攻击地面,空中或两者的能力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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