|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
||||
|
||||
|
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 |
|
#4
|
||||
|
||||
|
thats alright and thanks for the modification
colton22 |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Help With program |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|