私はJavaを学んでおり、チェスゲームの作成を試みています。 どのようなオブジェクトがセルオブジェクトの配列の指定されたインデックスにあるかを確認するにはどうすればよいですか? 開始時にすべてのオブジェクトを初期化します(各ポーンに1つずつ)。移動するとcell c
にあるpawn
が空いている場合はcell c
に移動し、完全であれば次が満杯かどうかを確認します場合は削除pawn
食べて、2つのスペースで自分の作品を移動します。Javaで特定のインデックスの2D配列に含まれるオブジェクトを確認するにはどうすればよいですか?
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);
}
}
データモデルの設計についてもう少し考えてください。現在は、そのセルがどのセル(正方形)にあるかを「知っている」が、そのセルにはピースがあるかどうかしかわかりません。部分も細胞も、ボード全体(フィールド)もすべての部分も知っているわけではありません。セルがそのセル内のピースを返すには、その情報がどこかにある必要があります。セルがピースを知らない場合、セルは情報にアクセスすることができません。私は、セルを、それ以外の方法( 'Cell'クラスの' Piece'フィールド)ではなく、その中にあると知っている方が自然であることが分かります。 –
@ OleV.V。だから、それはcru3lgeniusの答えは有効な方法ですか、それとも別のことを意味していますか? – Luke
その答えは、問題の自然な解決策であると思われるものを与え、私はおそらく行くでしょう。これは、あまりにも多くのコードを変更せずに問題を解決します。唯一の欠点は、ピースを動かすときには、(1)セルオブジェクトからセルを削除する(2)新しいセルの参照を保持するピースを更新すること(3)新しいセルを更新することですピースフィールドを参照してピースを参照します。 –