# Thread: Need help with random game!

1. Member Join Date
Aug 2007
Posts
8
Rep Power
0

## 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:-

Java 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

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;		//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): ");
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! ");
}```  Reply With Quote

2. Member Join Date
Jul 2007
Location
England, Bath
Posts
47
Rep Power
0

## 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 :)  Reply With Quote

3. Member Join Date
Aug 2007
Posts
8
Rep Power
0

## 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.

Java 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

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;		//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): ");
playerGuess = Integer.parseInt (input);

if (playerGuess==num) {
correctTry=true;
break; // break out of the while loop
}```
Last edited by silverq_82; 08-07-2007 at 04:11 PM.  Reply With Quote

4. Member Join Date
Jul 2007
Location
England, Bath
Posts
47
Rep Power
0

## 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?  Reply With Quote

5. Member Join Date
Aug 2007
Posts
8
Rep Power
0

## Hi,

thats right!  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•