import java.io.*;
import java.util.*;
public class Hangman
{
public static void main(String[] args) throws IOException
{
Scanner in;
in = new Scanner(System.in);
String secretWord = "dinosaur";
char[] secretWordArray = new char[] {'d', 'i', 'n', 'o', 's', 'a', 'u', 'r'};
char[] ansArray = new char[8];
int lives = 6;
System.out.println("Starting game...\n\n");
System.out.println("Your secret word is: ");
//Displays the 'fillers': *******
for(int i = 0; i<secretWordArray.length; i++)
{
ansArray[i] = '*';
}
while(lives>0)
{
System.out.println("\nInput a letter: ");
String guessString = in.nextLine();
char guess = guessString.charAt(0);
if(secretWord.contains(guessString))
{
for(int j = 0; j<secretWordArray.length; j++)
{
if(secretWordArray[j] == guess)
{
ansArray[j] = guess;
System.out.println("The word contains the letter " + guess + "!");
System.out.println("Your word looks like this: " + ansArray);
}
} //end for-loop
} //end if (guess correct statement)
else
{
System.out.println("The word does not contain the letter " + guess + ".\nYou have " + lives + " lives left.");
System.out.println("Your word looks like this: " + ansArray);
lives--;
}
} //end while (lives loop)
} //end main
} //end class
Coding a simple hangman game for CS101 final project. Stuck on what to do next.
I used a char[] secretWordArray to breakdown the word into each letter. The char[] ansArray is a blank array which at the start of the program, is equal to * * * * * * * . If the user guesses 'o', ansArray is changed to * * * o * * * *
I'm not really sure if there's a better alternative than using a char[] arr to go about this? Or if my thought process is right and I'm just coding it wrong? Help pls