Android等距棋盘

时间:2013-07-31 17:36:53

标签: java android

使用此代码,我用单个棋盘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)。但输出是这样的:

enter image description here

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

2 个答案:

答案 0 :(得分:3)

你写的是:

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

以及代码的第二部分

答案 1 :(得分:1)

你的for循环只循环了6次。你从0开始是正确的,但是让它int i = 0; i < 7; i++循环1。尝试将int i = 0; i <= 7; i++int i = 0; i < 8; i++都设置为有效。

相关问题