Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 8th, 2006, 04:28 PM
ChaNgeD ChaNgeD is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 15 ChaNgeD User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 21 m 37 sec
Reputation Power: 0
Help With program

The program prompts the user to choose a secret number then the computer has 10 trys to guess it. While the computer guesses the number the user tells him each time to go higher or go lower and the computer should guess higher or lower. You also must use recursion. So far i am done with the program but i will touch it up a bit. Now what i need help with is getting the computers guess to go higher if promted higher and go lower if prompted lower by the user and the computer's guess must also never go lower or higher then his previous guess.
Heres an example i can give u:

Please enter your secret number: 76
Now the computer will try and guess your number!
The computer guessed: 39
Please tell the computer to go higher or go lower!
Higher
The computer guessed: 91
Please tell the computer to go higher or go lower!
Lower

and so on......
heres my code:
Code:
import java.io.*;
import java.util.*;
public class ComputerGuessNumberRecursion
{
public static void main (String[] args) throws IOException
{
		System.out.println ("\t\tPLEASE READ INSTRUCTIONS"+
		"\n1.You choose a number from 1-100"+
		"\n2.The computer will have 10 trys to guess your number"+
		"\n3.If the computer guesses right you LOSE!"+
		"\n4.If the computer guesses wrong you WIN!"+
		"\n5.NO LETTERS OR INVALID SYMBOLS ALLOWED!"+
		"\n6.Enjoy!");Game();
}
private static void Game() throws IOException
{
	try
	{
		BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
		String input, user_n;
		Random rand_num = new Random();
		int secret_n, comp_n, Lose = 0, max = 100, min = 1;
		System.out.print("\nPlease enter secret number: ");
		secret_n = Integer.parseInt(input = keyboard.readLine().trim());
		if(secret_n <= 0 || secret_n > 100)
		{
			System.out.print("\nError!\n-----------------\nInvalid Answer\n-----------------\nReEnter your Guess: ");
			secret_n = Integer.parseInt(input = keyboard.readLine().trim());
		}
		System.out.println("\nNow the computer will try to guess your number");
		comp_n = rand_num.nextInt(max);
		System.out.println("\nThe computer guessed: "+comp_n);
		CompGuess(secret_n, Lose, comp_n);
	}
	catch(NumberFormatException e)															
	{
		System.out.println ("Invalid entry, follow direction! Please Start Over");
		Game();
	}
	catch(NullPointerException e)															
	{
		System.out.println ("Invalid entry, follow direction! Please Start Over");
		Game();
	}
}
private static int CompGuess(int secret_n, int Lose, int comp_n) throws IOException
{
		BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
		Random rand_num = new Random();
		String user_n;
		int min = 1, max = 100;
		Lose++;
		for(int loop = 0; loop <= 10; loop++)
		{
			System.out.println("\n\nTell the computer to go Higher or go Lower: ");
			user_n = keyboard.readLine().trim();
			if(user_n.equalsIgnoreCase("higher") || user_n.equalsIgnoreCase("lower"))
			{
				System.out.println("\nThe computer guessed: "+comp_n);
				comp_n = rand_num.nextInt(max);
				randomize(max, min);
			}
			if(comp_n == secret_n)
			{
				System.out.println("\n----------------\n|COMPUTER WINS!|\n----------------");
				TryAgain();	
			}
			if (Lose == 9)
			{
				System.out.println("\n-----------------\n|COMPUTER LOSES!|\n-----------------");
				TryAgain();
			}	
		}
		return comp_n;
}
private static int randomize(int max, int min)
{
	Random num_generator = new Random();
	int generate = 0;
	while(generate >= min)
	{
		generate = num_generator.nextInt(max);
	}
	return generate;
}
public static void TryAgain() throws IOException												
{
	String TryAgain;
	BufferedReader keyboard = new BufferedReader (new InputStreamReader (System.in));
	do
	{
		System.out.print ("\nWould you like to try again? type in (Y/N): ");
		TryAgain = keyboard.readLine().trim();
		if (TryAgain.equalsIgnoreCase ("y"))
		{
			Game();	
		}																																	
		if (TryAgain.equalsIgnoreCase ("n"))
		{
			System.exit(0);
		}																										
		else
		{
			System.out.print("\nError!\n-----------------\nInvalid Answer\n-----------------");
		}
	}
	while (!TryAgain.equalsIgnoreCase ("y") || !TryAgain.equalsIgnoreCase ("n"));					
}										
}

Last edited by MadCowDzz : November 9th, 2006 at 12:28 PM. Reason: added [code] tags

Reply With Quote
  #2  
Old November 9th, 2006, 01:35 AM
daniel_g daniel_g is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 60 daniel_g User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 3 m 50 sec
Reputation Power: 3
Your code is way too hard to read. Next time use some code brackets. Also, in the future, include some comments on the code.

First, consider changing this to a loop:
Code:
            if(secret_n <= 0 || secret_n > 100)
            {
               System.out.print("\nError!\n-----------------\nInvalid Answer\n-----------------\nReEnter your Guess: ");
               secret_n = Integer.parseInt(input = keyboard.readLine().trim());
            }


Now, as far as your question, you have to understand how the random function works
Code:
/* generate a number 0<comp_n<max */
comp_n = rand_num.nextInt(max);

/* 
 * So if player chose lower, then you would generate a
 * numbe between 0 and comp_n
 */
max = comp_n;
comp_n = rand_num.nextInt(max);

/* 
 * If player chose Higher, then you need to generate a number
 * between comp_n and max 
 */
min = comp_n;
comp_n = rand_num.nextInt(max-min) + min;

//the rest you should be able to figure out.

I just did all that off my head and I really need some sleep. Don't trust what you see. Test it, and make it better.

Reply With Quote
  #3  
Old November 9th, 2006, 12:32 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
ChaNgeD, I have modified your original post.
Next time please wrap your code with [code][/code] tags when posting

Colton22, I deleted your post. Pasting ChaNgeD's original code without indenting buy adding the forum wrapper kind of made it harder to read
No disrespect intended!
__________________
Daryl's Homepage | My Blogroll | My Profile | Firefox supporter!
DevArticles Forum Moderator

"The net is a waste of time, and that's exactly what's right about it." -- William Gibson

Reply With Quote
  #4  
Old November 9th, 2006, 01:28 PM
colton22's Avatar
colton22 colton22 is offline
\ ^_^ / - Moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2006
Location: near chicago, Illinois
Posts: 473 colton22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 1 h 19 m 14 sec
Reputation Power: 3
Send a message via AIM to colton22 Send a message via MSN to colton22 Send a message via Yahoo to colton22
thats alright and thanks for the modification

colton22

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Help With program


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway