Results 1 to 5 of 5
Thread: Need help with random game!
 08072007, 10:03 AM #1Member
 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 //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 nonduplicate 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! "); }
 08072007, 01:59 PM #2Member
 Join Date
 Jul 2007
 Location
 England, Bath
 Posts
 47
 Rep Power
 0
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:
 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.
 enter the loop checking the users inputs. Remember check the length is 5 and that they have entered a number.
 output the correct response
 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 :)
 08072007, 02:10 PM #3Member
 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 19. 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 //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 nonduplicate 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 }
Last edited by silverq_82; 08072007 at 04:11 PM.
 08072007, 02:27 PM #4Member
 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?
 08072007, 02:58 PM #5Member
 Join Date
 Aug 2007
 Posts
 8
 Rep Power
 0
Similar Threads

Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08052010, 04:49 AM 
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01172008, 10:44 PM 
random generation
By carlos123 in forum New To JavaReplies: 10Last Post: 01092008, 03:43 AM 
Random Integers
By www.kwalski.com in forum Java AppletsReplies: 8Last Post: 12092007, 05:49 PM 
Math.Random
By Java Tip in forum Java TipReplies: 0Last Post: 11232007, 02:09 PM
Bookmarks