Java引用数组/几个不同的类?
我是新来的,虽然我在这里多次找到了很多信息,但这是我的第一个问题,所以现在让我看看我是否应该改变我的提问方式 嗯,我正在尝试创建一个简单的国际象棋Java程序,为此,我为每种不同的棋子(骑士、皇后、棋子……)创建了一个不同的类,但现在我需要将所有棋子存储在同一数组中,以便设置并获得它们在棋盘上的位置 为此,我考虑创建一个整数数组,将对不同对象的引用存储在正确的位置,并在没有片段的地方使用null或zero,但我在这方面遇到了太多问题 我还考虑创建一个名为“pieces”的超类,以及这个类的数组。通过这种方式,使用switch,我可以根据值调用每个子类(1代表典当,2代表骑士…),但这似乎太糟糕了,因为我应该修改很多东西,而且我确信有更好的方法可以做到这一点 提前谢谢 编辑:这是我班皇后的一些代码,所以你知道我是如何编码的:Java引用数组/几个不同的类?,java,arrays,class,reference,chess,Java,Arrays,Class,Reference,Chess,我是新来的,虽然我在这里多次找到了很多信息,但这是我的第一个问题,所以现在让我看看我是否应该改变我的提问方式 嗯,我正在尝试创建一个简单的国际象棋Java程序,为此,我为每种不同的棋子(骑士、皇后、棋子……)创建了一个不同的类,但现在我需要将所有棋子存储在同一数组中,以便设置并获得它们在棋盘上的位置 为此,我考虑创建一个整数数组,将对不同对象的引用存储在正确的位置,并在没有片段的地方使用null或zero,但我在这方面遇到了太多问题 我还考虑创建一个名为“pieces”的超类,以及这个类的数组。
public class Queen {
//Clase de la mejor banda de música de la historia
//Color. True when white, false when black.
private final boolean color;
//Parametres for position
private int x, y;
public Queen(boolean c, int x, int y){
color = c;
this.x=x;
this.y=y;
}
public String toString(){
String a;
if(color) a="q";
else a="Q";
return a;
}
/* Checks if the movement to the given space is possible or not.
* Returns true if possible, false if not.
*/
public boolean checkMove(int i, int j){
//Checks that the given space is right
if(i<1||i>8||j<1||j>8) return false;
//Space is the same of the piece
else if(i==x&&j==y) return false;
//Given space is in the same column as the piece
else if(i==x){
//Space is under the piece
if(j>y){
}
//Space is over the piece
else if(j<y){
}
}
//Given space is in the same row as the piece
else if(j==y){
//Space is to the right of the piece
if(i>x){
}
//Space is to the left of the piece
else if(i<x){
}
}
//DIAGONALS
else{
//Space is in a diagonal, down left of the piece
for(int aux_x=x, aux_y=y; aux_x>1 && aux_y<8; aux_x--, aux_y++){
if(aux_x==i && aux_y==j){
}
else if(Grid.board)
}
//
}
}
}
公共类皇后{
//历史博物馆
//颜色。白色时为真,黑色时为假。
私有最终布尔色;
//位置参数
私有整数x,y;
公共皇后(布尔c,整数x,整数y){
颜色=c;
这个.x=x;
这个。y=y;
}
公共字符串toString(){
字符串a;
如果(颜色)a=“q”;
else a=“Q”;
返回a;
}
/*检查是否可以移动到给定空间。
*如果可能,返回true;如果不可能,返回false。
*/
公共布尔checkMove(int i,int j){
//检查给定的空间是否正确
如果(i8 | | j8)返回false;
//空间是同一件作品
否则,如果(i==x&&j==y)返回false;
//给定的空间与工件在同一列中
else如果(i==x){
//这块布下面有一块空地
如果(j>y){
}
//空间就在这块上面
else if(jx){
}
//空间在作品的左边
否则,如果(i1&&aux_y创建一个名为ChessPiece
的超类,则继承该类中的所有类(因为所有类都是棋子)。ChessPiece
将有一个变量来表示棋子当前所在的位置。这似乎是最标准的做法
e、 g
然后,您可以创建一个ChessPiece
的ArrayList
,并获取/设置它们的位置。考虑以下界面:
public interface ChessPiece {
public enum Color { BLACK, WHITE };
public Color getColor();
//Check if move to (i,j) is possible
public boolean checkMove(int i, int j);
}
这是对你的女王的改变:
public class Queen implements ChessPiece {
//Clase de la mejor banda de música de la historia
//Color. True when white, false when black.
private final Color color;
//Parametres for position
private int x, y;
public Queen(Color c, int x, int y){
color = c;
this.x=x;
this.y=y;
}
@Override
public Color getColor() {
return color;
}
public String toString(){
if(Color.BLACK.equals(color)) {
return "q";
} else {
return "Q";
}
}
/* Checks if the movement to the given space is possible or not.
* Returns true if possible, false if not.
*/
@Override
public boolean checkMove(int i, int j){
//Checks that the given space is right
if(i<1||i>8||j<1||j>8) return false;
//Space is the same of the piece
else if(i==x&&j==y) return false;
[...]
}
}
公共类皇后实现棋子{
//历史博物馆
//颜色。白色时为真,黑色时为假。
私人最终颜色;
//位置参数
私有整数x,y;
公共皇后(颜色c、整数x、整数y){
颜色=c;
这个.x=x;
这个。y=y;
}
@凌驾
公共颜色getColor(){
返回颜色;
}
公共字符串toString(){
if(颜色.黑色.等于(颜色)){
返回“q”;
}否则{
返回“Q”;
}
}
/*检查是否可以移动到给定空间。
*如果可能,返回true;如果不可能,返回false。
*/
@凌驾
公共布尔checkMove(int i,int j){
//检查给定的空间是否正确
如果(i8 | | j8)返回false;
//空间是同一件作品
否则,如果(i==x&&j==y)返回false;
[...]
}
}
现在,您可以简单地将所有片段保存在数组或列表中:
List<ChessPiece> onBoard = new ArrayList<ChessPiece>();
//Now add a queen
onBoard.add(new Queen(ChessPiece.Color.WHITE, 1, 4));
List board=new ArrayList();
//现在添加一个皇后
船上。添加(新皇后(棋子。颜色。白色,1,4));
他们都能告诉你一个举动是否有效:
public boolean isCheck(Color c) {
Point p = getPositionOfKing(c);
for(int i = 0; i <8; ++i) {
for(int j = 0; j <8; ++j) {
ChessPiece cp = getAt(i,j);
if(cp != null && !cp.getColor().equals(c)) {
if(cp.isValidMode(p.x, p.y)) {
//The piece on i, j could capture the king!
}
}
}
}
}
public boolean isCheck(c色){
点p=国王的位置(c);
对于(int i=0;我为每个图形类使用一个基类或一个公共接口。此外,您提到使用值作为标识符,您应该看看这里更喜欢的枚举。欢迎来到堆栈溢出。因为这是关于编码的-我们希望看到一些代码。因为这是关于帮助您自己-向我们展示您的尝试到目前为止。它在哪里中断?你在哪里卡住了?好吧,我的代码没有真正中断,因为我还没有尝试编译它,因为我在思考如何做的时候遇到了这个问题。无论如何,我要发布我的类皇后(我开发得最多的一个),所以你可以看到我试图对每个类做什么。它不完整,但它是我工作方式的模板。好的,我将用一个例子问你,因为我真的不知道如何实现它。如果我创建一个对象王,它将继承ChessPiece的属性。在这之前我很好,但我如何区分哪个对象是我想要的对象s空格[3][4]如果数组是为Chesspiece创建的?有没有一种方法可以从超类中访问子类的特定方法,而不需要一个属性来说明它是哪种类型的棋子?是的,有一种方法,请阅读instanceof
操作符。例如if(someObj instanceof Queen){Queen Queen Queen=(Queen)someObj;//用queen做点什么}
谢谢,我会读到的。到目前为止,我只是使用instanceof重写equals()方法,但我想还有更多的应用程序
public boolean isCheck(Color c) {
Point p = getPositionOfKing(c);
for(int i = 0; i <8; ++i) {
for(int j = 0; j <8; ++j) {
ChessPiece cp = getAt(i,j);
if(cp != null && !cp.getColor().equals(c)) {
if(cp.isValidMode(p.x, p.y)) {
//The piece on i, j could capture the king!
}
}
}
}
}