java - 嵌套 for 循环中的逻辑问题

标签 java for-loop logic minesweeper

Java programming - nested for loops for minesweeper game密切相关,我的扫雷程序设计为循环遍历单元格,检查每个相邻单元格,执行逻辑测试以检查它是否是地雷,然后跳转到下一个单元格。然而,当我运行它时,它不知何故变成了无限循环。我尝试过更改变量、反转符号(< 变为 > 且 + 变为 -)以及谷歌搜索其他解决方案,但我找不到任何内容。

打印输出用于调试,游戏板设置为[10][10],

public static void assignNumbers(int[][] gameBoard)
   {

      for(int r = 0; r <= (gameBoard.length - 1); r++){ //row
         for(int c = 0; c <= (gameBoard[0].length - 1); r++){ //column

            System.out.print("New Cell   ");

            for(int vR = r+1; vR > r-2; vR --){ //vR is visiting Row
               for(int vC = c+1; vC > c-2; vC --){ //vC is visiting Column

                  System.out.print("new item  ");
                  if (isValid(vR, vC, gameBoard)){

                     System.out.print("isMine?  "); 
                     if (isMine(vR, vC, gameBoard)){ 
                        gameBoard[r][c] += 1;
                        System.out.print("   MINE   "); 
                     } 
                     else {
                        System.out.print("   NO     ");
                     }
                  }               
               }
            }  
            System.out.println();          
         }         
      }
   }

public static boolean isMine(int r, int c, int[][] gameBoard){
      if(gameBoard[r][c] != 100){
         return false;
      }
      else{
         return true;
      }
}

public static boolean isValid(int r, int c, int[][] gameBoard) 
   { 
       // Returns true if row number and column number 
       // is in range 
      return ((r >= 0) && (r < SIDE)) && ((c >= 0) && (c < SIDE)) && !isMine(r, c, gameBoard); 
   } 

当我尝试运行它时,我得到一个无限的打印输出: “新细胞”之后是 9 个“新项目”。这个应该只打印100次(每个单元格打印一次),但是它在100次之后不会停止。我认为这是其中一个for循环中的逻辑错误,但我找不到它为了我的一生。感谢您的帮助,我会尽力回答任何问题。

编辑:标点符号

最佳答案

尝试用c++替代r++

for(int c = 0; c <= (gameBoard[0].length - 1); c++){ //column

关于java - 嵌套 for 循环中的逻辑问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59989659/

相关文章:

python - 不进入python 2.7.2中的for循环

c - 使用 C 语言控制指令练习解决的问题

R within() 操作顺序和逻辑

iOS:消息 'frame' 的接收者为零

java - CraftingManager 错误

java - IndexOutOfBoundsException:索引:1,大小:1

swift - 如何在swift中的for循环中使用异步任务

python - 使用 Pandas 数据帧时如何避免慢速 for() : loops,?

java - 使用java将xml转换为xsd

java - 与 Android 中的 GLThread 同步