你們中的許多人都知道計算機的名爲「KMines」的遊戲,我使用Android Studio爲手機創建了應用程序。 遊戲的作品,但不是很好,因爲控制它計算它在選定的框中有多少個炸彈太多而且滯後。 我需要幫助,這些是我的控制,如果箱子裏面沒有炸彈。我正在尋找一種方法來減少它們。減少統計「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;
}
}
使用for循環。此外,雖然代碼當然可以清理(通過使用循環),但循環將花費大致相同的時間 - 所以這不是緩慢的原因。 – tucuxi
怎麼樣?擴展解決方案 –