여기 체스 보드 (8x8)를 나타내는 멀티 -D 배열 조각을 만드는 ChessBoard 클래스가 있습니다. 조각이 움직일 때 조각이 어떤 공간을 통과하여 움직이는 지 확인하기 위해 보드를 스캔하려고합니다. 그렇다면 illegalMove 예외를 발생시킵니다. 아래 첨부 된 각 체스 조각에 대한 중첩 for 루프를 시작하는 방법입니다. 당신이 요청하는 경우 - 루프를위한 공간의 공석 반점 [] [] 여부를체스 보드를 검색하기 위해 중첩 된 for 루프 만들기
public class ChessBoard {
private Piece spots[][];
public ChessBoard() {
spots = new Piece[8][8];
}
public void placePieceAt(Piece piece, ChessPosition position)
{
spot[position.getX()][position.getY()] = piece;
piece.setPosition(position);
}
public abstract class Piece {
private ChessPlayer owner;
private ChessGame game;
protected ChessPosition position;
protected CPiece(ChessPlayer owner, ChessGame game, ChessPosition init_position) {
this.owner = owner;
this.game = game;
this.position = null;
game.getBoard().placePieceAt(this, init_position);
}
******here is where I was trying
public void checkIfPositionOccupied(ChessPosition destination){
ChessBoard[][] occupiedSpaces = new ChessBoard[8][8];
for (int i = 0; i <8 ; i++){
for(int j = 0; j<8; i++){
}
}
}
public void moveTo(ChessPosition destination)
throws IllegalMove
{
// Replace with your code as necessary
throw new IllegalMove(this, position, destination);
}
public char getMark() {
return mark;
}
}
class Rook extends Piece {
public Rook(ChessPlayer owner, ChessGame game, ChessPosition init_position) {
super(owner, game, init_position);
if (owner == game.getPlayer1()) {
mark = 'r';
} else {
mark = 'R';
}
}
public void moveTo(ChessPosition destination) throws IllegalMove
{
if((position.getX() == destination.getX() && position.getY() != destination.getY()) || (position.getX() != destination.getX() && position.getY() == destination.getY())){
setPosition(destination);
} else {
throw new IllegalMove(this, position, destination);
}
}
}
질문이 없습니다. – Dukeling
빈 공간을 찾기 위해 스팟 [] []을 스캔하기위한 둥지 for-loop를 만드는 방법 – Fish
이'if (owner == game.getPlayer1()) .. '도 다시 고려해야합니다. – Maroun