0

With this code I draw a isometric chessboard out of single board-pieces bmpWhite and bmpBlack

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

The output should a chessboar(8x8) with alternating colors. But the output is this:

enter image description here

As you can see the last two lines of the bottom and top are the same color. What did I do wrong?

4

2 回答 2

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

以及代码的第二部分

于 2013-07-31T17:42:25.333 回答
1

您的 for 循环仅循环 6 次。你从 0 开始是对的,但是int i = 0; i < 7; i++让它循环 1 短。尝试使它int i = 0; i <= 7; i++int i = 0; i < 8; i++两者都起作用。

于 2013-07-31T17:42:57.277 回答