[英]Java Tetris - iterating through double array
我正在構建tetris,並嘗試實現一種方法,該方法可通過tile的grid [] []進行迭代並從底部開始檢查每行的每一列,然后逐步向上移動(圖如果我開始,則會更快從底部開始,因為這是需要清除大多數行的位置)。
我對此的理解是-為每個行創建一個double for循環,檢查該行中的所有列是否已滿(不為null)。 如果是這樣,我將實現一個清除方法(實際上是將當前行設置為上面的行)。 現在,我只是想輸出“ Full”。
我的System.out.println(grid[row][col] + ", " + row + ", " + col);
check正確顯示它從底部開始,然后迭代每一列...但是if (grid[row][col] != null) {
check似乎不停留在該行...
public void checkBottomFull() {
int columnsFilled = 0;
//loops through rows only after each column has finished
for(int row = grid.length-2; row > 0; row--) {
//loops through all columns per row, then row decrements
for(int col = 0; col < grid[row].length; col++) {
System.out.println(grid[row][col] + ", " + row + ", " + col);
if (grid[row][col] != null) {
columnsFilled++;
if (columnsFilled == grid.length-1) {
System.out.println("full");
}
}
}
}
}
有什么想法嗎?
編輯
public void checkBottomFull() {
for(int row = grid.length-1; row >= 0; row--) {
//loops through columns of each individual row
if (isFull(row)) {
System.out.println("full");
clearRow(row);
}
}
}
public boolean isFull(int row) {
for (int col = 0; col < grid[row].length-1; col++) {
if(grid[row][col] == null) {
System.out.println(grid[row][col] + "... " + row + ", " + col);
return false;
}
}
return true;
}
public void clearRow(int row) {
for (int col = 0; col < grid[row].length-1; col++) {
System.out.println("clearing...");
grid[row][col] = grid[row-1][col];
}
}
System.out.println(grid[row][col] + "... " + row + ", " + col);
輸出:為什么列不遞增?
null... 9, 0
null... 8, 0
null... 7, 0
null... 6, 0
null... 5, 0
null... 4, 0
null... 3, 0
null... 2, 0
null... 1, 0
null... 0, 0
這里的幾個問題:
int columnsFilled = 0
移到外部for循環內,因為它僅是對當前行的檢查。 現在,經過第一行后,計數將不正確。 if (columnsFilled == grid.length-1)
實際上應該是if (columnsFilled == grid[row].length-1)
。 您的檢查將填充的列數與行數進行比較,而不是與給定行中的列數進行比較。 您可以考慮添加類似於以下內容的便利功能
public boolean isFullRow(Tile[][] grid, int row)
{
for(int i=0; i<grid[row].length; i++)
{
if(grid[row][i] == null){ return false; }
}
return true;
}
這將有助於調試您的代碼,並使其(略微但不明顯)更快。
然后,您的checkButtomFull()
函數可能看起來像
public void checkBottomFull() {
//loops through rows only after each column has finished
for(int row = grid.length-2; row > 0; row--) {
if(isFullRow(grid, row)){
// Do something if full row
}
}
}
最后,作為次要點,我的猜測是
for(int row = grid.length-2; row > 0; row--)
可以/應該寫成
for(int row = grid.length-1; row >= 0; row--) {
您發布的代碼中存在一些邏輯錯誤,附件的代碼修復了這些錯誤,而沒有為您實現所有功能。
public class SampleClass {
static String[][] grid = new String[4][10];
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
loadGrid();
checkBottomFull();
}
public static void checkBottomFull() {
int columnsFilled = 0;
//loops through rows only after each column has finished
for(int row = grid.length-1; row >= 0; row--) {//needed 'greater than or equal to'
columnsFilled=0; //need to reinitialize this before every iteration
for(int col = 0; col <= grid[row].length-1; col++) { //needed 'less than or equal to'
System.out.println(row + ", " + col+", "+grid[row][col]);
if (grid[row][col] != null) {
columnsFilled++;
}
}
System.out.println("columns that have a character" + columnsFilled);
System.out.println("Needs to be "+grid[row].length+" to remove row");
if (columnsFilled == grid[row].length) {
System.out.println("full"); //this is where you should remove a row
}
}
}
private static void loadGrid() {
grid[0] = new String[] {"X","X","X","X","X","X","X","X","X","X"};
grid[1] = new String[] {"X","X",null,"X","X","X",null,"X","X","X"};
grid[2] = new String[] {"X","X","X",null,"X","X",null,"X","X","X"};
grid[3] = new String[] {"X","X",null,"X","X","X",null,"X","X","X"};
}
}
public static void checkBottomFull() {
//loops through rows only after each column has finished
for(int row = grid.length-1; row >= 0; row--) {
//loops through all columns per row, then row decrements
int columnsFilled = 0;
for(int col = 0; col < grid[row].length; col++) {
System.out.println(grid[row][col] + ", " + row + ", " + col);
if (grid[row][col] != null && ++columnsFilled == grid[row].length) {
System.out.println("full");
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.