OK, i'm working on a hangman game in cmd prompt. I'm about halfway done, but i have hit a wall regarding the secret word, and the user guess. Now mind you, my professor has not taught the class any array manipulation methods or techniques for finding specific char in an array of strings. My problem arises when the secret word is chosen randomly by the computer, I do not know how to "cover" the secret word in asterisks or whatever char. I imagine an algorithm where i use array.length and match the user input char to individual chars in the array, problem is i have wasted a whole day and i cant come up with the algorithm for that. Please help guys, this is due in a few days. Oh, and related, i also cant figure out how to cover the letters user has guessed already. I print the alphabet horizontally and i dont know how to cover individual letters.
This is my code so far. Thanks is advance.
package hangmangame;
import java.util.Scanner;
import java.util.Random;
import java.io.*;
import java.util.*;
import java.lang.*;
public class HangManGame
{
public static void main(String[] args) throws IOException
{
HangManStates states = new HangManStates();
int state1 = 1;
int state2 = 2;
int state3 = 3;
int state4 = 4;
int state5 = 5;
int state6 = 6;
char letter;
String[] copyFrom = {"*"};
String[] newSecretWord = new String[10];
String[] letters = {"a", "b", "c", "d", "e", "f", "g" ,"h", "i" ,"j", "k", "l" ,"m" ,"n" ,"o" ,"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
String[] phrase = { "alabama", "alaska", "arizona", "arkansas",
"california", "colorado", "connecticut", "delaware",
"florida", "georgia", "hawaii", "idaho",
"illinois", "indiana", "iowa", "kansas",
"kentucky", "louisiana", "maine", "maryland",
"massachusetts", "michigan", "minnesota", "mississippi",
"missouri", "montana", "nebraska", "nevada",
"new hampshire", "new jersey", "new mexico", "new york",
"north carolina", "north dakota", "ohio", "oklahoma",
"oregon", "pennsylvania", "rhode island", "south carolina",
"south dakota", "tennessee", "texas", "utah",
"vermont", "virginia", "washington", "west virginia",
"wisconsin", "wyoming"};
int pos = phrase.length;
String tryAgain;
String answer;
Scanner userInput = new Scanner(System.in);
do{
int numOfPhrases = phrase.length;
Random randomWord = new Random();
int rand1 = randomWord.nextInt(numOfPhrases);
String word = phrase[rand1];
String wordCover = " ";
//System.arraycopy(copyFrom, 0, word, 0, 7);
do{
System.out.print("I am thinking of a phrase.\n");
states.initialState();
System.out.print("The current Word is: " + word + " \n");
System.out.print("The letters you have not guessed are: \n");
for(int index = 0; index < letters.length; index++)
{
System.out.print(letters[index] + " ");
}
System.out.print("\nEnter a letter: "); //USER ENTERS GUESS HERE
answer = userInput.nextLine();
if(!(answer.equals(word)))
{
System.out.print("Try again\n");
continue;
}
else if(answer.equals(word))
{
System.out.print("You got it\n");
break;
}
}while(!(answer.equals(word)));
System.out.print("Play again? yes/no\n");
tryAgain = userInput.nextLine();
if(tryAgain.equals("no"))
{
System.out.print("thanks for playing\n");
}
}while(tryAgain.equals("yes"));
}
}