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.

Page 1 of 3 123 LastLast
Results 1 to 25 of 51

Thread: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    28
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    As the title suggests, I need help creating a simple text-based hangman game without using arrays or object-oriented programming. No classes, no StringBuilder, no arrays of any kind. Everything I need must be in the main method. I've searched for any potential solutions to my problems the past few days before I asked for help myself, but I couldn't find the solutions I needed.

    The goal of the game is to guess the correct letters within a hidden word, and eventually the entire word is revealed once the user guesses all correct letters. What I'm struggling most with at the moment is revealing the guessed letters one at a time, out of a series of dashes that represents the hidden word. So, for instance, say the secret word is "loops". The displayed word to the user would simply look like this: ----- with each letter represented as a single dash. When the user guesses a correct letter, the letter is then "revealed" at the correct index of the string of dashes. If the user guessed "p", the new displayed word would look like this: ---p- as that is where the "p" in "loops" should be. In addition to this, I also need to have the user provide which indices s/he would like to guess, and only loop over those indices to check if the letter falls within them. Honestly, I have no idea of how to go about this, and would greatly appreciate any advice. I've posted all the code I have below.

    Note: The set of words that are drawn from in order to play the game are in another class known as RandomWord, which I don't have access to. This is where the String variable secretWord comes from, but it isn't necessary to have that code provided in order to help me.

    This is how I would like my program to work on a basic level:
    1. The program generates a random word (called secretWord) from a separate class.
    2. The random word is printed as a series of dashes (called displayWord) in order to "hide" the word from the user.
    3. The user enters the amount of spaces within the word s/he would like to guess.
    4. While the number of guesses allowed hasn't reached 0, and the user hasn't guessed the word, the program will loop the game process:
    5. The program asks the user which letter s/he would like to guess.
    6. The program asks the user which indicies s/he would like to check (separated by spaces) within secretWord (such as indicies: 0 1 2 3).
    7. The user enters the letter s/he would like to guess on one line, and the indices s/he would like to check on another, and the program determines whether or not that letter is found within that range of indices within secretWord.
    8. If the letter is not found within that range of indicies within secretWord, the program removes 1 guess from the number of guesses remaining, and asks the user again what letter s/he would like to guess and keeps looping through that process.
    9. If the letter is found within secretWord, the program will determine at exactly which index the letter occurs, and replaces one of the dashes in displayWord with that letter at the specified index (the index matching that of secretWord).
    10. The game will loop this process until either the user has guessed all the letters within secretWord, or s/he runs out of guesses.
    11. At this point, the program will ask the user if s/he wants to play again.
    12. If the user enters "Y" then the program will start over, generating another secretWord and starting the process of guesses over again.
    13. If the user enters "N" then the program will close.

    My code:
    import java.util.Scanner;
    public class Hangman2 
    {
    	private static final boolean testingMode = true;
    	public static void main(String[] args) 
    	{
    		// Variable Declarations
    		char playAgain;
    		int numGames, numSpacesAllowed, roundScore, totalScore, guessesRemaining;
    		String displayWord, numSpaces, secretWord, guess, substringOne, substringTwo;
     
    		// Variable Initializations
    		guess = "";
    		numGames = 20;
    		totalScore = 0;
    		roundScore = 0;
    		playAgain = ' ';
    		displayWord = "";
    		substringOne = "";
    		substringTwo = "";
    		guessesRemaining = 20;
    		secretWord = RandomWord.newWord();
     
    		// Scanner Object Declaration
    		Scanner input = new Scanner(System.in);
     
    		// Bulk of the game
    		do
    		{
    			// Game Setup
    			System.out.println("The secret word is: " + secretWord);
    			System.out.print("The word is: ");
    			displayWord = secretWord.replaceAll(".", "-");
    			System.out.println(displayWord);
     
    			System.out.println();
    			System.out.print("Enter the number of spaces allowed: ");
    			numSpacesAllowed = input.nextInt();
     
    			while (numSpacesAllowed <= 0 || numSpacesAllowed > secretWord.length())
    			{
    				System.out.println("Invalid input. Try again.");
    				System.out.println();
    				System.out.print("Enter the number of spaces allowed: ");
    				numSpacesAllowed = input.nextInt();
    			}
     
    			while (guessesRemaining > 0 && !secretWord.equals(displayWord)) //While game parameters are within range
    			{
    				System.out.print("Enter the letter you want to guess: ");
    				guess = input.next().substring(0, 1);
     
    				if (secretWord.indexOf(guess) >= 0) //If guess is in secretWord
    				{
    					System.out.println();
    					System.out.println("Your guess is in the word!");
     
    					//VARIABLE OUTPUTS FOR DEBUGGING PURPOSES ONLY
    					System.out.println();
    					System.out.println("VARIABLE OUTPUTS FOR DEBUGGING PURPOSES ONLY");
    					System.out.println("index: " + secretWord.indexOf(guess)); //Print index
    					System.out.println("displayWord: " + displayWord); //Print value of displayWord before change
    					System.out.println();
     
    					if (secretWord.charAt(secretWord.indexOf(guess)) == guess.charAt(0)) //If characters match at correct index
    					{
    						substringOne = displayWord.substring(0, secretWord.indexOf(guess));
    						substringTwo = displayWord.substring(secretWord.indexOf(guess) + 1);
    						displayWord = substringOne + guess + substringTwo;
    					}
     
    					//VARIABLE OUTPUTS FOR DEBUGGING PURPOSES ONLY
    					System.out.println();
    					System.out.println("VARIABLE OUTPUTS FOR DEBUGGING PURPOSES ONLY");
    					System.out.println("secretWord: " + secretWord); //Print value of secretWord
    					System.out.println("displayWord: " + displayWord); //Print value of displayWord
    					System.out.println("index: " + secretWord.indexOf(guess)); //Print value of index
    					System.out.println("guess: " + guess); //Print value of guess
    					System.out.println("substringOne: " + substringOne); //Print value of substringOne
    					System.out.println("substringTwo: " + substringTwo); //Print value of substringTwo
    					System.out.println();
     
    					System.out.println("The updated word is: " + displayWord);
    					System.out.println("Guesses Remaining: " + guessesRemaining);
     
    				} 
    				else 
    				{
    					System.out.println();
    					System.out.println("Your letter was not found in the spaces provided.");
     
    					guessesRemaining--;
     
    					System.out.println("Guesses Remaining: " + guessesRemaining);
    					System.out.println();
    				}
     
    				roundScore = (guessesRemaining * 10) / numSpacesAllowed; //Calculates roundScore
     
    				if (secretWord.indexOf(guess) >= 0) //Calculates totalScore if guess is in secretWord
    				{
    					totalScore += roundScore;
    				} 
    				else //If guess isn't in secretWord
    				{
    					roundScore = 0;
    				}
     
    				System.out.println("Score for this round: " + roundScore);
    				System.out.println("Total Score: " + totalScore);
    				System.out.println();
    			}
     
    			if (secretWord.equals(displayWord))
    			{
    				System.out.println("You have guessed the word! Congratulations!");
    				System.out.print("Would you like to play again? Yes (y) or No (n) ");
    				playAgain = (input.next().toUpperCase()).charAt(0);
     
    				if (playAgain == 'N')
    				{
    					input.close();
    					System.exit(0);
    				}
     
    				System.out.println();
    			} 
    			else if (!secretWord.equals(displayWord))
    			{
    				System.out.println("Sorry, you failed to guess the word.");
    				System.out.print("Would you like to play again? Yes (y) or No (n) ");
    				playAgain = (input.next().toUpperCase()).charAt(0);
     
    				if (playAgain == 'N')
    				{
    					input.close();
    					System.exit(0);
    				}
     
    				System.out.println();
    			}
     
    			if (numGames == 0) 
    			{
    				System.out.println("Sorry, you failed to guess the word.");
    				input.close();
    				System.exit(0);
    			}
    		} while (playAgain == 'Y');
    	}
    }

    Here are some example outputs from the console as it exists now:
    The secret word is: ASCII
    The word is: -----

    Enter the number of spaces allowed: 5
    Enter the letter you want to guess: A
    Your guess is in the word!

    VARIABLE OUTPUTS FOR DEBUGGING PURPOSES ONLY
    index: 0
    displayWord: -----


    VARIABLE OUTPUTS FOR DEBUGGING PURPOSES ONLY
    secretWord: ASCII
    displayWord: A----
    index: 0
    guess: A
    substringOne:
    substringTwo: ----

    The updated word is: A----
    Guesses Remaining: 20
    Score for this round: 40
    Total Score: 40

    Enter the letter you want to guess: S
    Your guess is in the word!

    VARIABLE OUTPUTS FOR DEBUGGING PURPOSES ONLY
    index: 0
    displayWord: A----


    VARIABLE OUTPUTS FOR DEBUGGING PURPOSES ONLY
    secretWord: ASCII
    displayWord: A----
    index: 0
    guess: A
    substringOne:
    substringTwo: ----

    The updated word is: A----
    Guesses Remaining: 20
    Score for this round: 40
    Total Score: 80

    Enter the letter you want to guess: C
    Your guess is in the word!

    VARIABLE OUTPUTS FOR DEBUGGING PURPOSES ONLY
    index: 1
    displayWord: A----


    VARIABLE OUTPUTS FOR DEBUGGING PURPOSES ONLY
    secretWord: ASCII
    displayWord: AS---
    index: 1
    guess: S
    substringOne: A
    substringTwo: ---

    The updated word is: AS---
    Guesses Remaining: 20
    Score for this round: 40
    Total Score: 120
    Attached Images Attached Images
    Last edited by dataghost; March 24th, 2019 at 04:36 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    Can you copy the contents of the console from when you execute the program and paste it here so we can see what you have?
    Add some comments showing and describing what you want the output to be.

    I assume you are able to use all of the String class's methods.

    Can you add some comments to the code describing what you are trying to do?
    For example what is the logic for building the String to display after a match was found?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2019
    Posts
    28
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    Here's an image of what my console output looks like currently:

    Screen Shot 2019-03-21 at 9.05.51 PM.jpg

    I would like the output to instead look like this...

    The secret word is: primitive
    The word is: ---------
    Enter the number of spaces allowed: 9
    Please enter the letter you want to guess: m

    Your guess is in the word!
    The updated word is: ---m-----

    Please enter the letter you want to guess: t

    Your guess is in the word!
    The updated word is: ---m-t---

    Please enter the letter you want to guess: e

    Your guess is in the word!
    The updated word is: ---m-t--e

    ...and so on. The updated word is where my output is wrong. And yes, I'm allowed to use all String methods.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    Can you add the logic steps that the code is using to build the contents of displayWord as comments?

    Please copy and paste the contents of the command prompt window. No screen shot images.

    In Windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2019
    Posts
    28
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    I tried my best but I couldn't figure out how to post from the console directly. I'm using Eclipse IDE on Mac. I did post a copied sample from the console into the initial question though, and then added some comments to the code, I hope it helps

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    I still do not see the logic for how the displayWord is used. Can you describe what displayWord is supposed to contain and how its value is built?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    I'm using Eclipse IDE with Windows. I can right click, highlight, and copy from the console. You can't do that on a Mac?

    Regards,
    Jim

  8. #8
    Junior Member
    Join Date
    Mar 2019
    Posts
    28
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    displayWord is supposed to be the "hidden" version of the secret word that is generated randomly. It is displayed as a series of dashes that is equal in length to the secret word. I set up a for loop to print out a series of dashes that is equal to the length of the secret word (say, "loops") and I've called it displayWord. What I want this section of my program to do is this: if the user guesses a correct letter that is in the secret word, displayWord should change to reflect that guess. It should change to have the correctly guessed letter in the place where that letter should be (the correct index) according to the secret word. So the display word for "loops" would look like this: ----- and once the user has guessed a correct letter within displayWord, say, "p", then displayWord is changed to look like this: ---p-. After that, if they guess "l", then displayWord should be updated to look like this: l--p- and so on.

    --- Update ---

    Hey Jim, I did do that, but it just appears in standard text form at the end of my question.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    print out a series of dashes that is equal to the length of the secret word (say, "loops") and I've called it displayWord.
    How many dashes does displayWorld contain after the loop?
    How many should it contain?

    displayWord should change to reflect that guess.
    Ok, what does the program need to do to make that happen?
    Your post describes the desired output but does not describe what the program needs to do to make it happen.
    What steps should it take?
    What values does it need to create the new contents for displayWord?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Mar 2019
    Posts
    28
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    This is what I'm trying to figure out. I tried splitting it into substrings, like this:

    System.out.print("Enter your guess: ");
    String guess = input.next().substring(0, 1);
     
    for(int index = 0; index < secretWord.length(); index++)
    {
         if(secretWord.indexOf(guess) == index)
         {
              String substringOne = displayWord.substring(0, index);
              String substringTwo = displayWord.substring(index + 1);
         }
    }
     
    System.out.println("The updated word is: " + substringOne + guess + substringTwo);

    Where displayWord is a series of dashes equal to the length of secretWord, and secretWord is a randomly generated word. I kept getting StringIndexOutOfBoundsException errors when I attempted this. After the loop, dashes should contain the same number of dashes - 1, because the user has guessed one letter correctly, and that letter should be placed in the correct index in displayWord corresponding to the index of that letter in secretWord.
    Last edited by dataghost; March 22nd, 2019 at 02:43 PM.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    displayWord is a series of dashes equal to the length of secretWord,
    It only has one dash. Print it out to see what is in it.
    The loop that builds it needs fixing.

    I tried splitting it into substrings,
    Please explain the logic the code is supposed to follow.
    It helps to use paper and pencil to work out index values needed for using the substring method.

    Don't write any more code until you have worked out good logic.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Mar 2019
    Posts
    28
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    This snippet of code...
    for(int index = 0; index < secretWord.length(); index++)
    {
    	displayWord = "-";
    	System.out.print(displayWord);
    }
    ...prints out the correct number of dashes corresponding to the length of secretWord. Is there another way I should go about doing that? Or do you think I need to store it again in order to keep the correct number of dashes after that for loop runs?

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    displayWord = "-";
    That statement assigns ONE dash to displayWord every time the loop goes around. When the loop is finished, displayWord contains one dash.

    If you want to concatenate dashes, the use the += operator like you did here:
    displayWord += secretWord.charAt(index);
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    dataghost (March 22nd, 2019)

  15. #14
    Junior Member
    Join Date
    Mar 2019
    Posts
    28
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    Thank you! That makes a lot of sense, I overlooked that. I changed to a substring method in the for loop over displayWord because I thought it would help more, and I think I've made a bit of progress. Those changes are now reflected in the code I provided in my initial question at the top of the screen. Now when I print, it adds the guessed letter after the fourth dash is printed of the string of dashes. This is what my console is showing:

    The secret word is: debugging
    The word is: ---------
    Enter the number of spaces allowed: 9
    Enter the letter you want to guess: u

    Your guess is in the word!
    The updated word is: ---u-
    Guesses Remaining: 20
    Score for this round: 22
    Total Score: 440

    Enter the letter you want to guess: d

    Your guess is in the word!
    The updated word is: d----------
    Guesses Remaining: 20
    Score for this round: 22
    Total Score: 440

    I'm assuming that there's a problem with how I've created my index. Do you know what I might be able to do to remedy this?
    Last edited by dataghost; March 22nd, 2019 at 05:03 PM.

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    Do you know what I might be able to do to remedy this?
    How are you trying to debug the code? Add some print statements that print out the value of variables that are being changed and used so you can see exactly what the code is doing. The print out will help you see the problem.

    Have you used a paper and pencil to check the values that the code should be using?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #16
    Junior Member
    Join Date
    Mar 2019
    Posts
    28
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    I have used paper and pencil, but I'm still struggling with the logic behind it I suppose. In my new code, the problem is my substringTwo variable. It isn't printing out the correct number of dashes after the guessed letter, which I've concatenated to substringOne. I've been using the Eclipse debugger as well, which shows me what values the variables are taking on step by step, so I've identified substringTwo as a problem, but I'm not sure how I can fix it. It either adds too many or too few dashes after substringOne and the guessed letter.

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    I can't help you with using the debugger. If you use print statements that print the values of variables, the print out could be posted and I might be able to point out problems I see.

    Also as a start, you need to post the current code with the debugging print statements.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #18
    Junior Member
    Join Date
    Mar 2019
    Posts
    28
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    Here are some print statements for relevant variables:

    VARIABLE OUTPUTS FOR DEBUGGING PURPOSES ONLY
    Value of secretWord: algorithms
    Value of displayWord: -----------
    Value of index at end of string: 9
    Value of guess: r
    Value of substringOne: ----
    Value of substringTwo: --
    Value of updatedDisplayWord: ----r--

    i've updated the code in the initial post as well.
    Last edited by dataghost; March 22nd, 2019 at 06:33 PM.

  20. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    What is wrong with what was printed in post #18? Be sure to add comments to values that are not what is expected.

    More debug print outs needed.
    displayWord before and after changes
    the value of the for loop index
    The value of the arguments to the substring methods.

    Note: When printing a variable's value be sure to give its name: ("Value of" is redundant)
       System.out.println("theVariable="+theVariable);  // print value of theVariable

    The code needs some design work done and then comments describing the logic needs to be added.
    I've added some questions:
    					for (int index = 0; index < secretWord.length(); index++) //Loop over secretWord      WHY?? 
    					{
    						displayWord += "-";                 //????  why add dash here
    						if (secretWord.indexOf(guess) == index) //If indices match           Then what???
    						{
    							substringOne = displayWord.substring(0, index);
    							substringTwo = displayWord.substring(index);
    							updatedDisplayWord = substringOne + guess + substringTwo;     //  What is done with this?
                                                           // why continue.  Code only handles first match
    						}
    If you don't understand my answer, don't ignore it, ask a question.

  21. #20
    Junior Member
    Join Date
    Mar 2019
    Posts
    28
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    Here are new debug print statements:
    The secret word is: Java
    The word is: ----
    Enter the number of spaces allowed: 4
    Enter the letter you want to guess: v

    Your guess is in the word!

    VARIABLE OUTPUTS FOR DEBUGGING PURPOSES ONLY (Before change)
    index: 0
    displayWord: -


    VARIABLE OUTPUTS FOR DEBUGGING PURPOSES ONLY (After change)
    secretWord: Java
    displayWord: --
    index: 0
    guess: v
    substringOne:
    substringTwo:
    updatedDisplayWord:


    VARIABLE OUTPUTS FOR DEBUGGING PURPOSES ONLY (Before change)
    index: 1
    displayWord: --


    VARIABLE OUTPUTS FOR DEBUGGING PURPOSES ONLY (After change)
    secretWord: Java
    displayWord: ---
    index: 1
    guess: v
    substringOne:
    substringTwo:
    updatedDisplayWord:


    VARIABLE OUTPUTS FOR DEBUGGING PURPOSES ONLY (Before change)
    index: 2
    displayWord: ---


    VARIABLE OUTPUTS FOR DEBUGGING PURPOSES ONLY (After change)
    secretWord: Java
    displayWord: ----
    index: 2
    guess: v
    substringOne: --
    substringTwo: --
    updatedDisplayWord: --v--


    VARIABLE OUTPUTS FOR DEBUGGING PURPOSES ONLY (Before change)
    index: 3
    displayWord: ----


    VARIABLE OUTPUTS FOR DEBUGGING PURPOSES ONLY (After change)
    secretWord: Java
    displayWord: -----
    index: 3
    guess: v
    substringOne: --
    substringTwo: --
    updatedDisplayWord: --v--

    The updated word is: --v--
    Guesses Remaining: 20
    Score for this round: 50
    Total Score: 50

    In post #18, I need substringTwo to print as ------ (six dashes) which is the number of dashes afte the guessed letter "r". However, it only printed two dashes.

  22. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    I need substringTwo to print as ------ (six dashes) which is the number of dashes afte the guessed letter "r". However, it only printed two dashes.
    Are you talking about this statement:
    substringTwo = displayWord.substring(index);
    What was in displayWord?
    What was the value of index?

    Are those values what you expected from your paper and pencil tests?

    This doesn't look right:
    guess: v
    substringOne: --
    substringTwo: --
    updatedDisplayWord: --v--
    Shouldn't the updated value be: --v-?
    If you don't understand my answer, don't ignore it, ask a question.

  23. #22
    Junior Member
    Join Date
    Mar 2019
    Posts
    28
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    Yes, I'm talking about substringTwo = displayWord.substring(index). The value of displayWord is ----, the value of index changes as it loops through the length of secretWord (starting at 0, and then looping until it reaches an index that matches the guessed letter). And yes you're right, updatedDisplayWord should print as: --v- but that's my problem. It isn't coming out as desired and I'm not sure what to do to rectify that fact.

  24. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    the value of index changes as it loops
    Why is there a loop?
    The value of displayWord is ----
    No it's not. This statement is constantly changing it:
       displayWord += "-";

    You still have not described the necessary, logical steps the program needs to take to work.
    Continually changing the code without a design makes it very hard to get the program to work.

    What is supposed to be in displayWord? How and when should its contents be changed?
    Can you write some details on the use of displayWord?
    If you don't understand my answer, don't ignore it, ask a question.

  25. #24
    Junior Member
    Join Date
    Mar 2019
    Posts
    28
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    The lines:
    for (int index = 0; index < secretWord.length(); index++) //Loop over secretWord
    {
     
    	displayWord += "-";
    	if (secretWord.indexOf(guess) == index) //If indices match
    	{
    		substringOne = displayWord.substring(0, index);
    		substringTwo = displayWord.substring(index);
    		updatedDisplayWord = substringOne + guess + substringTwo;
    	}
    }
    are meant to loop through the length of secretWord, adding one to index and running the if loop within the for loop, until it reaches an index of secretWord that matches the user's guessed letter. The for loop, paired with the if loop within it, is intended to match the index of displayWord with the index of secretWord, and place the user's guessed letter at that index in displayWord, which is equal to a series of dashes that is the same length as secretWord. And you're right, the value of displayWord is constantly changing with the line: displayWord += "-"; I need its length in dashes to match the length of secretWord. I've added the steps the program needs to take in the initial post.

    displayWord is meant to act as a sort of representation for the user of the randomly generated word, secretWord. This way, the user doesn't know what the secretWord is, but can make educated guesses based upon what is revealed of displayWord as the game goes on and s/he makes correct guesses as to what letters exist within it. It is used to store a series of dashes that equals the length of secretWord, and must be updated every round of the game (every guess). After every correct guess, it is updated to reflect that guess, where the guessed letter replaces the dash that is in its index. If secretWord is "loops", displayWord stores: -----. Once the user guesses a correct letter, "p", it replaces the dash at index in displayWord 3 with "p" (because that is where p exists within secretWord). displayWord is then updated to: ---p- and so on as the user makes correct guesses.

  26. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with Simple Text-Based Hangman Game (No Arrays or Object-Oriented Programming)

    must be updated every round of the game (every guess).
    How should the code do that? The current code changes displayWord by appending dashes inside the loop.
    Also the current code does not save the updated String contained in updatedDisplayWord

    value of displayWord is constantly changing with the line: displayWord += "-"; I need its length in dashes to match the length of secretWord.
    displayWord was filled with dashes at the beginning of the program so that its length matched secretWord's length. Why continue filling it?
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 0
    Last Post: September 7th, 2017, 09:40 PM
  2. Hi, i need help about object oriented programming
    By mottogo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 9th, 2014, 07:40 AM
  3. Java Programming (Object Oriented Programming) Assignment
    By azmilPhenom in forum Object Oriented Programming
    Replies: 3
    Last Post: December 21st, 2013, 07:26 AM
  4. Object Oriented Programming
    By cruzer66 in forum Object Oriented Programming
    Replies: 0
    Last Post: November 20th, 2013, 03:14 PM
  5. Object oriented programming
    By jonnitwo in forum Object Oriented Programming
    Replies: 8
    Last Post: September 2nd, 2011, 12:18 PM