Word Guessing Game

Trying to add to original code

Page 1 of 1

4 Replies - 1892 Views - Last Post: 30 March 2010 - 06:55 AM Rate Topic: -----

#1 draftcopy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-February 10

Word Guessing Game

Posted 29 March 2010 - 10:46 AM

Hello...

I'm having some trouble with different lines of code that is being incorporated into the original code. The first thing I am trying to do is cap the amount of guesses at 10. I added "limitedGuess = 10;" as the value. Further down the code you will see where I also added the extra code.

I am including the original program first, and then the program I added code to. Help with what I did wrong would be greatly appreciated. Thanks!


Original:


import java.util.Scanner;
 
 /**
  * Plays a word guessing game with one player.
  */
 public class WordGuess {

	public static void main(String[] args) {
		final String SECRET_WORD = "BRAIN";
		final String FLAG = "!";
		String wordSoFar = "", updatedWord = "";
		String letterGuess, wordGuess = "";
		int numGuesses = 0;
		Scanner input = new Scanner(System.in);
		
		/* begin game */
		System.out.println("WordGuess game.\n");
		for (int i = 0; i < SECRET_WORD.length(); i++) {
			wordSoFar += "-";								//word as dashes
		}
		System.out.println(wordSoFar + "\n");				//display dashes
	
		/* allow player to make guesses*/
		do {
			System.out.print("Enter a letter (" + FLAG + " to guess entire word): ");
			letterGuess = input.nextLine();
			letterGuess = letterGuess.toUpperCase();			
		
			/* increment number of guesses */
			numGuesses += 1;
			
			/* player correctly guessed a letter--extract string in wordSoFar up to the letter 
			 * guessed and then append guessed letter to that string. Next, extract rest of 
			 * wordSoFar and append after the guessed letter
			 */
			if (SECRET_WORD.indexOf(letterGuess) >= 0) {
				updatedWord = wordSoFar.substring(0, SECRET_WORD.indexOf(letterGuess)); 
				updatedWord += letterGuess;												
				updatedWord += wordSoFar.substring(SECRET_WORD.indexOf(letterGuess)+1, wordSoFar.length());
				wordSoFar = updatedWord;
			}
				
			/* display guessed letter instead of dash */
			System.out.println(wordSoFar + "\n");
		} while (!letterGuess.equals(FLAG) && !wordSoFar.equals(SECRET_WORD));
		
		/* finish game and display message and number of guesses */
		if (letterGuess.equals(FLAG)) {
			System.out.println("What is your guess? ");
			wordGuess = input.nextLine();
			wordGuess = wordGuess.toUpperCase();
		}
		if (wordGuess.equals(SECRET_WORD) || wordSoFar.equals(SECRET_WORD)) {
			System.out.println("You won!");		
		} else {
			System.out.println("Sorry. You lose.");
		}
		System.out.println("The secret word is " + SECRET_WORD);
		System.out.println("You made " + numGuesses + " guesses.");				
	}
}





Modified:


import java.util.Scanner;
 
 /**
  * Plays a word guessing game with one player.
  */
 public class WordGuess_Temp {

	public static void main(String[] args) {
		final String SECRET_WORD = "BRAIN";
		final String FLAG = "!";
		String wordSoFar = "", updatedWord = "";
		String letterGuess, wordGuess = "";
		int numGuesses = 0;  //shows number of guesses at end of game
		int limitedGuess = 10; //added limitedGuess = 10; to code
		Scanner input = new Scanner(System.in);
		
		/* begin game */
		System.out.println("WordGuess game.\n");
		for (int i = 0; i < SECRET_WORD.length(); i++) {
			wordSoFar += "-";								//word as dashes
		}
		System.out.println(wordSoFar + "\n");				//display dashes
	
		/* allow player to make guesses*/
		do {
			System.out.print("Enter a letter (" + FLAG + " to guess entire word): ");
			letterGuess = input.nextLine();
			letterGuess = letterGuess.toUpperCase();
			limitedGuess = 10; //added limitedGuess = 10; to code			
		
			/* increment number of guesses */
			numGuesses += 1;
			
			/* player correctly guessed a letter--extract string in wordSoFar up to the letter 
			 * guessed and then append guessed letter to that string. Next, extract rest of 
			 * wordSoFar and append after the guessed letter
			 */
			if (SECRET_WORD.indexOf(letterGuess) >= 0) {
				updatedWord = wordSoFar.substring(0, SECRET_WORD.indexOf(letterGuess)); 
				updatedWord += letterGuess;												
				updatedWord += wordSoFar.substring(SECRET_WORD.indexOf(letterGuess)+1, wordSoFar.length());
				wordSoFar = updatedWord;
			}
				
			/* display guessed letter instead of dash */
			System.out.println(wordSoFar + "\n");
		} while (!letterGuess.equals(FLAG) && !wordSoFar.equals(SECRET_WORD) || limitedGuess <= 10); // added "|| limtedGuess <= 10;" to code
		
		/* finish game and display message and number of guesses */
		if (letterGuess.equals(FLAG) || limitedGuess <= 10) //added "|| limitedGuess <= 10" to code{
			System.out.println("What is your guess? ");
			wordGuess = input.nextLine();
			wordGuess = wordGuess.toUpperCase();
		}
		if (wordGuess.equals(SECRET_WORD) || wordSoFar.equals(SECRET_WORD) && limitedGuess <= 10) //added "&& limitedGuess <= 10" to code {
			System.out.println("You won!");		
		} else {
			System.out.println("Sorry. You lose.");
		}
		System.out.println("The secret word is " + SECRET_WORD);
		System.out.println("You made " + numGuesses + " guesses.");				
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Word Guessing Game

#2 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Word Guessing Game

Posted 29 March 2010 - 11:29 AM

Ok, it looks like one of the conditions in your do-while loop is while limitedGuess <= 10. But you set limitedGuess to 10 INSIDE the loop, so EVERY time it runs, limitedGuess will equal 10. If you only want to allow 10 guesses, you should declare limitedGuess as 10 outside the loop, and decrement it each time the loop runs, then kill the loop at limitedGuess == 0;

Edit: Either that or leave limitedGuess alone and kill the loop when numGuesses == limitedGuess

This post has been edited by xor-logic: 29 March 2010 - 11:31 AM

Was This Post Helpful? 0
  • +
  • -

#3 draftcopy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-February 10

Re: Word Guessing Game

Posted 29 March 2010 - 11:43 AM

Hmmm... kinda lost. Could you point out where that correction would be made?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9039
  • View blog
  • Posts: 33,529
  • Joined: 27-December 08

Re: Word Guessing Game

Posted 29 March 2010 - 03:00 PM

The problem is here. Basically, limitedGuess will always be <= 10, as you set it to 10 at each iteration of your loop. You should consider declaring outside of the loop, and decrementing it on each iteration.
do { 
     ..code..
      limitedGuess = 10;

      ..code..
} while (!letterGuess.equals(FLAG) && !wordSoFar.equals(SECRET_WORD) || limitedGuess <= 10); // added "|| limtedGuess <= 10;" to code 


Was This Post Helpful? 0
  • +
  • -

#5 draftcopy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-February 10

Re: Word Guessing Game

Posted 30 March 2010 - 06:55 AM

View Postmacosxnerd101, on 29 March 2010 - 02:00 PM, said:

The problem is here. Basically, limitedGuess will always be <= 10, as you set it to 10 at each iteration of your loop. You should consider declaring outside of the loop, and decrementing it on each iteration.
do { 
     ..code..
      limitedGuess = 10;

      ..code..
} while (!letterGuess.equals(FLAG) && !wordSoFar.equals(SECRET_WORD) || limitedGuess <= 10); // added "|| limtedGuess <= 10;" to code 



Thanks for your input. I made some adjustments, but it's still not working as it should. Gonna have to play around some more.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1