6 Replies - 1121 Views - Last Post: 03 October 2012 - 08:03 PM Rate Topic: -----

#1 pineapplexrpess  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-October 12

Guessing Game

Posted 03 October 2012 - 11:10 AM

I'm creating a guessing game and I basically have everything done. What I got stuck on was getting the game to reset and presenting overall results. I was able to get to ask if the user would like to play again and have them respond, but I'm not sure how to reset it without repeating the code again.

package Assignments;

import java.util.*;
import java.text.*;

public class GuessingGame 

{
	
	public static void main(String[] args)
	{
		Scanner scanner = new Scanner(System.in);
		Random random = new Random();
		
		String choice = scanner.next();
	
		/*--------Create Random Number--------*/
		int number = random.nextInt(101);
		
		/*--------Begin Game--------*/
		System.out.println("I'm thinking of a number between 1 and 100...." );
		System.out.println("Your Guess?");
		
		int guess = scanner.nextInt(); //number the player guesses
		int tries = 5; //number of guesses
		
		/*--------Win--------*/
		if(guess == number)
		{
			System.out.println("You guessed it in " + tries);
		}
		
		
		/*--------Guessed Wrong Number--------*/
		
		while(tries != 1 || choice.equals("y") || choice.equals("Y"))
		{	
			int tries1 = 5 - tries; //number of tries taken
			if (guess != number)
			{	
				if(guess < number)
				{
					tries -= 1;
					
					System.out.println("It's higher.");
					System.out.println("You have " + tries + " guess left.");
					System.out.println("Your guess?");
					
					guess = scanner.nextInt();
				}
				else if(guess > number)
				{
					tries -= 1;
					
					System.out.println("It's lower.");
					System.out.println("You have " + tries + " guess left.");
					System.out.println("Your guess?");	
					
					guess = scanner.nextInt();
				}
				else if(guess == number)
				{
					System.out.println("You guessed it in " + tries1);
				}	
				
			}
		}
		
		/*--------Out of Guesses--------*/
		if(tries == 1 || guess == number)
		{
			System.out.println("Sorry you lost this round.");
			System.out.println("Correct number was " + number + ".");
			
			//Play Again?
			System.out.println("Play again?(Y/N)");
			choice = scanner.next();
		
			if(choice.equals("y") || choice.equals("Y"))
			{
				//Yes
				System.out.println("fgdg");
			}
			else
			{
				//No
				System.out.println("Your Overall Results: ");
				System.out.println("Total Games: ");
				System.out.println("Guess/Game: ");
				System.out.println("Best Score: ");
			}
			
		
		
		
			
			
			
			
			
			
		
		}	
			
		

	}
		
}






Is This A Good Question/Topic? 0
  • +

Replies To: Guessing Game

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,075
  • Joined: 05-April 11

Re: Guessing Game

Posted 03 October 2012 - 11:16 AM

You could wrap your whole game in a loop
You should keep looping while the answer is 'y', else terminate :)

do {
  //Game
} while(choice.equals("y");



Also I think it is time for you to take a step further and learn about methods.
Was This Post Helpful? 0
  • +
  • -

#3 pineapplexrpess  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-October 12

Re: Guessing Game

Posted 03 October 2012 - 11:27 AM

I tried to create a method:

public void play()
{

}


and then have the game in there but i kept getting an error
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,075
  • Joined: 05-April 11

Re: Guessing Game

Posted 03 October 2012 - 11:31 AM

View Postpineapplexrpess, on 03 October 2012 - 06:27 PM, said:

I tried to create a method:

public void play()
{

}


and then have the game in there but i kept getting an error


We can't help much if you deleted your attempt and doesn't have the error message :P

We will gladly help you getting the method to work correctly, but it requires you tell/show us what it is doing wrong
Was This Post Helpful? 0
  • +
  • -

#5 pineapplexrpess  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-October 12

Re: Guessing Game

Posted 03 October 2012 - 11:35 AM

So this is what I began with:

ublic class GuessingGame 

{
	
	public GuessingGame()
	{
		Scanner scanner = new Scanner(System.in);
		Random random = new Random();
		
		String choice = scanner.next();
		
		public void play()
		{
			
		}





the error code I get is "Syntax error on token "void", @ expected"

And the error is on the public void play()
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,075
  • Joined: 05-April 11

Re: Guessing Game

Posted 03 October 2012 - 11:49 AM

The problem is that you are declaring your method inside your constructor

I see it can be hard to learn about classes and methods at the same time
A constructor will be called when the object is initialized. You can use the constructor to setup the instance
Usually you would set instance variables from the constructor

public class GuessingGame 
{
	//This is a constructor
    //You can initialize a new instance by writing GuessingGame myReferenceName = new GuessingGame();
	public GuessingGame()
	{
		Scanner scanner = new Scanner(System.in);
		Random random = new Random();
		
		String choice = scanner.next();
		
		//Call the method named 'play' the method takes 0 arguments
		play();
	}
	
	//This is a method declared inside the GuessingGame class
	public void play()
	{
		
	}
}




If you don't want to deal with classes at this time, then you can add a 'static' keyword to the method declaration, this will make you able to call it in your previous code
Then you can go and read about classes when you are ready.
You do not need to know what static means at this time, but later on when you have learned about classes, you will have to learn what it means


public class GuessingGame {

    public static void main(String[] args) {
        play(); //Call from static context
    }

    public static void play() {

    }
}


Was This Post Helpful? 0
  • +
  • -

#7 pineapplexrpess  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-October 12

Re: Guessing Game

Posted 03 October 2012 - 08:03 PM

I created a method called play and included everything for the game in it and then made another class called PlayGame that extends the GuessGame so I could call play() but it doesn't work and I just don't know what I'm doing wrong.

This is the GuessGame:
package GuessingGame;

import java.util.*;



public class GuessGame 
{
	static Scanner scanner = new Scanner(System.in);
	
	static Random random = new Random();
	private static String choice = scanner.next();
	
	
	public GuessGame()
	{
		
			
	}
	
	
	public void play()
	{
		int rand_num = random.nextInt(101); //Random Number
		
		/*-----Begin Game-----*/
		System.out.println("I'm thinking of a number between 1 and 100...");
		System.out.println("Yourkk Guess?");
		
		int guess = scanner.nextInt(); //number the player guesses
		int tries = 5; //number of tries remaining
		
		/*-----Win-----*/
		if(guess == rand_num)
		{
			System.out.println("You guessed it on the first try!");
			
			/*-----Does The User Want To Play Again-----*/
			System.out.println("Play Again?");
			String choice = scanner.next(); 
			
			if(choice.equals("y") || choice.equals("Y") || choice.equals("Yes") || choice.equals("yes"))
			{
				play();
			}
		}
		
		/*-----Guessed Wrong Number-----*/
		while(tries != 1)
		{
			int tries1 = 5 - tries; //Total Tries Taken
			if (guess != rand_num)
			{	
				if(guess < rand_num)
				{
					tries -= 1;
					
					System.out.println("It's higher.");
					System.out.println("You have " + tries + " guess left.");
					System.out.println("Your guess?");
					
					guess = scanner.nextInt();
				}
				else if(guess > rand_num)
				{
					tries -= 1;
					
					System.out.println("It's lower.");
					System.out.println("You have " + tries + " guess left.");
					System.out.println("Your guess?");	
					
					guess = scanner.nextInt();
				}
				else if(guess == rand_num)
				{
					System.out.println("You guessed it in " + tries1);
				}	
				
				/*-----Out of Tries/Guessed Correctly-----*/
				if(tries == 1 || guess == rand_num)
				{
					System.out.println("Sorry you lost this round.");
					System.out.println("Correct number was " + rand_num + ".");
					
					/*-----Play Again?-----*/
					System.out.println("Play again?(Y/N)");
					choice = scanner.next();
				}
			}
		
		
		}
	}	
	
}




and this is the PlayGame()

package GuessingGame;

public class PlayGame extends GuessGame
{
	public PlayGame()
	{
		super.play();
	}
	
	
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1