2013-04-24 31 views
0

這裏顯示的是ChessBoard類創建Multi-D數組件 - 代表棋盤(8x8)。當一塊棋子正在移動時,我正在掃描棋盤,以查看棋子是否會移動通過佔用的任何空間(如果是,則拋出非法移動異常)。下面是我爲每個棋子開始嵌套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 2013-04-24 16:12:13

+0

如何創建一個嵌套for循環來掃描空間空缺的點[] [] – Fish 2013-04-24 16:13:18

+0

您需要重新考慮這個if(owner == game.getPlayer1())..'。 – Maroun 2013-04-24 16:14:40

回答

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. 
     } 
    } 
    } 

Make a hasPiece()方法返回布爾值,如果positi被佔用。

+1

'occupiedSpaces'還沒有任何數據。 'spot [i] [j] == null'來代替? – Dukeling 2013-04-24 16:18:10

+0

是啊,我的壞!需要更多的咖啡.. 它也可以是== null,是的。更簡單。 ;) – 2013-04-24 16:19:35

+0

那麼如果(spot [i] [j] = null)? – Fish 2013-04-24 16:22:08

0

一些完全錯誤..

如果Piece是一樣的東西Rook見你們的關係Rook is Piece。那你的ChessBoard有沒有64 (8*8)Piece?你應該有單獨的類Spots和不使用Piece定義ChessSPots

而且根據您的問題can the board to see if the piece will move through any spaces

不僅需要掃描DestinationPosition而且掃描可能的方式,以該位置爲當前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*/ 
    } 

}