[英]How can I check which object is in my 2D Array in a certain index with Java?
我正在学习Java,并且尝试创建国际象棋游戏。 如何检查单元格对象数组的指定索引中的哪个对象? 我在开始时初始化所有对象(每个典当一个),然后我想说的是,将cell c
中的任何pawn
移动到cell c
如果它是空闲的),如果它已满,则检查下一个对象是否已满并在其中这种情况下,删除吃掉的pawn
并将我的pawn
移动2个空格。 唯一的是我不知道该怎么做...
package chess;
public class Main {
public static Field field = new Field();
public static void main(String[] args) {
field.create();
resetStartCondition();
}
protected static void resetStartCondition() {
int y = 0;
for (int x=0; x<8; x++) {
Field.cells[x][y].setFull(false);;
y++; }
// -- SPAWN ALL PIECES --- \\
Tower towb1 = new Tower(0, Field.cells[7][0]);
Tower towb2 = new Tower(0, Field.cells[7][7]);
Tower town1 = new Tower(1, Field.cells[0][0]);
Tower town2 = new Tower(1, Field.cells[0][7]);
King kingb = new King(0, Field.cells[3][7]);
King kingn = new King(1, Field.cells[3][0]);
Queen queenb = new Queen(0, Field.cells[4][7]);
Queen queenn = new Queen(1, Field.cells[4][0]);
Horse horb1 = new Horse(0, Field.cells[1][7]);
Horse horb2 = new Horse(0, Field.cells[6][7]);
Horse horn1 = new Horse(1, Field.cells[1][0]);
Horse horn2 = new Horse(1, Field.cells[6][0]);
Alf alfb1 = new Alf(0, Field.cells[2][7]);
Alf alfb2 = new Alf(0, Field.cells[5][7]);
Alf alfn1 = new Alf(1, Field.cells[2][0]);
Alf alfn2 = new Alf(1, Field.cells[5][0]);
Ped pedb1 = new Ped(0, Field.cells[0][6]);
Ped pedb2 = new Ped(0, Field.cells[1][6]);
Ped pedb3 = new Ped(0, Field.cells[2][6]);
Ped pedb4 = new Ped(0, Field.cells[3][6]);
Ped pedb5 = new Ped(0, Field.cells[4][6]);
Ped pedb6 = new Ped(0, Field.cells[5][6]);
Ped pedb7 = new Ped(0, Field.cells[6][6]);
Ped pedb8 = new Ped(0, Field.cells[7][6]);
Ped pedn1 = new Ped(1, Field.cells[0][1]);
Ped pedn2 = new Ped(1, Field.cells[1][1]);
Ped pedn3 = new Ped(1, Field.cells[2][1]);
Ped pedn4 = new Ped(1, Field.cells[3][1]);
Ped pedn5 = new Ped(1, Field.cells[4][1]);
Ped pedn6 = new Ped(1, Field.cells[5][1]);
Ped pedn7 = new Ped(1, Field.cells[6][1]);
Ped pedn8 = new Ped(1, Field.cells[7][1]);
}
}
package chess;
public class Cell {
int color;
int x;
int y;
boolean full = false;
public Cell (int color, boolean full, int x, int y) {
this.color = color;
this.x = x;
this.y = y;
this.full = full; }
// setters
protected void setColor(int c) {
this.color = c; }
protected void setX(int x) {
this.x = x; }
protected void setY(int y) {
this.y = y; }
protected void setFull(boolean set) {
this.full = set; }
// getters
protected int getColor() {
return this.color; }
protected int getX(){
return this.x; }
protected int getY(){
return this.y; }
protected boolean isFull() {
return this.full; }
protected Piece contains() {
// What should I do?
return null;
}
}
package chess;
public class Field {
public Field(){
}
public static void main(String[] args){}
protected static Cell cells[][] = new Cell[8][8];
private int y=0;
private int col = 0;
// 0 = white, 1 = black
protected void create() {
// 0 = white, 1 = black
for (int x=0; x<8; x++) {
if (x%2 == 0) {
if (y%2 == 0) {
col = 1;
}
} else {
if(!(y%2 == 0)) {
col = 1;
}
}
cells[x][y] = new Cell(col, false, x+1, y+1);
y++; }
//
}
}
package chess;
public abstract class Piece {
protected boolean eaten = false;
protected String name;
protected int color;
protected Cell cell;
public Piece(String n, int pc, Cell c) {
this.name = n;
this.color = pc;
this.cell = c;
}
protected void move(Field field, Piece p, Cell moveToCell) {
field.move(p, moveToCell);
}
protected void eat(Field field, Cell eatPiece, Piece piece) {
field.deleteItem(field, eatPiece);
eatPiece.setFull(true);
}
}
package chess;
public class Alf extends Piece {
public Alf(int pc, Cell c) {
super("Alf", pc, c);
c.setFull(true);
}
}
package chess;
public class Horse extends Piece {
public Horse(int pc, Cell c) {
super("Horse", pc, c);
c.setFull(true);
}
}
package chess;
public class King extends Piece {
public King(int pc, Cell c) {
super("King", pc, c);
c.setFull(true);
}
}
package chess;
public class Ped extends Piece {
public Ped(int pc, Cell c) {
super("Ped", pc, c);
c.setFull(true);
}
}
package chess;
public class Queen extends Piece {
public Queen(int pc, Cell c) {
super("Queen", pc, c);
c.setFull(true);
}
}
package chess;
public class Tower extends Piece {
public Tower(int pc, Cell c) {
super("Tower", pc, c);
c.setFull(true);
}
}
一种选择是存储(在变量中)“ Piece”位于指定单元格上,并且方法“ contains”(我将其称为getCurrentPiece)可以返回“ Piece”的名称。 例如:
public class Cell {
private Piece currentPiece;
public void setPiece(Piece currPiece){
this.currentPiece = currPiece;
}
public String getCurrentPiece(){
return this.currentPiece.getName();
}
很显然,您将需要一个getName();。 “件”类中的方法。 您可以这样实现:
public abstract class Piece {
protected String getName(){
return this.name;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.