# Need help with random game!

Printable View

• 08-07-2007, 10:03 AM
silverq_82
Need help with random game!
Hi everyone,

i'm totally new to Java and need help on this. Creating a guessing game where i need to checking a user's input with 5 unique generated random numbers (1 - 9). I've completed the 5 unique generated random numbers but am unsure of how to create the 'checking of 5 number user input with the 5 random numbers'. Please guide me along. Thanks!! Below are part of the codes:-

Code:

```private static void newGame() throws IOException         {                 final int TOTAL_GUESS = 10;        //total number of guesses                 int guess;                                //number of guesses                 int playerGuess;                    //player's guess                 boolean correctTry=false;        //signal for correct guess                 String input;                              //input text data                 //int correctPos=0;                    //counts the correct position                 //int rightNum;                              //counts the right number                                 //Buffered reader to read console input         BufferedReader console = new BufferedReader (new InputStreamReader(System.in));                                         Scanner myScanner = new Scanner (System.in);                                 //Random generator             Random gen = new Random();             int num = gen.nextInt(9) + 1;                                       //Generate 5 non-duplicate numbers hidden from user                   int mixcount=1;                 int setcount=5;                                        //mutator values                 int[] numbers = new int[5];                //declare and create array                         Random mix = new Random();                         for(int i = 0 ; i< numbers.length; i++)                 {                 boolean diff = false;                         while (!diff)                         {                                 numbers[i] = mix.nextInt(9);                                 diff=true;                                 for (int j=0 ; j < i ; j++)                                         {                                                 if (numbers[i]==numbers[j])                                                 {                                                         diff = false;                                                         break;                                                 }                                         }                         }                         System.out.print(numbers[i]+" "); //remove this after all is complete                 }                                   //Player's input but only guessing 1 number how to guess 5 numbers in an array?             guess = 0;             while(guess < TOTAL_GUESS) {                     System.out.println("Guess " + guess);                     System.out.print("Enter five numbers (1 to 9): ");                     input = console.readLine();                     playerGuess = Integer.parseInt (input);                                         if (playerGuess==num) {                 correctTry=true;                 break; // break out of the while loop             }             else if (playerGuess < num)                 System.out.println("Higher...");             else                     System.out.println("Lower...");                                 guess++;             }                         if(correctTry)                         System.out.println("Correct!");                 else                         System.out.println("Total of " + TOTAL_GUESS + " guesses reached! ");     }```
• 08-07-2007, 01:59 PM
shanePreater
Before I can help you I could do with clarifying your problem a little bit.

So you generate a 5 digit number which the user has to guess. For example 12345?

The user then enters a guess, say 21345
You should respond with Lower
The user then enters 11234
You should respond higher
when the user then enters 12345
you return Correct and exit the program.

Is this the problem you have to solve or have I missed something?

If this is correct then I would do the following:
1. generate the 5 digits and slap them together into 1 number. You can do this by multiplying the digit by the correct factor or append a String with each digit and convert at the end.
2. enter the loop checking the users inputs. Remember check the length is 5 and that they have entered a number.
3. output the correct response
4. If the correct number was found then exit the loop.

If I have made an incorrect assumption or you need more help just let me know :)
• 08-07-2007, 02:10 PM
silverq_82
Hi,

thanks for the reply. I guessed i missed out some important points. Please ignore the part where it gives the hint 'higher' or 'lower'. What I need to do is to create a scenario where the user inputs 5 random number from 1-9. After which, there has to be a comparison between the random generated unique numbers and user's input. Hints on the wrong positions and right positions are also to be stated.

Code:

```private static void newGame() throws IOException         {                 final int TOTAL_GUESS = 10;        //total number of guesses                 int guess;                                //number of guesses                 int playerGuess;                    //player's guess                 boolean correctTry=false;        //signal for correct guess                 String input;                              //input text data                 //int correctPos=0;                    //counts the correct position                 //int rightNum;                              //counts the right number                                 //Buffered reader to read console input         BufferedReader console = new BufferedReader (new InputStreamReader(System.in));                                         Scanner myScanner = new Scanner (System.in);                                 //Random generator             Random gen = new Random();             int num = gen.nextInt(9) + 1;                                       //Generate 5 non-duplicate numbers hidden from user                   int mixcount=1;                 int setcount=5;                                        //mutator values                 int[] numbers = new int[5];                //declare and create array                         Random mix = new Random();                         for(int i = 0 ; i< numbers.length; i++)                 {                 boolean diff = false;                         while (!diff)                         {                                 numbers[i] = mix.nextInt(9);                                 diff=true;                                 for (int j=0 ; j < i ; j++)                                         {                                                 if (numbers[i]==numbers[j])                                                 {                                                         diff = false;                                                         break;                                                 }                                         }                         }                         System.out.print(numbers[i]+" "); //remove this after all is complete                 }                                   //Player's input but only guessing 1 number how to guess 5 numbers in an array?             guess = 0;             while(guess < TOTAL_GUESS) {                     System.out.println("Guess " + guess);                     System.out.print("Enter five numbers (1 to 9): ");                     input = console.readLine();                     playerGuess = Integer.parseInt (input);                                         if (playerGuess==num) {                 correctTry=true;                 break; // break out of the while loop             }```
• 08-07-2007, 02:27 PM
shanePreater
So you are not trying to create a single 5 digit number then, you actually want 5 single digit numbers and then indicate from that which are correct?
• 08-07-2007, 02:58 PM
silverq_82
Hi,

thats right!