Java - 制作纸牌游戏,对课程有疑问

时间:2013-12-15 03:43:27

标签: java

我是(大多数)自学成才的程序员,有一个java课程,所以我还是很新,我正试图从头开始制作我的第一个真正的应用程序。

基本上,我正在制作我拥有的纸牌游戏的应用版本(仅适合我!不卖或任何东西!)并且在这个纸牌游戏中你基本上使用你的入门卡从阵容中“购买”更多的牌从主甲板上抽出的卡片。这些卡有几个与它们相关联的值,可以很好地转换为“功率级别”和“点值”之类的应用程序,此外,当你像“画一张额外的卡片”或“销毁”时,这些卡片中的每一张都会发挥特殊作用。一张牌“或”给另一位玩家一张负面的牌“ - 那种类型的东西。

我已经为游戏制作了整个用户界面,并且(大部分)完成了这项工作,现在我正在研究它的所有内容 - 代码。我创建了一个名为Card的构造函数类,如下所示:

public class Card 
{
private int power;
private int value;
private String type;
private String cardName;
private ImageIcon image;

    public Card(int power, int value, String type, String cardName, ImageIcon image)
    {
        this.power = power;
        this.value = value;
        this.type = type;
        this.cardName = cardName;
        this.image = image;
    }
...
}

用省略号代表我所有的getter / setter方法。

所以你可以看到我的Card类将为他们分配所有必要的值但是一个 - 他们每个人做的特殊“事情”(抽奖卡/销毁卡等等)

我不确定在我的代码中应该放在哪里/怎么做。因为我有一个类型值,代表你可以绘制的不同类型的卡(设备/超级大国/英雄等等)。我想我应该为每种卡类型单独创建一个类..这是一个很好的编码实践?

总而言之,总结这篇非常冗长的帖子 - 我应该将所有不同的卡片“类型”分成不同的类别,我应该如何实现每张卡片的特殊“事物”呢?我觉得它将被包装在一个方法中,但据我所知,我不能让一个方法成为构造函数的一部分,可以吗?

2 个答案:

答案 0 :(得分:2)

您可以将卡片设为界面抽象类。如果你使用后者,你实际上可以定义逻辑类似于你上面已有的。根据您的路线,每个“卡”将实现接口或扩展抽象类。

接口的缺点是你必须将所有字段重新实现为子类的私有变量。

<强>抽象

public abstract class Card  {
    protected int power;
    protected int value;
    protected String type;
    protected String cardName;
    protected ImageIcon image;

    // You cannot instantiate this directly, only through children.
    public Card(int power, int value, String type, String cardName, ImageIcon image) {
        this.power = power;
        this.value = value;
        this.type = type;
        this.cardName = cardName;
        this.image = image;
    }

    // Getters/Setters...
}

public class AttackCard extends Card {
    public AttackCard(int power, int value, String type, String cardName, ImageIcon image);
        super(power, value, type, cardName, image);
    }
}

<强>接口

public interface Card  {
    int getPower();
    int getValue();
    String getType();
    String getCardName();
    ImageIcon getImage();
}

public class AttackCard implements Card {
    private int power;
    private int value;
    private String type;
    private String cardName;
    private ImageIcon image;

    public AttackCard(int power, int value, String type, String cardName, ImageIcon image);
        this.power = power;
        this.value = value;
        this.type = type;
        this.cardName = cardName;
        this.image = image;
    }

    // Implement methods from Card...
}

修改

如果您正在实施DC漫画套牌游戏,那么您可能只需要为所有卡片提供1个抽象类,并为Hero,Villain,Super Power和Starter提供界面。不确定他们所输入的是什么......然后所有英雄都会扩展抽象卡类以获得卡片的所有常见属性,并实现特定于Hero的方法。

每张卡都不需要单独的课程。每个英雄都只是英雄的一个实例。你只需要一个英雄卡集合。

// Hierarchy
abstract class Card
interface Hero
interface Villain
class HeroCard extends Card implements Hero
class VillainCard extends Card implements Villain

// Example constructors definitions for reference?
Card(String name, int value, int power);
HeroCard(String name, int value, int power);
VillainCard(String name, int value, int power);

// Create all heroes
List<Hero> heroes = new ArrayList<Hero>();
heroes.add(new HeroCard("Green Arrow", 5, 2));

// Create all villains
List<Villain> villains = new ArrayList<Villains>();
villains.add(new VillainCard("Ra's Al Ghul", 8, 3));

// All all the heroes and villains to all cards
List<Card> cards = new ArrayList<Card>();
cards.addAll(heroes);
card.addAll(villains);

答案 1 :(得分:1)

您可以使用inheritance通用类所在的functionality所有Card类可以放在那里,然后在每个子类Card中使用 public class SuperCardClass { ........ public void someCommonFunction() { } } public class SubCardFirstClass extends SuperCardClass { @Override public void someCommonFunction() { //specific to sub class } } 放置类的特定功能,也可以使用基类的常用功能。还可以覆盖超类的方法。

http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

{{1}}