i am trying to create an interactive program that gives a prompt for the hangman game. there is meant to be a phrase book of about 100 movies. The phrase book is in an attachment. So far what i have done is made a I made a for loop to go through the length of the secret phrase and used an if/else statement to assign asterisk if the alphabet pick is right and underscore if right. Well i am not meant to use arrays so I am really stuck and i need help trying to show the correct picks and also putting a counter showing the number of wrong picks. In the end a total of wrong picks ends the game and a prompt is meant to ask if u want to replay. Thank you all for the anticipated help I am really stuck on this stuff
vobiahu 0 Newbie Poster
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
public class PhraseBank {
private ArrayList<String> phrases;
private int currentIndex;
private String topic;
/*
* When a wordbank is created a window will open to
* select the file with the phrases. Each line in the file
* will be treated as a single phrase.
*/
public PhraseBank(){
phrases = new ArrayList<String>();
currentIndex = -1;
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e){
System.out.println("Unable to change look and feel");
}
loadWords();
}
/*
* Call this method to get the next phrase.
* The returned String will contain upper case
* letters are spaces
*/
public String getNextPhrase(){
currentIndex = (currentIndex + 1) % phrases.size();
return phrases.get(currentIndex);
}
public String getTopic(){
return topic;
}
// to consider. Shoudl this be broken up into two methods?
private void loadWords() {
// get the file
JFileChooser chooser = new JFileChooser(".");
int retval = chooser.showOpenDialog(null);
File f = null;
chooser.requestFocusInWindow();
if (retval == JFileChooser.APPROVE_OPTION)
f = chooser.getSelectedFile();
// first line is assumed to be the topic!
// load the phrases
try{
Scanner s = new Scanner(f);
topic = s.nextLine();
while(s.hasNextLine()){
String phrase = trim(s.nextLine().trim());
phrases.add(phrase.toUpperCase());
}
}
catch(IOException e){
System.out.println("Unable to load phrases. Please exit.");
}
// if no values add in one
if(phrases.size() == 0)
phrases.add("HANGMAN");
Collections.shuffle(phrases);
}
// I assume nextLine is not null.
// Return a String with only characters and spaces.
// All other characters in org are removed.
private static String trim(String org) {
String result = "";
for(int i = 0; i < org.length(); i++){
char ch = org.charAt(i);
if( Character.isLetter(ch))
result += ch;
else if(ch == ' ')
result += '_';
}
return result;
}
}
stephen84s 550 Nearly a Posting Virtuoso Featured Poster
jay90 0 Newbie Poster
i am trying to create an interactive program that gives a prompt for the hangman game. there is meant to be a phrase book of about 100 movies. The phrase book is in an attachment. So far what i have done is made a I made a for loop to go through the length of the secret phrase and used an if/else statement to assign asterisk if the alphabet pick is right and underscore if right. Well i am not meant to use arrays so I am really stuck and i need help trying to show the correct picks and also putting a counter showing the number of wrong picks. In the end a total of wrong picks ends the game and a prompt is meant to ask if u want to replay. Thank you all for the anticipated help I am really stuck on this stuff
Hey,
Make sure you submit your code if you have completed for those who need help in future!
Peace,
johnnyboy1307 0 Newbie Poster
im actually working on this exact same problem, my difficulty is when you guess a letter correct i dont know how to properly reveal the letters in the secret message. here is my code
i
mport java.util.Scanner;
public class Hangman
{
public static void main(String[] args)
{
intro();
playGame();
}
public static void playGame()
{
PhraseBank phrases = new PhraseBank();
String topic = phrases.getTopic();
String wordToBeGuessed = phrases.getNextPhrase();
String alphabet = "abcdefghijklmnopqrstuvwxyz";
alphabet = alphabet.toUpperCase();
Scanner keyboard = new Scanner(System.in);
System.out.print("Im thinking of a ");
System.out.println(topic);
System.out.println();
System.out.print("The current phrase is ");
String a = "";
for (int i = 0; i < wordToBeGuessed.length(); i++)
{
char ch = wordToBeGuessed.charAt(i);
if (ch != '_')
{
a += "*";
}
else
{
a += '_';
}
}
System.out.print(a);
System.out.println();
System.out.print("The letters you have not guessed yet are:");
System.out.println();
System.out.print(alphabet);
System.out.println();
int wrongGuesses = 0;
int correctGuesses = 0;
String b = "";
while (wrongGuesses < 5)
{
System.out.print("Enter your next guess: ");
String guess = keyboard.next();
System.out.println();
System.out.print("You guessed ");
System.out.print(guess);
if (wordToBeGuessed.indexOf(guess) >= 0)
{
System.out.println();
System.out.print("That is present in the secret phrase.");
System.out.println();
System.out.print("You have made ");
System.out.print(wrongGuesses);
System.out.print(" wrong guesses");
System.out.println();
correctGuesses += 1;
for (int j = 0; j < a.length(); j++)
{
if (wordToBeGuessed.indexOf(guess) == j)
{
b += wordToBeGuessed.charAt(j);
}
else
{
if (wordToBeGuessed.charAt(j) != '_')
{
b += "*";
}
if (wordToBeGuessed.charAt(j) == '_')
{
b += "_";
}
}
}
System.out.print("The current phrase is ");
System.out.print(b);
System.out.println();
}
else
{
System.out.println();
System.out.print("That is not present in the secret phrase.");
wrongGuesses += 1;
System.out.println();
System.out.print("You have made ");
System.out.print(wrongGuesses);
System.out.print(" wrong guesses");
System.out.println();
System.out.print("The current phrase is ");
System.out.print(b);
System.out.println();
}
}
}
Edited by Nick Evan because: added code-tags
Alyoops 0 Newbie Poster
Can you please help me on doing a hangman java program because i am very close to failing my class and its my last year!! im desperate! can you please send me the code things?
Alyoops 0 Newbie Poster
The code above does not compile! it says reached end of file while parsing
Katana24 3 Junior Poster
Where's the intro() method located? It isn't in the hangman class or the PhraseBank class.
If it returns a reach end of file error you would need another } added to the end, most likely.
jwenting 1,889 duckman Team Colleague
Can you please help me on doing a hangman java program because i am very close to failing my class and its my last year!! im desperate! can you please send me the code things?
If you're "in your last year" and it's not kindergarten, it's not a good sign if you can't do this yourself.
You're either terminally stupid (in which case you deserve to fail) or terminally lazy (in which case you deserve to fail), so you deserve to fail.
Alyoops commented: an idiot for not knowing anything about programming? You must be the idiot making it seem like programming is life. I REALLY CANT DO IT BECAUSE I KNOW NOTHING ABOUT IT! So you either help me or just leave me alone +0
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.