如何在Java程序中找到无限递归错误?

时间:2016-12-06 20:18:26

标签: java recursion infinite

struct Astruct & test::MethodOne(){
    Astruct testStruct;
    return testStruct;            // testStruct is destroyed here
}                                 // and the returned ref is invalid

显然,我在递归期间无限地调用方法check_move。我无法找出为什么会无限期地进行。这是错误消息:

    import java.lang.reflect.Array;
    import java.util.*;


    public class TMS {

        private static int row = 5, col = 5;
        private static String[][] board = new String[row][col];
        private static String[][] board_copy = new String[row][col];

        public static void main(String[] args) {
            for(String[] array:board) {
                Arrays.fill(array, "_");
            }
            create_mines();
            Scanner input = new Scanner(System.in);
            int inpx = 0, inpy;

            while(inpx != 69){
                show_board();
                inpx = input.nextInt();
                inpy = input.nextInt();
                if(board_copy[inpx][inpy] == "*"){
                    System.out.println("YOU LOOSE");
                    break;
                }
                check_move(inpx, inpy);
            }
        }

        public static void show_board() {
            for(String[] row: board){
                for(String element: row){
                    System.out.print(element+"\t");
                }System.out.println();
            }
        }

        public static void create_mines() {
            Random rand = new Random();
            rand.nextInt();

            for(String[] array:board_copy) {
                Arrays.fill(array, "_");
            }
            board_copy[1][1] = "*";
            board_copy[3][1] = "*";
            board_copy[3][3] = "*";
            board_copy[2][4] = "*";
        }

        public static void check_move(int posx, int posy){
            int mines = 0;
            if(posx-1 >= 0 && posy-1 >= 0)
                mines = (board_copy[posx-1][posy-1] == "*")? (mines+1):mines;
            if(posx >= 0 && posy-1 >= 0)
                mines = (board_copy[posx][posy-1] == "*")? (mines+1):mines;
            if(posx+1 < row && posy+1 < col)
                mines = (board_copy[posx+1][posy+1] == "*")? (mines+1):mines;
            if(posx-1 >= 0 && posy >= 0)
                mines = (board_copy[posx-1][posy] == "*")? (mines+1):mines;
            if(posx+1 < row && posy >= 0)
                mines = (board_copy[posx+1][posy] == "*")? (mines+1):mines;
            if(posx >= 0 && posy+1 < col)
                mines = (board_copy[posx][posy+1] == "*")? (mines+1):mines;
            if(posx-1 >= 0 && posy+1 < col)
                mines = (board_copy[posx-1][posy+1] == "*")? (mines+1):mines;
            if(posx+1 < row && posy-1 >= 0)
                mines = (board_copy[posx+1][posy-1] == "*")? (mines+1):mines;
            board[posx][posy] = Integer.toString(mines);

            if(mines == 0){
                if((posx-1) >= 0 && (posy-1) >= 0) {
                    System.out.println((posx-1)+" "+(posy-1));
                    check_move((posx - 1), (posy - 1));
                }
                if(posx >= 0 && (posy-1) >= 0) {
                    System.out.println((posx)+" "+(posy-1));
                    check_move(posx, (posy - 1));
                }
                if((posx+1) < row && (posy+1) < col) {
                    System.out.println((posx+1)+" "+(posy+1));
                    check_move((posx + 1), (posy + 1));
                }
                if((posx-1) >= 0 && posy >= 0) {
                    System.out.println((posx-1)+" "+(posy));
                    check_move((posx - 1), posy);
                }
                if((posx+1) < row && posy >= 0) {
                    System.out.println((posx+1)+" "+(posy));
                    check_move((posx + 1), posy);
                }
                if(posx >= 0 && (posy+1) < col) {
                    System.out.println((posx)+" "+(posy+1));
                    check_move(posx, (posy + 1));
                }
                if((posx-1) >= 0 && (posy+1) < col) {
                    System.out.println((posx-1)+" "+(posy+1));
                    check_move((posx - 1), (posy + 1));
                }
                if((posx+1) < row && (posy-1) >= 0) {
                    System.out.println((posx+1)+" "+(posy-1));
                    check_move((posx + 1), (posy - 1));
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

例如,如果您有两个没有地雷的相邻单元格,则递归不会结束。例如,如果在某个位置(posx,posy)没有我的,你可以在行中的(posx-1,posy-1)调用check_move

  if((posx-1) >= 0 && (posy-1) >= 0) {
      System.out.println((posx-1)+" "+(posy-1));
      check_move((posx - 1), (posy - 1));

现在,如果(posx-1,posy-1)也没有地雷,你将在行中的(posx,posy)调用check_move

if((posx+1) < row && (posy+1) < col) {
    System.out.println((posx+1)+" "+(posy+1));
    check_move((posx + 1), (posy + 1));

这将永远持续下去。您需要找到在所有情况下结束递归的方法。例如,您可以选中&#39;&#39;每个单元格上的标志阻止您在已经测试的位置重新调用check_move(可能还有其他方式......)。

正如安德烈亚斯指出的那样,你的inpx = 69不会产生预期的效果,因为你的例程会在它回到while循环测试之前调用异常。