/*********************** * Loopy - practice with loops * * @author Shawn Brenneman (modified by ibarland to use methods and no I/O) * @date 2019-Sep-25 ***********************/ public class LoopyAfter extends Object120 { /** Return the number of rolls of 2d6 until we rolled (1,1). */ static int rollsTilGet( int targetSum ) { int die1; int die2; // two loop vars: boolean gotTarget = false; // a loop-control variable int rollsSoFar = 0; // accumulator var (the pertinent info accumulated from all previous iterations) while (!gotTarget) { die1 = randomInt(6) + 1; die2 = randomInt(6) + 1; //System.out.println("Debug: rolled " + die1 + "," + die2); rollsSoFar = rollsSoFar + 1; // update accumulator var gotTarget = (die1+die2 == targetSum); // update loop-control var } return rollsSoFar; } static final int MAX_VAL = 100; /** Have a human play the hi-lo guessing game. * @return the number of guesses the human took. */ static int hiloHumanIO() { int target = randomInt(MAX_VAL); int guess; // We'll read input from the keyboard ("System.in"). int guessesSoFar = 0; // accumulator System.out.println( "debug: target is " + target ); System.out.print( "Guess a number in [0," + MAX_VAL + "): " ); guess = nextInt(); // Btw: func's full name is: Object120.nextInt() guessesSoFar = guessesSoFar + 1; while (guess != target) { // (no explicit loop-control var) String direction; if (guess > target) { direction = "high"; } else { direction = "low"; } System.out.print("Too " + direction + ". "); System.out.print("Guess again: "); guess = nextInt(); // update loop-condition guessesSoFar = guessesSoFar + 1; // update accumulator } System.out.println( "You got it in " + guessesSoFar + " guesses." ); return guessesSoFar; } /* Not actually unit-tests -- we can't use `assertEquals`, because the expected-outcome isn't known. */ public static void testAll() { System.out.println( "==== testing all" ); System.out.println( "#1: It took " + toString(rollsTilGet(2)) + " rolls to get snakeeyes." ); System.out.println( "#2: It took " + toString(rollsTilGet(2)) + " rolls to get snakeeyes." ); System.out.println( "#3: It took " + toString(rollsTilGet(2)) + " rolls to get snakeeyes." ); System.out.println( "#4: It took " + toString(rollsTilGet(2)) + " rolls to get snakeeyes." ); System.out.println( "" ); System.out.println( "#1: It took " + toString(rollsTilGet(3)) + " rolls to get acey-deucy." ); System.out.println( "#2: It took " + toString(rollsTilGet(3)) + " rolls to get acey-deucy." ); System.out.println( "#3: It took " + toString(rollsTilGet(3)) + " rolls to get acey-deucy." ); System.out.println( "#4: It took " + toString(rollsTilGet(3)) + " rolls to get acey-deucy." ); System.out.println( "" ); System.out.println( "#1: It took " + toString(rollsTilGet(7)) + " rolls to get 7." ); System.out.println( "#2: It took " + toString(rollsTilGet(7)) + " rolls to get 7." ); System.out.println( "#3: It took " + toString(rollsTilGet(7)) + " rolls to get 7." ); System.out.println( "#4: It took " + toString(rollsTilGet(7)) + " rolls to get 7." ); System.out.println( "" ); System.out.println( "It took us " + hiloHumanIO() + " guesses for the hidden number." ); return; } public static void main( String[] args ) { testAll(); } }