-1

I have looked at other do while issues on StackOverflow, but I could not find the solution to my issue. I have variables initialized outside the do{ and they are being use within, but when the variables reach a certain value, the while method does not jump out.

Here is what I have:

int aiShotHit = 0;
int shotHit = 0;

do{
  showBoard(board);
  bAi.showAi(ai);
  shoot(shoot,ships);
  bAi.aiHit(aiShoot);
  attempts++;

  if(hit(shoot,ships)){
    hint(shoot,ships,aiShips,attempts);
    shotHit++;
    System.out.println("\nShips Left on the Board: " + shotHit);
  }                
  else
    hint(shoot,ships,aiShips,attempts);

  changeboard(shoot,ships,board);


  if(bAi.aiHit(aiShoot,aiShips)){
    hint(shoot,ships,aiShips,attempts);
    aiShotHit++;
  }                
  else
    System.out.print("");

  bAi.changeAi(aiShoot,aiShips,ai);

}while(shotHit !=3 || aiShotHit !=3);

if (shotHit == 3){
  System.out.println("\n\n\nBattleship Java game finished! You Beat the Computer");

} 
System.out.print("You lost! The Ai beat you");
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
SkyToken
  • 23
  • 5
  • 3
    What are the values of both variables when you expect it to exit the loop but doesn't? Use a debugger, or just add some print statements, to figure that out. – Kevin Workman May 13 '14 at 18:02
  • 2
    You might be interested in [this java debugging tutorial](http://keysersblog.wordpress.com/2014/04/21/debugging-java-code-a-beginners-guide/) I wrote. Especially the part on debugging by printing. – keyser May 13 '14 at 18:02
  • 4
    purely conjecture, but I think you probably want to loop while `shotHit !=3 && aiShotHit !=3` AND not OR – James Montagne May 13 '14 at 18:03
  • 1
    As written, this loop will continue until both conditions shotHit == 3 and aiShotHit == 3 are true. – Eric Woodruff May 13 '14 at 18:03
  • will i have added print statements, and `shotHit` adds up to 3. But i am guessing that the initialized variables still remain 0. – SkyToken May 13 '14 at 18:07
  • What initialized variables are you referring to? And why are you guessing :p – keyser May 13 '14 at 18:08
  • @JamesMontagne I think you're right too. – Jesan Fafon May 13 '14 at 18:08
  • the initialized variables at the he top of the do while statement. `shotHit` and `aiShotHit`. – SkyToken May 13 '14 at 18:10
  • Verify that shotHit++ and aiShotHit++ are happening when you expect them to. Also verify your while condition (as others have said). – Edwin Torres May 13 '14 at 18:12
  • Ok. Thank you James and others. Your help was appreciated. – SkyToken May 13 '14 at 18:18

1 Answers1

1

You probably started out by saying, I want this to loop until shotHit is 3 or until aiHShotHit is 3. That would be

while (!(shotHit == 3 || aiShotHit == 3));

which is "loop while it is not the case that either shotHit or aiShotHit contains the value 3", but it's kind of ugly so you wanted to apply the negation operator to each subexpression and get rid of some parens. The mistake was thinking you can move the negation operator without changing anything else to get

while (shotHit != 3 || aiShotHit != 3);

This exits the loop only in the event that shotHit is 3 at the same time that aiShotHit is 3. Not what you want.

The correct transformation is

while (shotHit != 3 && aiShotHit != 3);

This much was covered in the comments. The guidelines for how to safely transform this kind of expression are De Morgan's rules, which describe how to transform conjunctions and disjunctions in terms of each other. Following those rules lets you can move the negation operator and change the parenthesization without changing the meaning of the expression:

"not (A or B)" is the same as "(not A) and (not B)"
"not (A and B)" is the same as "(not A) or (not B)"

Needing to reorganize an expression to make it more readable comes up a lot in programming and this is a tool you need in order to do it safely. If you want to know more about De Morgan's rules you might want to read this answer.

Community
  • 1
  • 1
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276