简体   繁体   中英

Loop not Incrementing

So to give some context. I'm creating a little minesweeper problem that solves itself. I'm starting with an extremely basic algorithm at first, which is going to go through and when it sees a '-' character it's going to take a guess at the spot. Yes I know that is horrible, but I'm going to advance it soon.

But here is my issue.

input

public int[] aiMove()
{
    int[] move = new int[2];
    for(int a = 0; a<board.length-1; a++)
    {
        for(int b = 0; b<board.length-1; b++)
        {
            System.out.println(a +" and "+ b);
            System.out.println(a+"<"+(board.length-1));
            System.out.println(b+"<"+(board.length-1));



            if(board[a][b]=='-')
            {
                move[0]=a;
                move[1]=b;
                return move;
            }
            else
            {

                System.out.println("No Moves left");
            }
        }
    }
    return move;
}

For some reason it doesn't want to increment the a and b values.

It's breaking into an infinite loop, so I did a quick check with those print statements in the code.

And this is what was returned.

 0<7
0 and 0
0<7
0<7
0 and 0
0<7
0<7
0 and 0
0<7
0<7
0 and 0
0<7
0<7
0 and 0
0<7
0<7
0 and 0
0<7
0<7
0 and 0
0<7
0<7

Every time this method is called it goes through this:

       if(board[a][b]=='-')
        {
            move[0]=a;
            move[1]=b;
            return move;

and aiMove must be being called repeatedly from another method.

Your method does not call No Moves left , therefore in this part of code

        if(board[a][b]=='-')
        {
            move[0]=a;
            move[1]=b;
            return move;
        }
        else
        {

            System.out.println("No Moves left");
        }

Everytime these three lines are executed and by returning "move" the method ends

            move[0]=a;
            move[1]=b;
            return move;

Because your board[0][0] is equal to - , even when you call it several times, it always ends in first cycle, therefore it does not cycle anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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