2013-01-11 52 views
-7

我对这个完全不知所措。这里的说明,到目前为止,代码:设置Java游戏块

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(...)部分下完成的代码帮助。

+3

这是什么问题? –

+0

请将文本墙重新格式化为更容易理解的内容,并清楚地表明您的问题。 –

+1

如果对于不清楚的指示有某些具体的内容,我相信你会发现周围的人很乐意帮忙。如果问题是“我该做什么”,可能很难在不违背任务要点的情况下帮助你。 –

回答

2

试图让你去,而无需编写你整个事情:

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

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

然后,“吸气剂”类型的函数返回当前对象中可用的值,这样

public int getPlayerType() { 
    return myPlayerType; 
} 

塞特斯是INVERS e,他们根据传递的参数设置内部变量:

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个变化,这将是同样适用。

这应该让你开始,不要犹豫,如果你后来卡住了,就不要犹豫了。祝你好运!

+0

该部分应该怎么做:该方法应该根据作品的类型,名称和当前位置(列和行)形成一个描述性字符串。字符串的确切格式取决于您! public String toString()我认为应该有一个if else语句并返回一些内容,但我不确定。 –

+0

@教授我会编辑我的答案 – fvu

+0

非常感谢。 –