2013-07-31 32 views
0

有了這個代碼我繪製等距棋盤出的單一板件bmpWhitebmpBlack的Android等距棋盤

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

輸出應一個chessboar(8×8)具有交替的顏色。但輸出是這樣的:

enter image description here

正如你所看到的底部和頂部的最後兩行是相同的顏色。 我做錯了什麼?

回答

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

,也爲代碼

+0

好吧,現在我知道最新的問題:當應用程序繪製最後一行時,將其繪製爲2行。但爲什麼? – Stupe

+0

我認爲在每一個循環中,你都會對棋盤上留下的東西進行着色。我完全不理解你的代碼。 – Paniz

1

您的循環只循環6次的第二部分。你從0開始是正確的,但是使它int i = 0; i < 7; i++它循環1短。嘗試使它int i = 0; i <= 7; i++int i = 0; i < 8; i++都工作。