设置Java游戏片段

时间:2013-01-11 17:32:07

标签: java eclipse ide

我对这一点完全不知所措。这是目前的说明和代码:

import java.util.*;

abstract public class AbstractGamePiece
{

    // These two constants define the Outlaws and Posse teams
    static public final int PLAYER_OUTLAWS = 0;
    static public final int PLAYER_POSSE = 1;

    // These variables hold the piece's column and row index
    protected int myCol;
    protected int myRow;

    // This variable indicates which team the piece belongs to
    protected int myPlayerType;

    // These two strings contain the piece's full name and first letter abbreviation
    private String myAbbreviation;
    private String myName;

    // All derived classes will need to implement this method
    abstract public boolean hasEscaped();

    // Initialize the member variables with the provided data.
    public AbstractGamePiece(String name, String abbreviation, int playerType)
    {

    }

}

我需要帮助的是完成公共AbstractGamePiece(...)部分下的代码。

1 个答案:

答案 0 :(得分:2)

试图让你无需为你编写全部内容:

对于第1点,目标是根据传递给构造函数的参数初始化内部变量(已在类中定义):

public AbstractGamePiece(String name, String abbreviation, int playerType) {
    myName = name;
    // and so on
}

然后,“getter”类型函数返回当前对象中可用的值,如此

public int getPlayerType() {
    return myPlayerType;
}

Setter是反向的,它们根据传递的参数设置内部变量:

public void setPosition(int col, int row) {
    myRow = row;
    myCol = col;
}

等等。

然后,根据说明,你将不得不使用这个抽象类作为几个具体类的基础:

public class Henchman extends AbstractGamePiece {

    // the constructor - not sure what exactly should be passed in here
    // but you get the idea - this constructor doesn't have to have the
    // same "signature" as super's
    public Henchman(String name) {
        super(name, "hm", PLAYER_OUTLAWS);
    }

    // an an implementation of abstract method hasEscaped
    @Override
    public boolean hasEscaped() {
        return false;  // as per the instructions
    }

}

toString方法将当前对象的特定描述作为(人类可读的)字符串返回,并且它可以例如用于打印人类可读的当前片段列表,以便在开始开发后帮助分析/调试游戏游戏引擎。正如说明所说,它的作用取决于你,使它返回所有有趣和识别信息。为了让你开始,为亨奇曼:

public toString() {
    String.format("Henchman name=%s team=%d escaped=%",myName,myTeam,hasEscaped());
}

但是有1000种变体同样适用。

这应该让你开始,如果你以后遇到困难,不要犹豫,创建一个新的问题。祝你好运!