0

This is a portion of my code for a Battleship game. The looping doesn't stop when I sink all of my ships. I take the input for the shoot array from player and the compshoot array from the random function.

      do {
        System.out.println();
        showBoard(board);
        shoot(shoot);
        System.out.println();
        if (board[shoot[0]][shoot[1]]==1 || board[shoot[0]][shoot[1]]==2) {
            if (board[shoot[0]][shoot[1]]==1){
                System.out.println("Oh no, you sunk your own ship :( ");
                myShip--;
                board[shoot[0]][shoot[1]]=3;
            }
            else if (board[shoot[0]][shoot[1]]==2) {
                System.out.println("Boom! You sunk a ship!");
                compShip--;
                board[shoot[0]][shoot[1]]=4;
            }
        }
        else if (board[shoot[0]][shoot[1]]==0) {
            System.out.println("Sorry, you missed");
            board[shoot[0]][shoot[1]] = 5;
        }
        compShoot(compShoot, shoot);
        System.out.println();
        System.out.println("Computers turn : ");
        System.out.println();
        if (board[compShoot[0]][compShoot[1]]==1 || board[compShoot[0]] 
        [compShoot[1]]==2) {
            if (board[compShoot[0]][compShoot[1]]==1){
                System.out.println("The Computer sunk one of your ships!");
                myShip--;
                board[compShoot[0]][compShoot[1]]=3;
            }
            else if (board[compShoot[0]][compShoot[1]]==2) {
                System.out.println("The Computer sunk one of its own 
                ships");
                compShip--;
                board[compShoot[0]][compShoot[1]]=4;
            }
        }
        else if (board[compShoot[0]][compShoot[1]]==0) {
            System.out.println("Computer missed");
            board[compShoot[0]][compShoot[1]] = 5;
        }
        System.out.println();
        System.out.println("Your ships : " + myShip + " | Computer ships : " 
        + compShip);
    }while (myShip != 0 || compShip != 0);
  • Welcome to Stack Overflow! Please read "How to create a [mcve]". Then use the [edit] link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you. – GhostCat Sep 17 '18 at 08:46
  • Your while says "exit if both myship and compship =0 – Dragonthoughts Sep 17 '18 at 08:47
  • 1
    We would need much more information to help here, like other parts of code, and input data ... but Jack is fully correct: what it takes here is **debugging**. Sitting down (maybe for hours) to understand what is happening where in your code. Learning programming is just that: sitting down for hours, banging your head against the wall to figure: *what is the computer doing*? If you delegate this experience, you also delegate the **learning** to others. – GhostCat Sep 17 '18 at 08:48

1 Answers1

0

Your current condition keeps you inside the loop if myShip != 0 OR compShip != 0.

You need to stay in the loop only if both myShip AND compShip are not 0:

do {

} while (myShip != 0 && compShip != 0);
Eran
  • 387,369
  • 54
  • 702
  • 768
  • 1
    There's a pretty well-established dupetarget for this. – T.J. Crowder Sep 17 '18 at 08:48
  • @T.J.Crowder Naw, I didn't know about that either. So it can't be *well established* ;-) – GhostCat Sep 17 '18 at 08:49
  • @T.J.Crowder `Why non-equality check of one variable against many values always returns true?` - but this question doesn't check one variable against many values. It checks two different variables against a single value, which doesn't always return true. – Eran Sep 17 '18 at 08:53
  • Same fundamental issue. – T.J. Crowder Sep 17 '18 at 09:17