Results 1 to 5 of 5
  1. #1
    silverq_82 is offline Member
    Join Date
    Aug 2007
    Posts
    8
    Rep Power
    0

    Default 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 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! ");
        }

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

    Default

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

  3. #3
    silverq_82 is offline Member
    Join Date
    Aug 2007
    Posts
    8
    Rep Power
    0

    Default

    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
    		
    		//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
                }
    Last edited by silverq_82; 08-07-2007 at 05:11 PM.

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

    Default

    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?

  5. #5
    silverq_82 is offline Member
    Join Date
    Aug 2007
    Posts
    8
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  2. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 11:44 PM
  3. random generation
    By carlos123 in forum New To Java
    Replies: 10
    Last Post: 01-09-2008, 04:43 AM
  4. Random Integers
    By www.kwalski.com in forum Java Applets
    Replies: 8
    Last Post: 12-09-2007, 06:49 PM
  5. Math.Random
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-23-2007, 03:09 PM

Posting Permissions

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