2013-04-24 2 views
0

여기 체스 보드 (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); 
    } 
} 

}

+3

질문이 없습니다. – Dukeling

+0

빈 공간을 찾기 위해 스팟 [] []을 스캔하기위한 둥지 for-loop를 만드는 방법 – Fish

+0

이'if (owner == game.getPlayer1()) .. '도 다시 고려해야합니다. – Maroun

답변

0

확실하지를 스캔 내가 둥지를 만들려면 어떻게해야합니까 이것도 괜찮습니다 :

ChessBoard[][] occupiedSpaces = new ChessBoard[8][8]; 
    for (int i = 0; i <8 ; i++){ 
    for(int j = 0; j<8; i++){ 
    if (spot[i][j]!=null) 
      //okay, occupied. 
     { 
      // Make whatever you want here. 
     } 
    } 
    } 

positi() 메서드가 false 일 경우 부울을 반환하는 hasPiece 에 점령되었습니다. Rook 같은 것이 관계 Rook is Piece를 참조입니다

Piece 경우 완전히 잘못된

+1

'occupiedSpaces'에는 아직 데이터가 없습니다. 'spots [i] [j] == null'입니까? – Dukeling

+0

그래, 내 나쁜 !!! 좀 더 커피가 필요해. 또한 == null 일 수도 있습니다. 더 간단한. ;) –

+0

그렇다면 (spot [i] [j] = null)? – Fish

0

뭔가 .... 그러면 ChessBoard64 (8*8)Piece이 어떻게 표시됩니까? 당신은 Spots에 대한 별도의 클래스가해야하지 현재 Piece을 위해 그 자리에 가능한 방법을 당신은 DestinationPosition를 스캔 할 필요가 귀하의 질문에 can the board to see if the piece will move through any spaces

당으로서 또한 ChessSPots

을 정의 할 수 Piece를 사용뿐만 아니라 스캔 할 수 있습니다. 그러므로 Move 방법은 ConCrete Piece like Rook

checkIfPositionOccupied 방법에는 루프가 필요하지 않습니다.

public void checkIfPositionOccupied(ChessPosition destination){ 
    if(spot[destination.getX()][destination.getY()] !=null){ 
     /*Destination is Occupied*/ 
    }else{ 
     /*Destination is Not Occupied*/ 
    } 

}