通过此代码,我从单个棋盘bmpWhitebmpBlack绘制了等距棋盘

for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 7; j++) {
            if (white == true) {
            color[i][j] = 0;
            white = false;
            }
            else {
                color[i][j] = 1;
                white = true;
            }
        }
    }
}




public void onDraw(Canvas canvas)
{
    if (gameViewWidth == 0)
    {
        gameViewWidth = theGameView.getWidth();
        gameViewHeight = theGameView.getHeight();
    }
    for (int xx=0; xx < 7; xx++)
    {
        for (int yy=0; yy < 7; yy++)
        {
            int x_start=(yy * 23);
            int y_start=(gameViewHeight / 2) + (yy * 12);
            int xx1=x_start + (xx * 23);
            int yy1=y_start - (xx * 12);
            if (color[xx][yy] == 0)
            {
                canvas.drawBitmap(bmpWhite, xx1, yy1, null);
            }
            else if (color [xx][yy] == 1)
            {
                canvas.drawBitmap(bmpBlack, xx1, yy1, null);
            }
        }
    }


输出应为具有交替颜色的国际象棋(8x8)。但是输出是这样的:



如您所见,底部和顶部的最后两行是相同的颜色。
我做错了什么?

最佳答案

你写 :

for (int i = 0; i < 7; i++) {
    for (int j = 0; j < 7; j++) {
        if (white == true) {
            color[i][j] = 0;
            white = false;
        }
        else {
            color[i][j] = 1;
            white = true;
        }
    }
}

但看来您有8 * 8的木板。所以你必须写:
for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
        if (white) {
        color[i][j] = 0;
        white = false;
        }
        else {
            color[i][j] = 1;
            white = true;
        }
    }
}

以及代码的第二部分

关于java - Android等距棋盘,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17977195/

10-10 22:50