Guessing Game - JAVA

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Hello all, My program is a guessing game that allows the user to enter the low and high numbers and how many guesses he/she gets. Then the program generates a random number between the low and high. Got everything working fine, just some small things that I need help with and one big thing that I am confused on.

SMALL THINGS:
1) When they incorrectly guess the number it tells you then says how many guesses you have left on your last guess it prints "Incorrect guess. You have 0 guess left". I don't want it to print that. Just want you to enter you last guess and if its wrong it just say " You lost!!!".

2) I need to have the while to evaluate a CHAR of either Y/N but I couldn't get it to work. I just threw in a int just to see if it was even capable of working, it was. I had it as String line = nextLine(); but when I had this in it didn't even allow me to type anything.

3) If you say yes to continue and you reenter the same low and high numbers the random number is always the same. For example I ran it 4 times with the low = 1 and high = 3 and the random number was always 3. Is there a way to make it randomize a new number every time they say yes to playing again?

BIG THING:
"GameInfo class. Your program should contain a class called GameInfo that will be used to store the following data during program execution: low and high range values, number of guesses allowed, number of guesses taken, did the player win or lose. Make sure you write get/set methods and at least one constructor."

I know how to make the new class and stuff. Just confused on how I would get the values in the guess class over to the GameInfo class so I can print to a file.

The files format is like this:
Won Hgh Low Alw Tkn

I need to put in the info for every time they play game until the program ends.




Code:
import java.util.Scanner;
import java.io.PrintStream;
import java.util.Random;
 
public class Guess {
	
	
	
    
    public static void main(String[] args) {
    	
    	int high;
    	int low;
    	int guess;
    	int guesses;
		int rNum;
		int g;
    	
    		Scanner input = new Scanner(System.in);
    		
    		System.out.println("Number Guessing Program");
    		System.out.println("------------------------\n");
    		do
    		{
    			
    			System.out.printf("\nEnter the low value: ");
    			low = input.nextInt();
    			System.out.printf("Enter the high value: ");
    			high = input.nextInt();
    			System.out.printf("Enter the number of guesses allowed: ");
    			guesses = input.nextInt();
    		
		
				Random rand = new Random();
				rNum = rand.nextInt(high-low) + (low+1);
				
					for (int c = 0; guesses > c; guesses--)
					{
						System.out.printf("Enter a guess (%d-%d): ",low,high);
						guess = input.nextInt();
						
							if (guess != rNum)
							{
								System.out.printf("Incorrect guess. You have %d guess left\n",(guesses-1));
							}
							else
							{
								System.out.printf("You won!!!\n\n");
									break;
							}
							
							if (guesses == 1)
								System.out.printf("You lost!!!");
						
					
					}
			
				System.out.printf("\nDo you want to keep playing (Y/N): ");
    			g = input.nextInt();
    		} while ((g == 1) || (g == 1));
    		System.out.println("\nGame over. Thank you\n\n");
    	
    		

    	
	}
}

What it should look like:
output.png



Thanks to all that help =D :hello:
 
Solution
So your code is such that the majority of the game logic is in a single method - your main method. Whilst this can be ok for small programs it's not best practice to do so.

What you really want to do is push this logic into a class somewhere perhaps a new class called Game. In your main method create a new instance of Game and call start on it. Obviously in your Game class you'll need to implement a start method! The Game class should have a private member that's the GameInfo.

Within your start method you can look for "y" to start again. In this scenario you could call a method called say reset which would:
1) Set the private gameInfo variable to an new GameInfo instance
2) Call the start method again

Does that make sense?

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
UPDATED CODE:

Still need help on all the SMALL THING from above. Also not sure how to get the how many guesses were taken for my log.

Guess Class
Code:
import java.util.Scanner;
import java.io.PrintStream;
import java.util.Random;
 
public class Guess {
	
	
	
    
    public static void main(String[] args) {
    	
    	int high;
    	int low;
    	int guess;
    	int guesses;
		int rNum;
		int won;
		int hgh;
		int lw;
		int allow;
		int taken;
		int g;
		int p = 0;
		PrintStream out;
		GameInfo game = new GameInfo();
    	
    	Scanner input = new Scanner(System.in);
    		
    	System.out.println("Number Guessing Program");
    	System.out.println("------------------------\n");
    	
    	try
    	{
    		out = new PrintStream ("gamelog.txt");
    		out.println("Won   Hgh   Low   Alw   Tkn");
    	
    	
    		do
    		{
    			
    			System.out.printf("\nEnter the low value: ");
    			low = input.nextInt();
    			System.out.printf("Enter the high value: ");
    			high = input.nextInt();
    			System.out.printf("Enter the number of guesses allowed: ");
    			guesses = input.nextInt();
    			game.setAllowed(guesses);
    			allow = game.getAllowed();
    		
		
				Random rand = new Random();
				rNum = rand.nextInt(high-low) + (low+1);
				
					for (int c = 0; guesses > c; guesses--)
					{
						System.out.printf("Enter a guess (%d-%d): ",low,high);
						guess = input.nextInt();
						
							if (guess != rNum)
							{
								System.out.printf("Incorrect guess. You have %d guess left\n",(guesses-1));
							}
							else
							{
								System.out.printf("You won!!!\n\n");
								p = 1;
									break;
							}
							
							if (guesses == 1)
								System.out.printf("You lost!!!");
								
							
					}
					
				game.setHigh(high);
				game.setLow(low);
				game.setTaken(guesses);
				game.setWon(p);
						
				hgh = game.getHigh();
				low = game.getLow();
				taken = game.getTaken();
						
				if (p == 0)
				{
					out.println("No" + "    " + hgh + "    " + low + "    " + allow + "     " + taken);
				}
				else
				{
					out.println("Yes" + "    " + hgh + "    " + low + "    " + allow + "     " + taken);
				}
			
				System.out.printf("\nDo you want to keep playing (Y/N): ");
    			g = input.nextInt();
    		} while ((g == 1) || (g == 1));
    		System.out.println("\nGame over. Thank you\n\n");
    	
    		out.close();
    	}
    	
    	catch (Exception e)
    	{
    		System.out.println("ERROR: Could not open file!");
    	}

    	
	}
}


GameInfo Class
Code:
class GameInfo {
	private int won;
	private int high;
	private int low;
	private int allowed;
	private int taken;
	
	GameInfo(){
		won = 0;
		high = 0;
		low = 0;
		allowed = 0;
		taken = 0;
	}
	
	public void setWon(int newWon)
	{
		won = newWon;
	}
	
	public void setHigh(int newHigh)
	{
		high = newHigh;
	}
	
	public void setLow(int newLow)
	{
		low = newLow;
	}
	
	public void setAllowed(int newAllowed)
	{
		allowed = newAllowed;
	}
	
	public void setTaken(int newTaken)
	{
		taken = newTaken;
	}
	
	public int getWon()
	{
		return won;
	}
	
	public int getHigh()
	{
		return high;
	}
	
	public int getLow()
	{
		return low;
	}
	
	public int getAllowed()
	{
		return allowed;
	}
	
	public int getTaken()
	{
		return taken;
	}
	
	
	
}
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Figured out everything, just wondering if you pick Y to play again, is there a way to clear all the values? Here's the code to look at.

Code:
import java.util.Scanner;
import java.io.PrintStream;
import java.util.Random;
 
public class Guess {
	
	
	
    
    public static void main(String[] args) {
    	
    	int high;
    	int low;
    	int guess;
    	int guesses;
		int rNum;
		int won;
		int hgh;
		int lw;
		int allow;
		int taken;
		String s1;
		int gues = 0;
		int p = 0;
		PrintStream out;
		GameInfo game = new GameInfo();
    	
    	Scanner input = new Scanner(System.in);
    		
    	System.out.println("Number Guessing Program");
    	System.out.println("------------------------\n");
    	
    	try
    	{
    		out = new PrintStream ("gamelog.txt");
    		out.println("Won   Hgh   Low   Alw   Tkn");
    	
    	
    		do
    		{
    			
    			System.out.printf("\nEnter the low value: ");
    			low = input.nextInt();
    			System.out.printf("Enter the high value: ");
    			high = input.nextInt();
    			System.out.printf("Enter the number of guesses allowed: ");
    			guesses = input.nextInt();
    			game.setAllowed(guesses);
    			allow = game.getAllowed();
    		
		
				Random rand = new Random();
				rNum = rand.nextInt(high-low) + (low+1);
				
					for (int c = 0; guesses > c; guesses--)
					{
						System.out.printf("Enter a guess (%d-%d): ",low,high);
						guess = input.nextInt();
						
							if (guess != rNum)
							{
								System.out.printf("Incorrect guess. You have %d guess left\n",(guesses-1));
							}
							else
							{
								System.out.printf("You won!!!\n\n");
								p = 1;
								gues++;
									break;
							}
							
							if (guesses == 1)
								System.out.printf("You lost!!!");
								
						gues++;	
					}
					
				game.setHigh(high);
				game.setLow(low);
				game.setTaken(gues);
				game.setWon(p);
						
				hgh = game.getHigh();
				low = game.getLow();
				taken = game.getTaken();
						
				if (p == 0)
				{
					out.println("No" + "    " + hgh + "    " + low + "    " + allow + "     " + taken);
				}
				else
				{
					out.println("Yes" + "    " + hgh + "    " + low + "    " + allow + "     " + taken);
				}
			
				System.out.printf("\nDo you want to keep playing (Y/N): ");
    			s1 = input.next();
    			
    		} while (s1.equalsIgnoreCase("y"));
    		System.out.println("\nGame over. Thank you\n\n");
    	
    		out.close();
    	}
    	
    	catch (Exception e)
    	{
    		System.out.println("ERROR: Could not open file!");
    	}

    	
	}
}

GAME INFO
Code:
class GameInfo {
	private int won;
	private int high;
	private int low;
	private int allowed;
	private int taken;
	
	GameInfo(){
		won = 0;
		high = 0;
		low = 0;
		allowed = 0;
		taken = 0;
	}
	
	public void setWon(int newWon)
	{
		won = newWon;
	}
	
	public void setHigh(int newHigh)
	{
		high = newHigh;
	}
	
	public void setLow(int newLow)
	{
		low = newLow;
	}
	
	public void setAllowed(int newAllowed)
	{
		allowed = newAllowed;
	}
	
	public void setTaken(int newTaken)
	{
		taken = newTaken;
	}
	
	public int getWon()
	{
		return won;
	}
	
	public int getHigh()
	{
		return high;
	}
	
	public int getLow()
	{
		return low;
	}
	
	public int getAllowed()
	{
		return allowed;
	}
	
	public int getTaken()
	{
		return taken;
	}
	
	
	
}
 

Rusting In Peace

Distinguished
Jul 2, 2009
312
0
19,060
So your code is such that the majority of the game logic is in a single method - your main method. Whilst this can be ok for small programs it's not best practice to do so.

What you really want to do is push this logic into a class somewhere perhaps a new class called Game. In your main method create a new instance of Game and call start on it. Obviously in your Game class you'll need to implement a start method! The Game class should have a private member that's the GameInfo.

Within your start method you can look for "y" to start again. In this scenario you could call a method called say reset which would:
1) Set the private gameInfo variable to an new GameInfo instance
2) Call the start method again

Does that make sense?
 
Solution

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Not really, we just are starting with multiple classes and inheritance. I am actually going to be posting a new program that has 3 classes. Probably going to need some help with it.