0

I am writing a battleship program. Right now I am testing a couple lines of code to see if it will place the boat going in the up direction. How my program is set up is that if, for example, the user clicks on the aircraft carrier button to set his aircraft carrier, the program should also set the ai's aircraft carrier. The boats are placed on a button array, called tlba. aifirstclicki is set by a random generator so that it will choose a random row. aifirstclickj chooses a random column, in conjunction the two pinpoint a spot on the button array (which is 10x10). I wrote the following code to try to make it so that if the program has an outofboundsexception error,or in other words if the program chooses a first spot that will eventually cause an outofbounds exception error because the for loop will keep adding spots until aiclickcount = 5, it should start over and pick a different spot until it finds a spot that will allow it to place all 5 spots. I keep getting stuck in an infinite loop though.

    int aiclickcount = 0;
    while (directiondecider == 0)
    {//up
        aifirstclicki = generator.nextInt(10);
        aifirstclickj = generator.nextInt(10);
        while (aifirstclicki != 3 &&
               aifirstclicki != 2 &&
               aifirstclicki != 1 &&
               aifirstclicki != 0)
        {
            for(int k=0; k<shiplength; k++)
            {
                tlba[aifirstclicki - k][aifirstclickj].setBackground(Color.RED);
                aistringarray[aifirstclicki - k][aifirstclickj] = "aircraftcarrier";
                aioccupied2d[aifirstclicki - k][aifirstclickj] = true;
                aiclickcount++;
            }

            if (aiclickcount == 5)
            {
                shipset = true;
                break;
            } 
        }
        System.out.println(shipset);
    }

Does anyone know what's wrong or have a different solution to my problem?

James
  • 9,064
  • 3
  • 31
  • 49
  • Did you try stepping through in a debugger? – Brian Rogers Oct 14 '12 at 22:53
  • Your code is very poorly made - firstly make it easy to read and then go through debugger. Why do you generate random number twice? Why do you check four conditions instead of just one inequality? Just generating nextint(6)+4 will be equivalent to this all. – sashkello Oct 14 '12 at 22:58
  • i don't generate it twice. It's only supposed to generate it once and only should generate it again if the boat goes out of bounds –  Oct 15 '12 at 00:45

3 Answers3

0

You never have aiclickcount == 5 if your shiplength is not 5. Put if into your for loop. You don't need the second while at all, you don't break out of it as well. Just generate number greater than 3 by nextInt(6) + 4.

sashkello
  • 17,306
  • 24
  • 81
  • 109
  • the shiplength is 5. It is just not listed in this snippet of code. and secondly do you mean change the inner while or the outer while? –  Oct 14 '12 at 22:52
  • Edited post. As you write, it will be adding spots until aiclickcount == 5. If spots are added in your for loop, then if should be inside it, not outside. – sashkello Oct 14 '12 at 22:55
  • I just moved the break outside the for loop. Thanks so much for your help. –  Oct 15 '12 at 01:27
0

Your code does not tell us, which value the variable shiplength has. If it's 0 the for-loop will never be entered thus aiclickcount will remain 0 and your break statement is never reached (under the premise that the random value of aifirstclicki is greater than 3). Try to step through your code with a debugger and let it display the values for the variables to you to find out what's going on.

Joachim Rohde
  • 5,915
  • 2
  • 29
  • 46
  • the shiplength is 5. It is initialized above this snippet. Secondly, aifirstclicki should be greater than 3, but I want the program to run that if it is 3 or less it should start over and try to find a new spot. –  Oct 14 '12 at 22:54
0

Your break; is only going to get you out of the second while loop, not the first as it only works on the inner-most loop that it is part of.

Java allows you to specify multi-level breaks, rather than having to complicate your loop conditions: Breaking out of nested loops in Java

Community
  • 1
  • 1
gwaigh
  • 1,182
  • 1
  • 11
  • 23
  • So how would I make it so that it breaks out of the inner loop when click count =5? –  Oct 15 '12 at 00:56
  • Read the answer to [Breaking out of nested loops in Java](http://stackoverflow.com/questions/886955/breaking-out-of-nested-loops-in-java), it shows how to add a label to the outer loop that the `break;` statement can reference. – gwaigh Oct 15 '12 at 01:31