2 Replies - 6590 Views - Last Post: 07 February 2012 - 12:15 PM Rate Topic: -----

#1 warmgoldfish   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-February 12

Having trouble summing my guess count in the Guessing Game.

Posted 07 February 2012 - 11:43 AM

//Kyle Diaz
//02/06/12
//Assignment 5

//This program plays a guessing game with a max range as maxNUM.

import java.util.*;

public class Guess{
   public static final int maxNUM = 5; //The range of the guessing game.
   
   public static void main (String [] args) {
      Scanner console = new Scanner(System.in);
      
		//myHaiku();
		
		
      int guesses = playGame(console);
		System.out.print("Do you want to play again? ");
      String option = console.next();
      char replay = option.charAt(0);
      
		int games = 1;
		while (replay == 'Y' || replay == 'y'){
         playGame(console);
			games++;
			System.out.print("Do you want to play again? ");
    		option = console.next();
     		replay = option.charAt(0);
		} 
		if (replay == 'N' || replay == 'n'){
				reportResults(console,guesses,games);
		}
		
		
     	//int gamesPlayed = repeat(console);      
      
      //reportResults(console, guesses, games);
      
   }
   
   //required: a method to play one game with the user (just one game, not multiple games)
   public static int playGame(Scanner console){
      Random r = new Random();
      int answer = r.nextInt(maxNUM) + 1;
      
      System.out.println("Winning number: " + answer);	//DELETE ME LATER
      System.out.println(); 										//DELETE LATER
      
      System.out.println("I'm thinking of a number between 1 and " + maxNUM + "...");
      
      int response = 0;
      int guess = 0;									//I changed this
      
      while (response != answer){
         System.out.print("Your guess? ");
         response = console.nextInt();
         if (response > answer){
            System.out.println("It's lower.");
         }	else if (response < answer){
            System.out.println("It's higher.");
         }
         if (response == answer){
            if(guess == 1){
               System.out.println("You got it right in " + guess + " guess");
            }
            else if (response == answer){
               System.out.println("You got it right in " + guess + " guesses");
            }
         }
		
      guess++;
		for (int i = 1; i <= 1; i++){				//things I changed
			guess = (guess*guess) % 10;			//things I changed
			}
      System.out.println("guess = " + guess + " -1"); //DELETE LATER
	   }
      
      return guess-1;								//needs to be changed?
   }
   
	//Not used
   public static int repeat(Scanner console){
      System.out.println();
      
      System.out.print("Do you want to play again? ");
      String option = console.next();
      char replay = option.charAt(0);
      int games = 0;
      while (replay == 'Y' || replay == 'y'){
         playGame(console);
         System.out.print("DO YOU WANT TO PLAY AGAIN? ");
         String nextGame = console.next();
         replay = nextGame.charAt(0);
         games++;
      }
      return games+1;
   }
   
   
   //required: a method to report overall results to the user
   public static void reportResults(Scanner console, int guesses, int gamesPlayed){
      
      System.out.println("Overall results:");
      System.out.println("    total games   = " + gamesPlayed);
      System.out.println("    total guesses = " + guesses);
      System.out.println("    guesses/game  = " + 1.0*guesses/gamesPlayed);
      System.out.println("    best game     = " );
      
   }
   
   //required: a method that introduces the game with your haiku
   public static void myHaiku(){
      System.out.println("Hello, new user.");
      System.out.println("Guess a number and I'll say:");
      System.out.println("Higher or lower.");
      System.out.println();
   }
   
   public static double round1(double round){
      return Math.round(round * 10.0) / 10.0;
   }
   
}





In this code, I am supposed to create a guessing game where the user inputs guesses and it compares it to the random number generated in my method. I am able to return the amount of guesses for one particular game, but I can't find a way to store the guesses for the game, then move on to the game and do it again.

If I'm unclear, below will be an example.

>>Play a guessing game
>>User guesses correctly in 5 tries
>>Do you want to play again?
>>Yes

>>Play guessing game
>>User guesses correctly in 2 tries
>>Do you want to play again?
>>No

>> games played: 2
>> guesses: 7****


****This is what I want to be produced in my output. However, I am always getting the guess amount for the first game rather than summing the guesses.

I am trying to use a for loop to try and store it, but I am having a lot of trouble. Thank you for any help.

This post has been edited by macosxnerd101: 23 July 2013 - 10:20 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble summing my guess count in the Guessing Game.

#2 ianian112   User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: Having trouble summing my guess count in the Guessing Game.

Posted 07 February 2012 - 11:51 AM

guesses += playGame(console);



(the one in your while loop)
Was This Post Helpful? 0
  • +
  • -

#3 blackcompe   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1159
  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: Having trouble summing my guess count in the Guessing Game.

Posted 07 February 2012 - 12:15 PM

I'm thinking of a number between 1 and 5...
Your guess? 5
It's lower.
Your guess? 3
It's lower.
Your guess? 1
You got it right in 4 guesses
Do you want to play again? 



That's not right. I got it in 3 tries. Here are things I'd change:

if (replay == 'N' || replay == 'n') {
            reportResults(console, guesses, games);
}



Is the conditional block necessary? If you broke the while loop, the user had to enter "no."

In your main method, you have duplicated code. That's generally means your design is wrong. Convert that while loop to a do/while loop, and you only have to write the prompt code once.

public static void main(String[] args) {
    //declare vars for game count, choice, and total guess count
    do {
        //play game, increment game count
        //prompt
        //get choice
    } while (choice equals 'Y');



You could make the console a private static class member, instead of passing it to your function. That would make for a better method signature. BTW, maxNUM should be private.

for (int i = 1; i <= 1; i++){				//things I changed
			guess = (guess*guess) % 10;			//things I changed
			}
      System.out.println("guess = " + guess + " -1"); //DELETE LATER



What is all that? I'd just remove it, and simply return the number of guesses the user made.

ianian112's comment will remedy your specific problem.

This post has been edited by blackcompe: 07 February 2012 - 12:16 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1