2013-04-01 30 views
1

我正在構建俄羅斯方塊,並試圖實現一種方法,它通過tile [] []迭代並檢查每行的每列,從底部開始,一直向上(figured如果我從底部開始,那麼這將是一個更快的檢查,因爲這是大多數行需要清除的地方)。Java俄羅斯方塊 - 迭代通過雙數組

我對此的理解是 - 爲每一行創建一個double for循環,檢查該行中的所有列是否已滿(非空)。如果是,我會實現一個清晰的(這實際上是設置當前行=上面的行)。目前,我只是想輸出「Full」。

System.out.println(grid[row][col] + ", " + row + ", " + col);檢查正確顯示,它開始在底行,然後再遍歷每個列...但if (grid[row][col] != null) {檢查似乎並沒有被留在該行...

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 
+2

什麼是電流輸出?你確定要從'row = grid.length -2'開始而不是'row = grid.length - 1'嗎? – nattyddubbs

+0

放一個System.out。在調用顯示行的值的第一個for循環之後的println。 – bakoyaro

+0

@bakoyaro'grid = new Tile [width] [height];'Tile只是一個類,其寬度和高度由Board.java確定 - 在用paintComponent填充行時,我填充每個tile,例如:'g.fillRect (row * tilesize,col * tilesize,tilesize,tilesize);' – Growler

回答

4

幾個問題在這裏:

  1. 因爲它的假設是隻有當前排的檢查你應該將int columnsFilled = 0外爲內循環。就像現在一樣,在經過第一行之後,計數將不正確。
  2. 您正在檢查if (columnsFilled == grid.length-1),實際上這應該是if (columnsFilled == grid[row].length-1)。您的檢查將填充列的數量與行數進行比較,而不是針對給定行中的列數。
+0

我更新了我的帖子。仍然有輸出問題。有什麼想法嗎?謝謝! – Growler

0
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");  
      } 
     } 
    } 
} 
2

,可以考慮添加類似的便利功能

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--) { 
+0

你最後一點的兩個循環版本有不同的效果。如果OP意圖忽略第一行和最後一行,則不能也不應該進行重寫。 –

+0

@PatriciaShanahan,那是真的。如果意圖是忽略第一行和最後一行,那麼它被正確寫入。但是,我的猜測是,這不是他們的意圖。 – jedwards

+0

@jedwards我喜歡你分離的想法。它仍然沒有正確迭代特定行中的列,因爲我用所有非空值填充了行,但isFull不返回true。 – Growler

1

有在你發佈的代碼中有幾個邏輯錯誤,附加的代碼修正了它們而沒有實現everythi爲你。

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

}

+0

此實現似乎不起作用。 – Growler

+0

@Growler請更具體 – bakoyaro