2017-05-31 117 views
-1

你們中的許多人都知道計算機的名爲「KMines」的遊戲,我使用Android Studio爲手機創建了應用程序。 遊戲的作品,但不是很好,因爲控制它計算它在選定的框中有多少個炸彈太多而且滯後。 MY MAP 我需要幫助,這些是我的控制,如果箱子裏面沒有炸彈。我正在尋找一種方法來減少它們。減少統計「KMines」遊戲中有多少枚炸彈的控件

if(mat_prato[y][x] == 0){ 

         if(y == 0 && x == 0){   //The four corners of the map 
          if(mat_prato[y][x + 1] == 9) 
           c = c + 1; 
          if(mat_prato[y + 1][x] == 9) 
           c = c + 1; 
          if(mat_prato[y + 1][x + 1] == 9) 
           c = c + 1; 
         } else if(y == 0 && x == 9){ 
          if(mat_prato[y][x - 1] == 9) 
           c = c + 1; 
          if(mat_prato[y + 1][x] == 9) 
           c = c + 1; 
          if(mat_prato[y + 1][x- 1] == 9) 
           c = c + 1; 
         } else if(y == 9 && x == 9){ 
          if(mat_prato[y][x - 1] == 9) 
           c = c + 1; 
          if(mat_prato[y - 1][x] == 9) 
           c = c + 1; 
          if(mat_prato[y - 1][x- 1] == 9) 
           c = c + 1; 
         } else if(y == 9 && x == 0){ 
          if(mat_prato[y][x + 1] == 9) 
           c = c + 1; 
          if(mat_prato[y - 1][x] == 9) 
           c = c + 1; 
          if(mat_prato[y - 1][x+ 1] == 9) 
           c = c + 1; 
         } else if(y == 0 && 0 < x && x < 9){  // The sides of the map 
          if(mat_prato[y][x - 1] == 9) 
           c = c + 1; 
          if(mat_prato[y + 1][x - 1] == 9) 
           c = c + 1; 
          if(mat_prato[y + 1][x] == 9) 
           c = c + 1; 
          if(mat_prato[y + 1][x + 1] == 9) 
           c = c + 1; 
          if(mat_prato[y][x + 1] == 9) 
           c = c + 1; 
         } else if(y == 9 && 0 < x && x < 9){ 
          if(mat_prato[y][x - 1] == 9) 
           c = c + 1; 
          if(mat_prato[y - 1][x - 1] == 9) 
           c = c + 1; 
          if(mat_prato[y - 1][x] == 9) 
           c = c + 1; 
          if(mat_prato[y - 1][x + 1] == 9) 
           c = c + 1; 
          if(mat_prato[y][x + 1] == 9) 
           c = c + 1; 
         } else if(x == 0 && 0 < y && y < 9){ 
          if(mat_prato[y - 1][x] == 9) 
           c = c + 1; 
          if(mat_prato[y - 1][x + 1] == 9) 
           c = c + 1; 
          if(mat_prato[y][x + 1] == 9) 
           c = c + 1; 
          if(mat_prato[y + 1][x + 1] == 9) 
           c = c + 1; 
          if(mat_prato[y + 1][x] == 9) 
           c = c + 1; 
         } else if(x == 9 && 0 < y && y < 9){ 
          if(mat_prato[y - 1][x] == 9) 
           c = c + 1; 
          if(mat_prato[y - 1][x - 1] == 9) 
           c = c + 1; 
          if(mat_prato[y][x - 1] == 9) 
           c = c + 1; 
          if(mat_prato[y + 1][x - 1] == 9) 
           c = c + 1; 
          if(mat_prato[y + 1][x] == 9) 
           c = c + 1; 
         } else if(0 < x && x < 9 && 0 < y && y < 9){  // The other cell in the center 
          if(mat_prato[y - 1][x - 1] == 9) 
           c = c + 1; 
          if(mat_prato[y - 1][x] == 9) 
           c = c + 1; 
          if(mat_prato[y - 1][x + 1] == 9) 
           c = c + 1; 
          if(mat_prato[y][x - 1] == 9) 
           c = c + 1; 
          if(mat_prato[y][x + 1] == 9) 
           c = c + 1; 
          if(mat_prato[y + 1][x - 1] == 9) 
           c = c + 1; 
          if(mat_prato[y + 1][x] == 9) 
           c = c + 1; 
          if(mat_prato[y + 1][x + 1] == 9) 
           c = c + 1; 
         } 

         if(c == 0){ 
          mat_prato[y][x] = 10; 
          mThumb[position] = R.drawable.empty; 
         } else if(c == 1){ 
          mat_prato[y][x] = 1; 
          mThumb[position] = R.drawable.one; 
         } else if(c == 2){ 
          mat_prato[y][x] = 2; 
          mThumb[position] = R.drawable.two; 
         } else if(c == 3){ 
          mat_prato[y][x] = 3; 
          mThumb[position] = R.drawable.three; 
         } else if(c == 4){ 
          mat_prato[y][x] = 4; 
          mThumb[position] = R.drawable.four; 
         } else if(c == 5){ 
          mat_prato[y][x] = 5; 
          mThumb[position] = R.drawable.five; 
         } else if(c == 6){ 
          mat_prato[y][x] = 6; 
          mThumb[position] = R.drawable.six; 
         } else if(c == 7){ 
          mat_prato[y][x] = 7; 
          mThumb[position] = R.drawable.seven; 
         } else if(c == 8){ 
          mat_prato[y][x] = 8; 
          mThumb[position] = R.drawable.eight; 
         } 
        } 
+0

使用for循環。此外,雖然代碼當然可以清理(通過使用循環),但循環將花費大致相同的時間 - 所以這不是緩慢的原因。 – tucuxi

+0

怎麼樣?擴展解決方案 –

回答

1

您應該使用循環遍歷單元格的鄰居而不是巨大的if語句。循環可能不會更快,但是沒有程序員會爲這個問題使用其他任何東西:它們非常簡單得多。

讓我們假設你有定義了這個方法:

// returns true only if bomb at x,y; and false for "outside" or "no bomb" 
private boolean isBomb(int x, int y) 

現在,您可以定義在使用該其他方法:

private int countBombsAround(int x, int y) { 
    int deltas[][] = { 
     {-1, -1}, {-1, 0}, {-1, 1}, 
     { 0, -1}, /*skip*/ { 0, 1}, 
     { 1, -1}, { 1, 0}, { 1, 1}}; 
    int bombs = 0; 
    for (int delta[] : deltas) { 
     if (isBomb(x+delta[1], y+delta[0]) bombs ++; 
    } 
    return bombs; 
} 

而且這種方法適用於所有有效(甚至無效!)x,y在遊戲中的位置。它也短得多,一旦你知道它有效(通過測試),你不需要再次改變它。雙贏!

請注意,上面的代碼不在中心廣場 - 你應該先通過isBomb進行檢查。此外,有許多方法可以編寫類似的循環。重要的一點是,如果你發現自己在程序中一遍又一遍地寫類似的東西,還有一個更好的方法來做到這一點

+0

謝謝,今天晚上我試着去解決它 –