Current location: Hot Scripts Forums » Programming Languages » Everything Java » Number Puzzle


Number Puzzle

Reply
  #1 (permalink)  
Old 03-07-11, 01:28 AM
isfan isfan is offline
New Member
 
Join Date: Mar 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rolleyes Number Puzzle

Hi... Ihave a problem regarding this question... Somebody can help me?

Question : Users task is to guess 4 digits generated by your program. Those digits should be unique. Your program will be give hint to users based on their guess.
Number of Green – Right digit at the right place
Number of Red – Right digit but at the wrong place

I just got the code but it not works.

Code:
import java.util.Random; 
import java.util.Scanner;  
public class NumberGame
{
	public static void main(String[] args)
	{
		Random generator = new Random();
		Scanner in = new Scanner(System.in);
		
		int number = generator.nextInt(100) + 1;
		int on_off = 1;
		
		while(on_off == 1){
			System.out.print("Guess a number between 1 and 100: ");
			int guess = in.nextInt();
			
			if(guess < number){
				System.out.println("Too Low");
			}
			
			if(guess > number){
				System.out.println("Too High");
			}
			
			if(guess == number){
				System.out.println("Correct! You win!");
				on_off = 0;
			}
		}

	}
}

Last edited by UnrealEd; 03-07-11 at 01:57 AM. Reason: added [code] wrappers
Reply With Quote
  #2 (permalink)  
Old 03-07-11, 02:18 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
The task is a lot more complex than what you thought it was (based on your code snippet). It's actually a game called Masterminds, but instead of using colors you use numbers, which adds more difficulty to the game as there are a lot more options. On the other hand it makes it makes it a lot easier when you show which numbers are positioned wrongfully and which are correct.

The reason why you're code isn't properly working is because you're comparing values instead of positions, which is something that can't be done with integers. 81 is larger than 15, but it doesn't say that the one is a correct number but is in the wrong position.
While you're comparing numbers, you should in fact be comparing strings. You need to read in a string of numbers, and compare each and every character in it to the random number, also stored in a string.

Here's basically what the code should do to compare both strings:
  • loop over the read-string (let's say the current character is read_i)
    • loop over the random (which is now fixed) number (let's say the current character is rand_i)
      • check if read_i equals rand_i
      • if so:
        • check if the loop-pointers are equal
        • if so:
          • show a green number
        • if not:
          • show a red number
      • if not:
        • continue the loop
Note that my above pseudo-code will show weird result when you enter a digit twice and it has a match: Suppose 12341 is supposed to match 98176, than the 1 will show up twice in red, while in fact only one 1 is correct (I don't know exactly how it works in mastermind, so you'll have to ask).

Feel free to ask for more information, I'd be happy to help you out.
I've made a mastermind game in Java a couple of years ago, so I have a fully working code, but in my opinion it's better that you find the solution yourself, rather than me giving it to you
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Puzzle Onsite still alive :-) Zahneissen Website Reviews 7 09-26-11 08:21 AM
Perfect number... Argo_Jeude C/C++ 1 07-14-05 02:35 PM
add a number every 30 minutes Dion PHP 5 08-31-04 12:52 AM


All times are GMT -5. The time now is 05:57 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.