【问题标题】:Finding a way to handle colors and powers on a board game寻找一种在棋盘游戏中处理颜色和力​​量的方法
【发布时间】:2012-06-01 12:58:14
【问题描述】:

我正在为我的学校项目创建一个游戏,所以我在这里不是为了任何类型的代码,只是关于如何做我想做的事情的全球想法......

我有 Pieces,每件都有自己的颜色和类型,如下所示:
片 1 - 黄色 - 简单
第 2 部分 - 黄色 - 易爆
....
Piece 10 - 蓝色 - 改变颜色

在游戏开始时,该列表中的随机棋子(之前创建的列表中只有一种棋子),如果我们连续有 3 个或更多棋子,游戏将删除它们并计算分数,但是如果三个(或更多)中的一个具有任何“力量效应”,那么周围的碎片必须对这种力量做出反应(我的英语水平很难解释,但我认为你明白了:S..)

目前我的游戏只有简单的部分,为此我使用了这样的枚举:

public enum CorPeca 
{    
     AMARELO,
     AZUL,
     VERMELHO;
}

在板课上我是这样做的:

    this.arrayPecas = new ArrayList<Peca>();

    for (CorPeca corPeca : CorPeca.values())
        this.arrayPecas.add(new Peca(corPeca));

在 Pieces 课上我正在这样做

public Peca(CorPeca tipoPeca)
{
    this.definirPeca(tipoPeca);
}

public CorPeca getCorPeca() { return this.corPeca; }
public char getCharPeca() { return this.charPeca; }

public void definirPeca(CorPeca tipoPeca)
{
    switch (tipoPeca)
    {
        case AMARELO:
            this.charPeca = 'a';
            this.corPeca = CorPeca.AMARELO;
            break;
        case AZUL:
            this.charPeca = 'u';
            this.corPeca = CorPeca.AZUL;
            break;
        case VERMELHO:
            this.charPeca = 'e';
            this.corPeca = CorPeca.VERMELHO;
            break;                
    }
}

但我在想一个很好的方法来轻松添加那些“Power Pieces”,但更重要的是,将来能够添加更多颜色和更多“Powers”......

我正在考虑创建子类并使用扩展,但整个想法并没有填满我的脑海,因为我们不能扩展多个类...

那么谁能给我一个关于如何存档的全局想法?

【问题讨论】:

    标签: java hierarchy


    【解决方案1】:

    执行此操作的经典 OOP 方法是定义一个抽象基类,它代表所有部分并包含通用功能(例如权力列表):

    public abstract class Piece {
        // put in fields that every piece has, e.g. x and y co-ordinate here
    
        // array of powers
        protected ArrayList<Power> powers=new ArrayList<Power>();
    
        // method to add new powers to the piece
        public void addPower(Power power) {
            powers.add(power);
        }
    
        // power method - optionally overriden by specififc pieces
        public void applyPowers() {
            for (Power power:powers) {
                power.methodToActivatePower();
            }
        }
    
        // abstract char method
        public abstract char getChar();
    
    }
    

    请注意,abstract 表示您不能直接实例化此类 - 您只能创建具体子类的实例。

    然后扩展这个类来实现每个特定的部分:

    public class YellowPiece extends Piece {
    
        // override applyPowers for yellow piece (if needed)
        @Override
        public void applyPowers() {
            // code for yellow piece power handling goes here
        }
    
        // override char - required
        @Override
        public char getChar() {
            return 'y';
        }
    
    }
    

    您仍然可以创建 ArrayList&lt;Piece&gt; 并使用以下内容填充它:

    ArrayList<Piece> pieces = new ArrayList<Piece>();
    pieces.add (new YellowPiece());
    pieces.add (new GreenPiece());
    // etc....
    

    然后,如果您想应用特定作品的力量,只需这样做:

    pieces.get(pieceNumber).applyPower();
    

    您显然需要大量自定义此代码以使其在您的游戏中运行,但希望它可以为您提供总体思路。请注意,还有许多其他方法可以实现相同的目标 - 我刚刚描述了“经典”OOP 方法。

    附:抱歉所有代码都是英文的,但你的英语比我的葡萄牙语好得多:-)

    【讨论】:

    • 您忘记指定 YellowPiece extends Piece。您还应该添加 @Override 注释作为良好做法。
    • 非常感谢您的帮助!真的^^(PS:这是葡萄牙语不是西班牙语,这是一个常见的错误没问题xD)。只是一个问题,您的编码是说每种颜色都有特定的力量不是吗?那么如何定义不同的力量呢?
    • @aliasbody - 嗯,好吧,也许我误解了权力的意义......那么 Power 本身可以是一个抽象类,它有多个用于不同电源类型的子类。那么你可以做类似piece.addPower(new ExplosionPower()); 等的事情。希望这有意义吗?将对答案进行一些快速编辑
    • 因此,如果我理解得很好,我将拥有 2 个“超类”碎片和权力(例如),每个都有各自的子类黄色、蓝色等。用于碎片和爆炸、ChangeCOlor 等。 . 为了权力。现在我只需要找到一种方法,并修改代码,这样我就可以将它连接到我的棋盘类,以使电源激活并重现对游戏的影响。我有同样的想法只是不知道如何应用它(因为抽象与子类的事情),现在这一切对我来说很有意义^^谢谢!
    • @aliasbody - 听起来你已经掌握了 - 祝你一切顺利,玩得开心!
    猜你喜欢
    • 2013-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    相关资源
    最近更新 更多