1

So I'm doing homework, and it is supposed to be a game of cows and bulls.

I have generated 4 random integers to be guessed, and the user enters a 4 digit number. I then validate that the number is indeed 4 digits long and turn it into a array of CHARs. After that, I wanted to compare the randomly generated numbers one by one with the elements in the array, and if they are equal, add a "1" to a counter.

I have tried many solutions that I found on the internet to similar problems, some very difficult ones I have not as they seemed like overkill, because I am still very new to programming and I feel as though I am missing something simple and obvious. The problem is, everytime I iterate through the array, the elements should match, but the condition is always "False".

Here is the problematic part of the code:

    Scanner Sc1 = new Scanner(System.in);
    if(Sc1.hasNextInt()){                            // is it a number ? ?
        int user_guess = Sc1.nextInt();
        String user_guess_string = Integer.toString(user_guess);                  // how.
        int length = user_guess_string.length();                                   // many
        if(length != 4){                                                           //digits
            System.out.println("On vaja 4 numbrid.Mäng hakkab otsast pihta");
        cows_and_bulls();}

        char[] player_number = user_guess_string.toCharArray();  
        char num0 = (char)random1;  //  casting
        char num1 = (char)random2;  //  random numbers
        char num2 = (char)random3;   //  into
        char num3 = (char)random4;    //  chars
        int cows = 0;    // counter of instances where a number guessed by the user is in array
        int bulls = 0;  // counter of instances where a guessed number is in array and correct spot

        for (char element : player_number) {
            if (element == num0) {
                cows = cows + 1;
            }
        }

        System.out.println(cows);
        }

    else{ System.out.println("Not a number. Game will restart");
        cows_and_bulls();}

    }

I have also tried this :

        for (int i = 0; i < 4; i++) {
            char c = Array.getChar(player_number,i);
            if (num0 == c) {
                System.out.println("works");
                break;
            }
        }

I know I am very bad at this, please help.

2 Answers 2

1

A char array is the wrong data structure, let's get rid of it. Adding magic numbers and casting is a code smell, I.E. (char)(48 + random1).

From Sc1.nextInt() we know the input is an int, from if(length != 4) we know it's 4 characters long and we want a collection of int from it.

char[] player_number = user_guess_string.toCharArray() becomes:

List<Integer> player_number = Arrays.stream(user_guess_string.split(""))
        .map(Integer::parseInt)
        .collect(Collectors.toList());

Now, char num0-3 can be changed to int and we're dealing with the datatypes we intended to.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm not sure where are you getting random1, random2, random3 and random4 from as it's not part of the logic you pasted above but, the problem here is that you are not adding the ASCII of '0' i.e. 48 to the values above while casting them as characters.

Assuming that all the random numbers you have are valid digits in between 0 - 9, the following should work.

char num0 = (char)(48 + random1);
char num1 = (char)(48 + random2);
...
...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.