gpt4 book ai didi

java - Android绘制棋子

转载 作者:搜寻专家 更新时间:2023-11-01 08:35:56 24 4
gpt4 key购买 nike

我目前正在开发 Android 版本的国际象棋,但我遇到了平局问题。我已经设置了一个 ChessboardView 类来扩展 View 并使用另一个名为 Tile 的辅助类。问题是我所有的作品都被绘制在正确的位置(我的调试器显示当我点击瓷砖时我正在击中的是什么)但是所有的图像都显示为黑皇后(我认为这是最后一种情况在我的 switch 语句中被击中,因为当我摆脱它时,所有的棋子都会变成黑车)。这是相关代码:

棋盘 View .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());
}
}
}

瓷砖.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);
}
}

最佳答案

您需要在每个案例的末尾放置一个break;。否则它会倒下并执行下一个案例。

关于java - Android绘制棋子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36631552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com