public class Hangman {
public static void ttt(String inputWord) {
int wordLength = inputWord.length();
String blanks = "";
for(int i = 0; i < wordLength; i++) {
blanks = blanks.concat("_ ");
}
System.out.println(blanks);
int points = 0;
int counter = 0;
ArrayList<String> usedChars = new ArrayList<String>();
ArrayList<String> allChars = new ArrayList<String>();
for(int i = 0; i < wordLength; i++) {
allChars.add(inputWord.substring(i, i + 1));
}
while(points < wordLength) {
Scanner reader = new Scanner(System.in);
System.out.println("Guess: ");
String guess = reader.nextLine();
int checker = 0;
for(int k = 0; k < usedChars.size(); k++) {
if(!usedChars.get(k).equals(guess)) {
checker = checker + 1;
}
else {}
}
if(checker == usedChars.size()) {
for(int i = 0; i < wordLength; i++) {
if(guess.equals(inputWord.substring(i, i + 1))) {
points = points + 1;
System.out.println("Correct!");
}
else {}
}
usedChars.add(guess);
ArrayList<String> tempList = new ArrayList<String>();
for(int i = 0; i < allChars.size(); i++) {
for(int k = 0; k < usedChars.size(); k++) {
if(!allChars.get(i).equals(usedChars.get(k))) {
tempList.add(allChars.get(i));
}
}
}
String inputWord2 = inputWord;
for(int i = 0; i < tempList.size(); i++) {
inputWord2 = inputWord2.replace(tempList.get(i), "_");
}
System.out.println(inputWord2);
System.out.println("Used letters: " + usedChars);
}
else {
System.out.print("Sorry, that letter has already been used\n");
}
counter = counter + 1;
if(counter == 5) {
points = wordLength;
}
}
System.out.println("The word was " + inputWord);
System.out.println("Game over");
}
public static void main(String[] args) {
ttt("barbarian");
}
}
I know that it takes a lot of effort to get through the code of people, especially mine, as it has been amateurish for so long, so I did my best to comment on all my code in order to try to explain what I'm thinking about. The executioner game is pretty sophisticated, and I'm just trying to type spaces, but with guessed letters.
So, for example, the secret word is java
I think j
output: j ___
my code actually gets this far, but for any additional guesses, the conclusion is simple: ______
My question will be essentially, how do I get this to replace the cycle, in order to actually continue working after the first time?
Again, I want to thank everyone who answers in advance and again when I read them tomorrow morning.