Advertisement
NicholasCSW

Binary Search Guessing Game

Mar 20th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1.  
  2. /**
  3.  * Guesses the user's number on a high low basis
  4.  *
  5.  * @author Ulizio
  6.  * @version 3.0
  7.  */
  8. import java.util.Scanner;
  9. public class GuessingGameUlizio
  10. {
  11.     public static void main(String [] args) {
  12.        
  13.        System.out.println("I am Earl. I will be guessing your number today.");
  14.        
  15.        int max = getMax();
  16.        int min = getMin();
  17.        int roundCount = 0;
  18.        
  19.        while(true){
  20.            
  21.            int currentGuess = guess(max, min);
  22.            
  23.            System.out.println(currentGuess);
  24.            
  25.            boolean finished = check(currentGuess);
  26.            if(finished == false) {
  27.                
  28.                char highOrLow = highLow();
  29.                
  30.                if(highOrLow == 'h') {
  31.                    
  32.                    max = (max + min)/2;
  33.                }
  34.                else if(highOrLow == 'l') {
  35.                    
  36.                    min = (max + min)/2;
  37.                }
  38.                else {
  39.                
  40.                }
  41.                
  42.            }
  43.            
  44.            if(finished == true) {
  45.                
  46.                System.out.println("I guessed your number in " + roundCount + " rounds!");
  47.                
  48.                break;
  49.            }
  50.            else {
  51.                
  52.            }
  53.            
  54.            roundCount++;
  55.         }
  56.        
  57.     }
  58.    
  59.     public static int guess(int max, int min) {
  60.         /**
  61.          * This method calculates Earl's guess.
  62.          */
  63.        
  64.         int sumMaxMin = max + min;
  65.        
  66.         int guess = sumMaxMin/2;
  67.        
  68.         return(guess);
  69.        
  70.     }
  71.    
  72.     public static char highLow() {
  73.         /**
  74.          * This method asks the user if the guess was high or low.
  75.          */
  76.        
  77.         Scanner scan = new Scanner(System.in);
  78.        
  79.         System.out.println("If my guess was too high, type h. If it was too low, type l.");
  80.        
  81.         char userInput = scan.next().charAt(0);
  82.        
  83.         return(userInput);
  84.        
  85.     }
  86.    
  87.     public static int getMax() {
  88.         /**
  89.          * This method gets the user's max.
  90.          */
  91.        
  92.         Scanner scan = new Scanner(System.in);
  93.        
  94.         System.out.println("High End?");
  95.         int max = scan.nextInt();
  96.        
  97.         return(max);
  98.        
  99.     }
  100.    
  101.     public static int getMin() {
  102.         /**
  103.          * This method gets the user's min.
  104.          */
  105.        
  106.         Scanner scan = new Scanner(System.in);
  107.  
  108.         System.out.println("Low End?");
  109.         int min = scan.nextInt();
  110.        
  111.         return(min);
  112.        
  113.     }
  114.    
  115.     public static boolean check(int guess) {
  116.         /**
  117.          * This method checks the computer's guess.
  118.          */
  119.        
  120.         Scanner scan = new Scanner(System.in);
  121.        
  122.         System.out.println("Was I correct? Type y for yes or n for no.");
  123.        
  124.         char userInput = scan.next().charAt(0);
  125.        
  126.         boolean finished = false;
  127.         if(userInput == 'y') {
  128.            
  129.             finished = true;
  130.         }
  131.         else if(userInput == 'n') {
  132.            
  133.             finished = false;
  134.         }
  135.         else {
  136.                
  137.         }
  138.        
  139.         return(finished);
  140.        
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement