0
\$\begingroup\$

In this program, there is a code that the user has to try to solve in 10 guesses. If you have any numbers in the right spot, it will tell you how many you have correct and the value of the correct numbers so you can figure it out. I know there has to be a much simpler way to do this, I just can't put one together. I could use arrays (although i'm not the best with them yet) to find certain values, but i'm not positive on how to do that. Any tips? (I apologize in advance for any sloppy coding or poor choice of variable names. Also this is in a main method, i just didn't copy the class name or the main method)

    String code = "53492";
    int guesses = 10;
    Scanner s12 = new Scanner(System.in);
    System.out.println("Try to guess the 5 number code. You have 10 guesses: ");
    String guess = s12.nextLine();
    int right = 0;
    int combo = 0; //correct values combined
    int correctInt = 0;

    for(int i = 0; i < 10; i++) //will allow you to guess a certain amount of times
    {
        combo = 0; 
        right = 0;
        for(int x = 0; x < 5; x++) //starts searching at first spot(0)
        {
            int guessInt = Integer.parseInt(guess);
            int codeInt = Integer.parseInt(code);
            String getCorrectInt = guess.substring(x, x + 1);
            correctInt = Integer.parseInt(getCorrectInt);  //stores value of num at the value of
            String getCodeGuess = code.substring(x, x + 1);
            int codeGuess = Integer.parseInt(getCodeGuess);
                if(guessInt == codeInt)
                {
                    System.out.println("Correct! You guessed it!");
                    System.exit(0);
                }
                if(correctInt == codeGuess)  //if the num at the position matches the code at the right position
                {
                    combo += correctInt;    //totals up the correct amount of numbers in right spot
                    right++; //keeps track of how many are right
                }
        }
        guesses--;
        System.out.println("You have " + right + " number(s) correct that equal to " + combo + " and you have " + guesses + " guesses left.");
        System.out.println("Guess again: ");
        guess = s12.nextLine();
    }
    System.out.println("You ran out of guesses!  The code was " + code);
\$\endgroup\$
1
\$\begingroup\$

Streams clean this up a little bit, however I removed print statements here so that the logic can be seen a little clearer:

String code = "53492";
int guesses = 10;
Scanner scanner = new Scanner(System.in);

while (guesses-- > 0) {
    String guess = scanner.nextLine();
    if (guess.equals(code)) {
        return; //guess was correct, can also just compare correct to guess length
    }

    int correct = compare(guess, code, Stream::count); //count elems
    int combo = compare(guess, code, s -> s.reduce(0, Integer::sum)); //sum
    // print information, note this requires three iterations since we stream twice
    // and have an #equals check. There are still some optimizations you
    // can make to remove this
}

//elsewheres
public <R> R compare(String one, String two, Function<IntStream, R> back) {
    //apply our stream function to an intstream for a mutable return
    return back.apply(IntStream.range(0, code.length())
                      .filter(i -> guess.charAt(i) == code.charAt(i)) //correct chars
                      .map(i -> guess.charAt(i) - '0')); //map to values
}

It seemed like it would be a neater solution before I actually sat and wrote it, but I figured it'd be nice to at least see an alternative.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy