Advertisement
NicholasCSW

StringArraySearchV4

Mar 29th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.54 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.*;
  9. import java.io.*;
  10. import java.util.Scanner;
  11. public class GuessingGameUlizio
  12. {
  13.     public static void main(String [] args) {
  14.        try {
  15.            
  16.            System.out.println("I am Jeff. I will tell you if your word is in stock today at THE BEST STORE.");
  17.            
  18.            
  19.            Scanner file = new Scanner(new BufferedReader(new FileReader("words.txt")));
  20.            
  21.            int wordCount = 0;
  22.            int max = 0;
  23.            int min = 0;
  24.            
  25.            while(file.hasNext()) {
  26.                file.next();
  27.                wordCount++;
  28.                
  29.             }
  30.            
  31.             file = new Scanner(new BufferedReader(new FileReader("words.txt")));
  32.             String [] words = new String[wordCount];
  33.            
  34.             for (int i = 0; i < wordCount; i++) {
  35.                
  36.                 words[i] = file.next();
  37.                
  38.             }
  39.            
  40.             Scanner scan = new Scanner(System.in);
  41.             System.out.println("Please input the word to search for:");
  42.             String userInput = scan.next();
  43.             scan.close();
  44.            
  45.             max = words.length;
  46.        
  47.            int roundCount = 0;
  48.            
  49.            int currentGuess = (max + min)/2;
  50.        
  51.            while(true){
  52.                
  53.                
  54.                
  55.                //System.out.println(currentGuess);
  56.                
  57.                boolean finished = check(userInput, words, currentGuess);
  58.                
  59.                
  60.                if(finished == false) {
  61.                    
  62.                    if(userInput.compareToIgnoreCase(words[currentGuess]) < 0) {
  63.                        
  64.                        max = currentGuess - 1;
  65.                     }
  66.                     else if (userInput.compareToIgnoreCase(words[currentGuess]) > 0) {
  67.                        
  68.                         min = currentGuess + 1;
  69.                     }
  70.                     else {
  71.                        
  72.                     }
  73.                  
  74.                }
  75.                
  76.                if(max ==  min) {
  77.                    
  78.                    System.out.println("Your word is not in stock.");
  79.                    break;
  80.                 }
  81.                
  82.                if(finished == true) {
  83.                    
  84.                    System.out.println("I found your word in aisle " + currentGuess);
  85.                    
  86.                    break;
  87.                }
  88.                else {
  89.                    
  90.                }
  91.                
  92.                roundCount++;
  93.             }
  94.            
  95.            
  96.            
  97.            
  98.         } catch (Exception e) {
  99.            
  100.             System.out.println("YEET");
  101.         }
  102.        
  103.        
  104.        
  105.        
  106.     }
  107.    
  108.     public static int guess(int max, int min) {
  109.         /**
  110.          * This method calculates Earl's guess.
  111.          */
  112.        
  113.         int sumMaxMin = max + min;
  114.        
  115.         int guess = sumMaxMin/2;
  116.        
  117.         return(guess);
  118.        
  119.     }
  120.    
  121.     public static char highLow() {
  122.         /**
  123.          * This method asks the user if the guess was high or low.
  124.          */
  125.        
  126.         Scanner scan = new Scanner(System.in);
  127.        
  128.         System.out.println("If my guess was too high, type h. If it was too low, type l.");
  129.        
  130.         char userInput = scan.next().charAt(0);
  131.        
  132.         return(userInput);
  133.        
  134.     }
  135.    
  136.     public static int getMax() {
  137.         /**
  138.          * This method gets the user's max.
  139.          */
  140.        
  141.         Scanner scan = new Scanner(System.in);
  142.        
  143.         System.out.println("High End?");
  144.         int max = scan.nextInt();
  145.        
  146.         return(max);
  147.        
  148.     }
  149.    
  150.     public static int getMin() {
  151.         /**
  152.          * This method gets the user's min.
  153.          */
  154.        
  155.         Scanner scan = new Scanner(System.in);
  156.  
  157.         System.out.println("Low End?");
  158.         int min = scan.nextInt();
  159.        
  160.         return(min);
  161.        
  162.     }
  163.    
  164.     public static boolean check(String userInput, String [] words, int guess) {
  165.         /**
  166.          * This method checks the computer's guess.
  167.          */
  168.        
  169.         boolean finished = false;
  170.         if(userInput.equals(words[guess])) {
  171.            
  172.             finished = true;
  173.         }
  174.         else {
  175.             finished = false;
  176.                
  177.         }
  178.        
  179.         return(finished);
  180.        
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement