Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-07-2007, 11:03 AM
Member
 
Join Date: Aug 2007
Posts: 8
Rep Power: 0
silverq_82 is on a distinguished road
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:-

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! ");
    }
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 08-07-2007, 02:59 PM
Member
 
Join Date: Jul 2007
Location: England, Bath
Posts: 47
Rep Power: 0
shanePreater is on a distinguished road
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
__________________
Shane Preater - The One Vision Group
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-07-2007, 03:10 PM
Member
 
Join Date: Aug 2007
Posts: 8
Rep Power: 0
silverq_82 is on a distinguished road
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.

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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-07-2007, 03:27 PM
Member
 
Join Date: Jul 2007
Location: England, Bath
Posts: 47
Rep Power: 0
shanePreater is on a distinguished road
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?
__________________
Shane Preater - The One Vision Group
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-07-2007, 03:58 PM
Member
 
Join Date: Aug 2007
Posts: 8
Rep Power: 0
silverq_82 is on a distinguished road
Default
Hi,

thats right!
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
random numbers without random class` carlos123 New To Java 4 01-17-2008 11:44 PM
random generation carlos123 New To Java 10 01-09-2008 04:43 AM
Random Integers www.kwalski.com Java Applets 8 12-09-2007 06:49 PM
Math.Random Java Tip Java Tips 0 11-23-2007 03:09 PM
Implementing "Game Over" in Minesweeper game based on Gridworld framework. JFlash New To Java 0 11-16-2007 12:02 AM


All times are GMT +2. The time now is 04:20 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org