Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Guessing game syntax help needed

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Guessing game syntax help needed

    Hi Everyone,

    I am new to java and the assignments are are fairly simple for someone more experienced. I am currently having trouble with accumulating my variables for tallying the number of guesses of each color in the place that I need them to. I am also having trouble with my random number generator. I know that it is only selecting one 1 random number then using that 1 number through all 10 iterations of the loop when I need it to generate a new number for each guess. The actual specs in the books words is contained within the jpg. Thank you for any advice.

    20130327_111310.jpg


    import java.util.Random;
    import java.util.Scanner;
     
     
    public class ESPgame {
    	String colorInput, computerColor;
    	int computerNum, correct, incorrect;
    	int redGuess, greenGuess, blueGuess, orangeGuess, yellowGuess;
    	Scanner keyboard = new Scanner(System.in);
    	Random rand = new Random();
    	public static void main(String[] args) {
     
     
    		ESPgame newGame = new ESPgame();
    		newGame.welcomeGamer();
    		newGame.inputColor();
    		newGame.computerColor();
    		newGame.colorGuess();
    		newGame.reportGame();
    		System.exit(0);
    	}
     
     
     
    	public void welcomeGamer(){
     
    		System.out.println("Welcome to the ESP Game!"+
    							"\nTry to guess the same color as the computer. There are 10 attempts.");
    	}
     
    	public void inputColor(){
    	System.out.println("Please enter your choice of color: "+
    						"\nRed" +
    						"\nGreen" +
    						"\nBlue" +
    						"\nOrange" +
    						"\nYellow");
     
    	colorInput = keyboard.nextLine();
     
    	}
     
    	public void computerColor(){
     
    		computerNum = rand.nextInt(4);
     
    		if (computerNum == 0){
    			computerColor = "red";
    		}
    		else if(computerNum == 1){
    			computerColor = "green";
    		}
    		else if(computerNum == 2){
    			computerColor = "blue";
    		}
    		else if(computerNum == 3){
    			computerColor = "orange";
    		}
    		else if(computerNum == 4){
    			computerColor = "yellow";
    		}
    	}
     
    	public void colorGuess() {		 
     
    		correct = 0; 
    		 incorrect = 0;
    		for(int i = 0; i < 10; i++){
     
    		if(colorInput.equalsIgnoreCase(computerColor)){
    			System.out.println("Lucky! You guessed the same color as the computer.");
    			correct ++;	
     
    		}
     
     
     
    		else {
    			System.out.println("Sorry! Guess again.");
    		incorrect ++;
     
    		}
    		colorTally();
    		inputColor(); 
    		}
    		keyboard.close();
    	} // end method	
     
     
    	public void colorTally() {
    		redGuess = 0;
    		greenGuess = 0;
    		blueGuess = 0;
    		orangeGuess = 0;
    		yellowGuess = 0;
     
    		if(colorInput.equalsIgnoreCase("red")){
    				redGuess ++;	
    			}
    	   else if(colorInput.equalsIgnoreCase("green")){
    		   		greenGuess ++;
    		}
    	   else if(colorInput.equalsIgnoreCase("blue")){
    		   blueGuess ++;
    	   }
    	   else if(colorInput.equalsIgnoreCase("orange")){
    		   orangeGuess ++;
    	   }
    	   else if(colorInput.equalsIgnoreCase("yellow")){
    		   yellowGuess ++;
    	   }
    	   else {
    		   System.out.println("Invalid Color");
    	   }
     
    	} // end method
     
     
     
    	   public void reportGame(){
     
    	System.out.println("The Results of the game are in!"+
    						"\nPlayer 1:"+
    						"\nCorrect guesses: " + correct  +
    						"\nIncorrect guesses: " + incorrect +
    						"\nRed guesses: "	+ redGuess +								
    						"\nGreen guesses: "	+	greenGuess +					
    						"\nBlue guesses: "  + blueGuess +
    						"\nOrange guesses: "  + orangeGuess +
    						"\nYellow guesses: "  + yellowGuess );
    	} // end method
     
    } // end class


  2. #2
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Guessing game syntax help needed

    You are setting the value of each colorGuess to 0 every time you enter the colorTally function this is clearing your saved data. You should have one place to initialize your variables.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Guessing game syntax help needed

    I have correcting the colorGuess variable. Where my real challenge lies now is that each time the program runs, my random object is only generating 1 number then giving the user 10 guesses to get it right, when it should generate a new number for each guess.

  4. #4
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Guessing game syntax help needed

    If the color needs to change after every guess then just move the function that gives you your random color into your for loop. That would cause it to regenerate each time. Seems simple or is there something i'm missing?
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Guessing game syntax help needed

    No it does make sense, thank you for your help!

Similar Threads

  1. Guessing game help
    By Norm in forum Loops & Control Statements
    Replies: 4
    Last Post: March 23rd, 2013, 07:54 AM
  2. Guessing Game
    By Spark2.0 in forum Object Oriented Programming
    Replies: 4
    Last Post: June 6th, 2012, 12:14 PM
  3. Guessing game help
    By np657 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 12th, 2012, 07:39 AM
  4. guessing game
    By scottey313 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2011, 02:30 PM
  5. Guessing Game
    By scottey93 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 7th, 2011, 02:50 PM