简体   繁体   English

应用重力:2D数组

[英]Applying Gravity : 2D arrays

My program consists of a grid composed by a 2D array. 我的程序由2D数组组成的网格组成。 The user input deletes certain elements of the grid, leaving blank spaces. 用户输入将删除网格的某些元素,并保留空白。 My issue is making the elements above the blank spaces drop down. 我的问题是使空格上方的元素下降。

Originaly I had an array to check if a space is blank, and if is was blank, the element above would be deleted and recreated where there used to be a blank. 最初,我有一个数组来检查空格是否为空白,如果为空白,则上面的元素将被删除并在以前为空白的地方重新创建。

My problem is, the array I used to do this was Left->Right, Top->Down (i++, j++), this leaves me with the problem of having to repeat the whole code to get newly built blank spaces. 我的问题是,我用来执行此操作的数组是Left-> Right,Top-> Down(i ++,j ++),这给我留下了必须重复整个代码才能获得新生成的空格的问题。 (For example, if I delete something from the 3rd row, the 2nd row would be blank, but I'd have checked the 2nd row already. (例如,如果我从第三行中删除了某些内容,那么第二行将为空白,但是我已经检查了第二行。

Since it would be very uneffective (including overstack errors) I decided to do the reserve, bottom->top, right->left array (i--,j--), my issue with this is that I'm getting an array out of bounds error, even though I made sure its not possible for it to go out of bounds. 由于这将是非常无效的(包括堆栈错误),因此我决定进行保留,底部->顶部,右侧->左侧数组(i-,j--),所以我遇到的问题是要获取数组超出范围错误,即使我确保它不可能超出范围也是如此。

Here is the piece of code which is giving problems 这是一段有问题的代码

public static void dropBall(){
 for (i =Settings.row-1;i>=0;i--){
  for (j =Settings.col-1;i>=0 ; j--){
   if (i <0||j<0)break;
   if (Settings.grid[i+1][j]==666){ 
    //checking if the space below has the 666 ID (666 ID equals to blank)                   
    Settings.grid[i+1][j]=Settings.grid[i][j]; //Deleting the current blank                 
   }
  }
 }
}

Note: Settings.row and Settings.col are similar to something.lenght , meaning they have the lenght of the grid, even though the grid starts at 0. 注意: Settings.row and Settings.colsomething.lenght相似, something.lenght意味着它们具有网格的长度,即使网格从0开始。

How can I avoid the outofBounds error in this situation? 在这种情况下,如何避免outofBounds错误?

  • On line3: 在第3行:

      for (j =Settings.col-1;i>=0 ; j--){ 

change to: 改成:

                for (j =Settings.col-1;j>=0 ; j--){
  • and you can remove the if line after. 然后可以删除if行。

  • and start your i on Settings.row-2 instead 然后在Settings.row-2上启动i

  • try this: 尝试这个:

      public static void dropBall(){ for (i =Settings.row-1;i>=0;i--){ for (j =Settings.col-1;j>=0 ; j--){ if (Settings.grid[i+1][j]==666){ //checking if the space below has the 666 ID (666 ID equals to blank) Settings.grid[i+1][j]=Settings.grid[i][j]; //Deleting the current blank Settings.grid[i][j] = 666; } } } } 
  1. Start from the second row at the bottom, and not from the first (since there is no row below it): 从底部的第二行开始,而不是从第一行开始(因为它下面没有行):

      for (i = Settings.row-2 ... // instead of row-1 
  2. Remove this line (it is redundant): 删除此行(这是多余的):

      if (i <0||j<0)break; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM