|
| 1 | +package org.teachingkidsprogramming.recipes.completed.section03ifs.KataQuestions; |
| 2 | + |
| 3 | +import org.teachingextensions.logo.utils.EventUtils.MessageBox; |
| 4 | + |
| 5 | +//Your user knows the answer, the computer gets 8 guesses to determine the answer |
| 6 | +//You tell the computer whether its guess is too high or too low |
| 7 | +//Write each of the English line comments (use at least 8 line comments) |
| 8 | +//Number each comment line at the end |
| 9 | +//Verify - step one - Translate EACH comment line into code |
| 10 | +//Verify - step two - Run your code after each line |
| 11 | +public class ReverseHiLow_Answer |
| 12 | +{ |
| 13 | + public static void main(String[] args) |
| 14 | + { |
| 15 | + MessageBox |
| 16 | + .showMessage("Hello, actual human! Think of a number between 1 and 100, and I will attempt to guess it."); |
| 17 | + MessageBox.showMessage("Click OK when you have one in mind."); |
| 18 | + int attemptNumber = 1; |
| 19 | + int currentGuess = 50; |
| 20 | + int highestPossible = 100; |
| 21 | + int lowestPossible = 1; |
| 22 | + while (attemptNumber < 8) |
| 23 | + { |
| 24 | + MessageBox.showMessage("Beginning attempt number " + attemptNumber); |
| 25 | + String feedback = MessageBox.askForTextInput("Is " + currentGuess |
| 26 | + + " the correct number? Write 'yes' if it is, if not write 'too high' or 'too low'."); |
| 27 | + if (feedback.equals("yes")) |
| 28 | + { |
| 29 | + MessageBox.showMessage("Got it on attempt number " + attemptNumber + "! Darn, I'm good. See ya!"); |
| 30 | + System.exit(0); |
| 31 | + } |
| 32 | + else |
| 33 | + { |
| 34 | + if (feedback.equalsIgnoreCase("too high")) |
| 35 | + { |
| 36 | + highestPossible = currentGuess; |
| 37 | + currentGuess = currentGuess - ((currentGuess - lowestPossible) / 2); |
| 38 | + } |
| 39 | + else if (feedback.equalsIgnoreCase("too low")) |
| 40 | + { |
| 41 | + lowestPossible = currentGuess; |
| 42 | + currentGuess = currentGuess + ((highestPossible - currentGuess) / 2); |
| 43 | + } |
| 44 | + attemptNumber++; |
| 45 | + } |
| 46 | + } |
| 47 | + // Variable: currentGuess |
| 48 | + // Computer guesses currentGuess, asks if too high or too low |
| 49 | + // User inputs |
| 50 | + // If right, computer congratulates itself and ends |
| 51 | + // If wrong, computer takes feedback and adjusts currentGuess accordingly |
| 52 | + } |
| 53 | +} |
0 commit comments