Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Guesses the user's number on a high low basis
- *
- * @author Ulizio
- * @version 3.0
- */
- import java.util.*;
- import java.io.*;
- import java.util.Scanner;
- public class GuessingGameUlizio
- {
- public static void main(String [] args) {
- try {
- System.out.println("I am Jeff. I will tell you if your word is in stock today at THE BEST STORE.");
- Scanner file = new Scanner(new BufferedReader(new FileReader("words.txt")));
- int wordCount = 0;
- int max = 0;
- int min = 0;
- while(file.hasNext()) {
- file.next();
- wordCount++;
- }
- file = new Scanner(new BufferedReader(new FileReader("words.txt")));
- String [] words = new String[wordCount];
- for (int i = 0; i < wordCount; i++) {
- words[i] = file.next();
- }
- Scanner scan = new Scanner(System.in);
- System.out.println("Please input the word to search for:");
- String userInput = scan.next();
- scan.close();
- max = words.length;
- int roundCount = 0;
- int currentGuess = (max + min)/2;
- while(true){
- //System.out.println(currentGuess);
- boolean finished = check(userInput, words, currentGuess);
- if(finished == false) {
- if(userInput.compareToIgnoreCase(words[currentGuess]) < 0) {
- max = currentGuess - 1;
- }
- else if (userInput.compareToIgnoreCase(words[currentGuess]) > 0) {
- min = currentGuess + 1;
- }
- else {
- }
- }
- if(max == min) {
- System.out.println("Your word is not in stock.");
- break;
- }
- if(finished == true) {
- System.out.println("I found your word in aisle " + currentGuess);
- break;
- }
- else {
- }
- roundCount++;
- }
- } catch (Exception e) {
- System.out.println("YEET");
- }
- }
- public static int guess(int max, int min) {
- /**
- * This method calculates Earl's guess.
- */
- int sumMaxMin = max + min;
- int guess = sumMaxMin/2;
- return(guess);
- }
- public static char highLow() {
- /**
- * This method asks the user if the guess was high or low.
- */
- Scanner scan = new Scanner(System.in);
- System.out.println("If my guess was too high, type h. If it was too low, type l.");
- char userInput = scan.next().charAt(0);
- return(userInput);
- }
- public static int getMax() {
- /**
- * This method gets the user's max.
- */
- Scanner scan = new Scanner(System.in);
- System.out.println("High End?");
- int max = scan.nextInt();
- return(max);
- }
- public static int getMin() {
- /**
- * This method gets the user's min.
- */
- Scanner scan = new Scanner(System.in);
- System.out.println("Low End?");
- int min = scan.nextInt();
- return(min);
- }
- public static boolean check(String userInput, String [] words, int guess) {
- /**
- * This method checks the computer's guess.
- */
- boolean finished = false;
- if(userInput.equals(words[guess])) {
- finished = true;
- }
- else {
- finished = false;
- }
- return(finished);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement