2016-04-14 71 views
1

我目前正在開發的國際象棋的Android版本,但我有平局的問題。我已經設置了一個ChessboardView類來擴展視圖,並使用另一個稱爲Tile的輔助類。問題在於我的所有作品都在正確的位置上繪製(我的調試器顯示當我點擊瓷片時我所擊中的是什麼),但所有圖像都顯示爲Black Queens(我認爲是因爲這是最後一種情況)在我的轉換聲明中被擊中,因爲當我擺脫它時,所有的部分變成黑色車)。下面是相關的代碼:Android的繪圖棋子

ChessboardView.java:

protected void onDraw(final Canvas canvas){ 
    final int width = getWidth(); 
    final int height = getHeight(); 
    this.squareSize = Math.min(
      getSquareSizeWidth(width), 
      getSquareSizeHeight(height) 
    ); 
    getOrigins(width,height); 
    for(int col = 0; col < NUM_RC; col++){ 
     for(int row = 0; row < NUM_RC; row++){ 
      final int xCoord = getXCoord(col); 
      final int yCoord = getYCoord(row); 
      final Rect tileRect = new Rect(xCoord,yCoord,xCoord+squareSize,yCoord+squareSize); 
      String loc = getRowString(row) + getColumnString(col); 
      String piece = Chessboard.getPieceAtLocation(loc); 
      tiles[col][row].setTileRect(tileRect); 
      tiles[col][row].draw(canvas,piece,getContext()); 
     } 
    } 
} 

Tile.java

public void draw(Canvas canvas,String piece, Context c){ 
    canvas.drawRect(tileRect, tileColor); 
    if(!piece.equals(" ") && !piece.equals("##")){ 
     Bitmap pieceBM = null; 
     switch(piece) { 
      case "wp": { 
       pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.white_pawn); 
      } 
      case "wK": { 
       pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.white_king); 
      } 
      case "wB": { 
       pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.white_bishop); 
      } 
      case "wN": { 
       pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.white_knight); 
      } 
      case "wR": { 
       pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.white_rook); 
      } 
      case "wQ": { 
       pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.white_queen); 
      } 
      case "bp": { 
       pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.black_pawn); 
      } 
      case "bK": { 
       pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.black_king); 
      } 
      case "bB": { 
       pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.black_bishop); 
      } 
      case "bN": { 
       pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.black_knight); 
      } 
      case "bR": { 
       pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.black_rook); 
      } 
      case "bQ": { 
       pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.black_queen); 
      } 
     } 
     canvas.drawBitmap(pieceBM,null,tileRect,null); 
    } 
} 

回答

3

你需要把一個break;在每一種情況下結束。否則它會下降並執行下一個案例。

+0

ahh哇我完全錯過了。謝謝! – atg963

+0

@ atg963和大括號('{}')是多餘的;在switch/case塊中,每個case的語句由'case x:'和'break'分隔,除了'switch()'之後的外括號之外,switch/case塊不需要花括號。實例和更多信息:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html – 11684

+0

而@GabeSechan:我知道它已經相當一段時間,因爲你寫你的答案,但我覺得它可能是對未來的遊客來說很好,可以在你的回答中加入這個。 – 11684