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