I have a game of Hangman. in this code it prints the letters guessed which are,
ID: 3620238 • Letter: I
Question
I have a game of Hangman.in this code it prints the letters guessed which are, in the code, "guessedletters." and it prints the letters to the screen with the letters one after another.
what i need is for the guesses to look like this:
let's say the secret word is "apple" and the user guesses "l"
this should appear on the screen after the user guesses. IF GUESSED CORRECTLY:
"_ _ _ l _"
if guessed incorrectly, continue from "Here is the board: _ _ _ _ _ " which is in the code.
I'm not sure if im supposed to incorporate that into the word.append and what I also can't figure out is how to make the letter guessed appear in the correct location. Such as in my example above.
here's the code.
import java.util.*;
import java.io.*;
public class Hangman
{
public static void main(String[] args) throws IOException
{
int words,n,i;
Random gen = new Random();
int maxTries = 7;
int wordLength,index;
boolean solved,found;
Scanner inScan = new Scanner(System.in);
char letter;
String secretWord="";
StringBuffer guessedLetters = new StringBuffer();
//the fileScan gets the first word for the game
Scanner fileScan = new Scanner(new FileInputStream("words.txt"));
words=fileScan.nextInt();
n=gen.nextInt(words);
for(i=0;i<=n;i++)
secretWord = fileScan.next();
//Creates a StringBuffer for the viewing of the letters guessed
StringBuffer word = new StringBuffer();
for(i = 0; i < secretWord.length(); i++)
word.append("_ ");
//System.out.println(word);
System.out.println("Welcom to Hangman!");
System.out.println("You can");
System.out.println(" 1. Start a new game.");
System.out.println(" 2. Quit.");
System.out.println("Please enter your choice: ");
//System.out.println("Your word is " + secretWord.length() + " letters long.");
System.out.println("Here's the board: ");
System.out.println(word);
solved=false;
while(maxTries > 0)
{
found=false;
System.out.println("The letters that you have guessed are: " + guessedLetters);
System.out.print("Please enter a letter to guess: ");
//Scanner inScan = new Scanner("words.txt");
letter = inScan.next().charAt(0);
guessedLetters.append(letter + " ");
index=0;
while(secretWord.indexOf(letter,index) != (-1))
{
found=true;
word.setCharAt(secretWord.indexOf(letter,index),letter);
index=secretWord.indexOf(letter,index)+1;
System.out.println("Great, You made a correct Guess!");
System.out.println("Let's keep going.");
System.out.println("The word: "+word);
}
if(!found)
maxTries--;
System.out.println("You have " + maxTries + " wrong guesses left.");
}
}
}
Explanation / Answer
please rate - thanks I added a couple of other niceties. I think what you asked for was already thereimport java.util.*;
import java.io.*;
public class Hangman
{
public static void main(String[] args) throws IOException
{int words,n,i;
Random gen = new Random();
int maxTries = 7;
int wordLength,index,foundletters;
boolean solved,found;
Scanner inScan = new Scanner(System.in);
//String letter;
char letter;
String secretWord="";
StringBuffer guessedLetters = new StringBuffer();
//the fileScan gets the first word for the game
Scanner fileScan = new Scanner(new FileInputStream("words.txt"));
words=fileScan.nextInt();
n=gen.nextInt(words);
for(i=0;i<=n;i++)
secretWord = fileScan.next();
//Creates a StringBuffer for the viewing of the letters guessed
StringBuffer word = new StringBuffer();
for(i = 0; i < secretWord.length(); i++)
word.append("_");
System.out.println(word);
foundletters=secretWord.length();
System.out.println("Welcome to the game of HANGMAN!!!!");
System.out.println("You will have 7 chances to guess what the word is.");
System.out.println("Your word is " + secretWord.length() + " letters long.");
System.out.println("The word: "+word);
solved=false;
while(maxTries > 0&&!solved)
{found=false;
System.out.print("Please enter a letter to guess: ");
//Scanner inScan = new Scanner("words.txt");
letter = inScan.next().charAt(0);
guessedLetters.append(letter + " ");
index=0;
while(secretWord.indexOf(letter,index) != (-1))
{found=true;
foundletters--;
word.setCharAt(secretWord.indexOf(letter,index),letter);
index=secretWord.indexOf(letter,index)+1;
System.out.println("correct guess ");
if(foundletters==0)
{solved=true;
System.out.println("congradulations on guessing the word");
}
}
System.out.println("The word: "+word);
if(!solved)
System.out.println(" The letters that you have guessed are: " + guessedLetters);
if(!found)
maxTries--;
if(!solved)
System.out.println("You have " + maxTries + " wrong guesses left. ");
}
if(!solved)
System.out.println("The word is "+secretWord);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.