Home > Enterprise >  Do-While loop doesn't terminate when condition is false
Do-While loop doesn't terminate when condition is false

Time:11-07

This is a game where you can take biscuits from barrels, either from barrel1, barrel2, or both. The last player to take the last biscuits wins the game. I implemented the game in a do-while loop so that it loops every turn. However, once the number of biscuits in both barrels = 0, the loop doesn't terminate and keeps on taking scanner input.

N.B. This is coursework for university, so please do not tell me exactly what to do or give me exact code, just suggestions or why my code is not working.

import java.util.Scanner;

public class LastBiscuit {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int barrel1 = 6;
        int barrel2 = 8;

        // Simple turn counter. incremented every loop. if even, player 2 turn
        int turnCounter = 0;

        do {
            turnCounter  ;
            int howMany = 20;
            String turnAction;
            // prints out biscuits left in each barrel
            String output1 = String.format("Biscuits Left - Barrel 1: %d",barrel1);
            String output2 = String.format("Biscuits Left - Barrel 2: %d",barrel2);
            System.out.println(output1);
            System.out.println(output2);

            // Check turn counters value. If even, it is player 2 turn, else player 1 turn.
            if (turnCounter % 2 == 0) {
                System.out.println("Player Turn: 2");
            }
            else {
                System.out.println("Player Turn: 1");
            }
            
            // Player picks what action to take in their turn. Stored in turnAction. Only allows correct input
            System.out.print("Choose a barrel: barrel1 (one), barrel2 (two), or both (both), or skip turn (skip)?");
            do {
            turnAction = in.next();
            } while (!turnAction.equalsIgnoreCase("one") && !turnAction.equalsIgnoreCase("two") &&
                !turnAction.equalsIgnoreCase("both") && !turnAction.equalsIgnoreCase("skip"));

            // Player picks how many biscuits to take, if at all. If biscuits taken larger than biscuits remaining,
            // they have to re-enter integer
            if (!(turnAction.equalsIgnoreCase("skip"))) {
                System.out.print(" How many biscuits are you taking?");
                while(!in.hasNextInt()) {
                    System.out.println("Try again");
                    in.next();
                }
                howMany = in.nextInt();
                while (barrel1 - howMany < 0 || barrel2 - howMany < 0 || howMany <= 0) {
                    howMany = in.nextInt();

                }
            }

            // Takes biscuits from barrels chosen
            if (turnAction.equalsIgnoreCase("one")) {
                barrel1 -= howMany;
            }
            if (turnAction.equalsIgnoreCase("two")) {
                barrel2 -= howMany;
            }
            if (turnAction.equalsIgnoreCase("both")) {
                barrel1 -= howMany;
                barrel2 -= howMany;
            }
            // do nothing on skip


        } while (barrel1 > 0 || barrel2 > 0);

        //bug? doesnt print? outside of do-while loop
        // doesnt exit loop?
        System.out.println("YOYYYOOYOY");

        if (turnCounter % 2 == 0) {
            System.out.println("Player 2 Wins! ");
        }
        else {
            System.out.println("Player 1 Wins! ");
        }
    }
}

CodePudding user response:

You have an infinite while loop running at:

while (barrel1 - howMany < 0 || barrel2 - howMany < 0 || howMany <= 0) {
    howMany = in.nextInt();
}

This only happens when you require more biscuits than a barrel contains or you request a negative number of biscuits. When it does you will be forever in this while loop. Place a breakpoint at howMany = in.nextInt(); as already suggested and you will see it happening.

  •  Tags:  
  • java
  • Related