d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Long Shot. Ton Of Fg For Easy Hangman Help 400 Fg > Need Help Within 54 Minutes
Add Reply New Topic New Poll
Member
Posts: 7,319
Joined: Apr 20 2007
Gold: 2,383.33
Mar 25 2013 09:12am
heres whats required of me:
Write a Java class, Hangman, that can be used to play the game of Hangman. See a sample run of this game in
the file attached to this assignment named, GameOutput.pdf. Output from your application should be very
similar. Here is what your Hangman class should contain:
Five Attributes (instance variables):
• the secret word, a private String variable
• the disguised word, a private String variable, in which each unknown letter in the secret word is
replaced with a question mark (?). For example, if the secret word is abracadabra, and the letters a, b,
and e have been guessed, then the disguised word is displayed as ab?a?a?ab?a.
• the letters, in alphabetical order, that have been incorrectly guessed; a private String variable
• the number of guesses made so far, an int variable
• the number of incorrect guesses made so far, an int variable
Five Methods:
• initialize: a public void method that takes one String parameter as the secret word and initializes the
secret word and other instance variables to begin playing a round of Hangman.
• makeGuess: a private void method that accepts a parameter of type, char, and responds appropriately
to the guess that character, c, is in the secret word. This involves counting the guess, counting
incorrect guesses, recalculating the disguised word if the guess is correct, adding the character to the
letters guessed incorrectly (alphabetically) if the guess is incorrect, etc.
• accessor methods for the secret word and the disguised word: getSecretWord and getDisguisedWord
• boolean‐returning method, isFound, returns true if the secret word has been guessed
You may add other methods and/or instance variables to assist in implementing the game of Hangman. Be
sure that the output of you game looks much like the same output in GameOutput.pdf, except that your
application should work for any word chosen as the secret word. Your Hangman class should use a main
method as follows in order to play the game:

public static void main(String[] args) {
Hangman game = new Hangman();
game.initialize("Happiness");
System.out.println("Lets play a round of hangman.");
game.playGame();
}


Note that this application does not pick a truly secret word since the programmer chooses the word ahead of
time. This problem can easily be solved once we know how to read files. For now, having the programmer
choose the word this way will be fine, just make sure your code works for many different words.
When comparing your output to the sample, be sure to note the prompts made by the program, the format of
the disguised word, the display of the number of guesses, the number of incorrect guesses, the letters guessed
incorrectly (given in alphabetical order), the error messages for entering more than one letter and for entering
letters already guessed (both correct and incorrect), and the congratulations remark.





heres an example of what the output is supposed to look like


Lets play a round of hangman.
The disguised word is <?????????>
Guess a letter: e
Guesses made 1 with 0 wrong ()
The disguised word is <??????e??>
Guess a letter: o
Guesses made 2 with 1 wrong (o)
The disguised word is <??????e??>
Guess a letter: s
Guesses made 3 with 1 wrong (o)





heres my code:



public class Hangman

{
public static void main(String[] args)
{

char letter;
String s1;
private String secret;
private String disguisedWord;
private String guesses;
int count;
int countwrong;
boolean isFound;

/* main class **/
Scanner keyboard = new Scanner(System.in);
Hangman game = new Hangman();
game.initialize("happiness");
System.out.println("lets play a round of hangman.");
game.playGame();
}
/* intialize class = changes secret word to all question marks, sets boolean to false so game loops **/
public void initialize (String s1)
{
secret = s1;
diguisedWord = "";
for (int count=0; count < secret.length(); count++)
diguisedWord =+ "?";
isFound = false;
}

/* executes game: starts loop, guesses characters and runs method makeGuess **/
public void playGame()
{
System.out.println(" the disguised word is " + disguisedWord);
System.out.println(" guess a letter");
makeGuess();
}

private void makeGuess (char letter)
{
letter = keyboard.nextChar();
letter = guess.charAt(0);
guesses += letter;
if (secret.indexOf(letter) < 0)
{
countwrong++;
System.out.println("Guesses made " + count + " with " + countwrong + " wrong" + guesses);
}
else // letter is correct(process letter.)
count++;
getDisguisedWord();
{
if (secret.indexOf(letter) > 0)
{
disguisedWord.indexOf(letter) = letter;
if (disguisedWord.indexOf('?')<0)
{
isFound=true;
System.out.println(" congratulations, you found the secret word:");
return secret;
}
}
return diguisedWord;
}
}
}
}
}


heres the errors im getting while compiling:


----jGRASP exec: javac -g Hangman.java

Hangman.java:29: error: illegal start of expression
private String secret;
^
Hangman.java:30: error: illegal start of expression
private String disguisedWord;
^
Hangman.java:31: error: illegal start of expression
private String guesses;
^
Hangman.java:88: error: class, interface, or enum expected
}
^
4 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.


and when i attempt to run, i get :

The following file could not be parsed
"c:documents and settings\user\my documents\hangman.java"

proceeding before correcting is not recommended


then

no main methods, applets, or MIDlets found in file;


This post was edited by ouch617 on Mar 25 2013 09:13am
Member
Posts: 7,319
Joined: Apr 20 2007
Gold: 2,383.33
Mar 25 2013 09:13am
should be an easy fix but ive been working at it all night and cant figure why its not finding the main method
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Mar 25 2013 09:56am
Your code had a lot of errors so i just wrote the whole thing from scratch. Hope it's not too late.

HangMan.java
Code
import java.util.Scanner;

public class HangMan {

 private String secretWord;

 private String disguisedWord;

 private String letters;

 private int numberOfGuesses;

 private int numberOfIncorrectGuesses;

 public void initialize( String secretWord ) {
   this.secretWord = secretWord;
   this.disguisedWord = "";
   for ( int i = 0; i < secretWord.length( ); i++ ) {
     disguisedWord += "?";
   }
 }

 public void makeGuess( char guess ) {
   boolean match = false;
   for ( int i = 0; i < secretWord.length( ); i++ ) {
     if ( secretWord.charAt( i ) == guess ) {

       StringBuilder newString = new StringBuilder( disguisedWord );
       newString.setCharAt( i, guess );

       disguisedWord = newString.toString( );

       match = true;
     }
   }
   if ( !match ) {
     numberOfIncorrectGuesses++;
   }
 }

 public boolean isFound( ) {
   if ( disguisedWord.contains( "?" ) ) {
     return false;
   } else {
     System.out.println( "You win" );
     return true;
   }
 }

 public String getSecretWord( ) {
   return secretWord;
 }

 public String getDisguisedWord( ) {
   return disguisedWord;
 }

 public void playGame( ) {
   while ( !isFound( ) ) {
     System.out.println( "The disguised word is <" + disguisedWord + ">" );
     numberOfGuesses++;
     System.out.println( "Guess a letter: " );
     makeGuess( new Scanner( System.in ).next( ).charAt( 0 ) );

     System.out.println( " Guesses made " + numberOfGuesses + " with "
         + numberOfIncorrectGuesses + " wrong" );
   }
 }

}


Main.java
Code
public class Main {
 public static void main( String[] args ) {
   HangMan game = new HangMan( );
   game.initialize( "happiness" );
   System.out.println( "Lets play a round of hangman." );
   game.playGame( );

 }
}



Here's the output


Guesses made 2 with 0 wrong
The disguised word is <h?pp?????>
Guess a letter:
a
Guesses made 3 with 0 wrong
The disguised word is <happ?????>
Guess a letter:
s
Guesses made 4 with 0 wrong
The disguised word is <happ???ss>
Guess a letter:
i
Guesses made 5 with 0 wrong
The disguised word is <happi??ss>
Guess a letter:
n
Guesses made 6 with 0 wrong
The disguised word is <happin?ss>
Guess a letter:
e
Guesses made 7 with 0 wrong
You win

This post was edited by labatymo on Mar 25 2013 09:59am
Member
Posts: 7,319
Joined: Apr 20 2007
Gold: 2,383.33
Mar 25 2013 10:03am
youre too late but im sending fg anyways. let me know your email so you can break down my errors and explain why yours is working? i get the same error while copying and pasting your code into my jgrasp:

no main methods, applets, or MIDlets found in file;
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Mar 25 2013 10:10am
Quote (ouch617 @ Mar 25 2013 12:03pm)
youre too late but im sending fg anyways. let me know your email so you can break down my errors and explain why yours is working? i get the same error while copying and pasting your code into my jgrasp:

no main methods, applets, or MIDlets found in file;


Did you add both the java files? I don't have any experience with jgrasp, but make sure you're running Main.java. Maybe try right clicking it to run. You could also copy and paste the main method from Main.java to HangMan.java so it will look like this...

Code
import java.util.Scanner;

public class HangMan {

 private String secretWord;

 private String disguisedWord;

 private String letters;

 private int numberOfGuesses;

 private int numberOfIncorrectGuesses;

 public void initialize( String secretWord ) {
   this.secretWord = secretWord;
   this.disguisedWord = "";
   for ( int i = 0; i < secretWord.length( ); i++ ) {
     disguisedWord += "?";
   }
 }

 public void makeGuess( char guess ) {
   boolean match = false;
   for ( int i = 0; i < secretWord.length( ); i++ ) {
     if ( secretWord.charAt( i ) == guess ) {

       StringBuilder newString = new StringBuilder( disguisedWord );
       newString.setCharAt( i, guess );

       disguisedWord = newString.toString( );

       match = true;
     }
   }
   if ( !match ) {
     numberOfIncorrectGuesses++;
   }
 }

 public boolean isFound( ) {
   if ( disguisedWord.contains( "?" ) ) {
     return false;
   } else {
     System.out.println( "You win" );
     return true;
   }
 }

 public String getSecretWord( ) {
   return secretWord;
 }

 public String getDisguisedWord( ) {
   return disguisedWord;
 }

 public void playGame( ) {
   while ( !isFound( ) ) {
     System.out.println( "The disguised word is <" + disguisedWord + ">" );
     numberOfGuesses++;
     System.out.println( "Guess a letter: " );
     makeGuess( new Scanner( System.in ).next( ).charAt( 0 ) );

     System.out.println( " Guesses made " + numberOfGuesses + " with "
         + numberOfIncorrectGuesses + " wrong" );
   }
 }

 public static void main( String[] args ) {
   HangMan game = new HangMan( );
   game.initialize( "happiness" );
   System.out.println( "Lets play a round of hangman." );
   game.playGame( );

 }
}


Then you can just delete the Main.java file. I only added Main.java because it's good coding practice to have the main method in it's own class.

This post was edited by labatymo on Mar 25 2013 10:10am
Member
Posts: 7,319
Joined: Apr 20 2007
Gold: 2,383.33
Mar 25 2013 10:21am
yea i keep rereading your code and mine, while yours is far more concise; i still see the required components in mine, the same main method is available; the functions are passed through....

when you say make a main.java. does that mean im supposed to save two separate instances as have them both called into the same program when i compile them? cause im just copying all source code into my compiler as is.

exactly what i pasted is exactly what was written in my source code (same for yours ) and theyre both returning the same error.
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Mar 25 2013 10:35am
Quote (ouch617 @ Mar 25 2013 12:21pm)
yea i keep rereading your code and mine, while yours is far more concise; i still see the required components in mine, the same main method is available; the functions are passed through....

when you say make a main.java. does that mean im supposed to save two separate instances as have them both called into the same program when i compile them? cause im just copying all source code into my compiler as is.

exactly what i pasted is exactly what was written in my source code (same for yours ) and theyre both returning the same error.


It's not necessary to have a separate file for the main method, but it's good to have it.

I think your IDE is trying to use the wrong file as the starting point, or it might not automatically detect the main method.

I'd say just download Eclipse. It's probably the best IDE out there for Java, and it will automatically find your main method.
Member
Posts: 7,319
Joined: Apr 20 2007
Gold: 2,383.33
Mar 26 2013 04:09am
downloaded eclipse and its miles better.


anyways i finally nailed it


my main was lacking curly braces and was out of order; i thought all methods could be called within each other; they have to be separate instead


import java.util.Scanner;
public class Hangman
{
char letter;
String s1;
String s2;
private String secret;
private String disguisedWord;
private String charactersGuessed = "";
int count=0;
int countwrong=0;
boolean isFound;
Scanner keyboard = new Scanner(System.in);

/* main method, creates new hangmang class, runs initialize method, and runs playgame method. **/
public static void main( String[] args)
{
Hangman game = new Hangman();
game.initialize("happiness");
System.out.println("Lets play a round of hangman.");
game.playGame();
}

/* intialize class = changes secret word to all question marks, sets boolean to false so game loops **/
public void initialize(String s1)
{
secret = s1;
disguisedWord = "";
for (int count=0; count < secret.length(); count++)
{
disguisedWord += "?";
isFound = false;
}
}

/* executes game: starts loop, guesses characters and runs method makeGuess **/
public void playGame()
{
while (! isFound)
{
System.out.println("the disguised word is " + disguisedWord);
System.out.println("guess a letter");
makeGuess();
}
}

/* fills in disguised question marks with characters, alphabetizes guessed corrections, and displays outputs. **/
private void makeGuess()
{
s2 = keyboard.next();
letter = s2.charAt(0);
if (s2.compareTo(charactersGuessed)<0)
{
charactersGuessed = letter + charactersGuessed;
}
else
{
charactersGuessed = charactersGuessed + letter;
}
//System.out.println("testing for errors" + secret.indexOf(letter));
if (secret.indexOf(letter) < 0)
{
countwrong++;
System.out.println("Guesses made " + count + " with " + countwrong + " wrong and letters guessed:" + charactersGuessed);
}
else // letter is correct(process letter.)
{
count++;
System.out.println("Guesses made " + count + " with " + countwrong + " wrong and letters guessed:" + charactersGuessed);
replaceGuessedLetter(letter);
}
}

/*replaces guessed letter's question marks with correct letters. **/
public void replaceGuessedLetter(char guessedLetter)
{
if (secret.indexOf(letter) >= 0)
{
String temp= disguisedWord;
disguisedWord= "";
for (int i=0; i<secret.length(); i++)
{
//System.out.println("debugged " + secret.charAt(i));
if (secret.charAt(i)== guessedLetter)
{
disguisedWord+= guessedLetter;
}
else
{
disguisedWord+= temp.charAt(i);
}
}

if (disguisedWord.indexOf('?')<0)
{
isFound=true;
System.out.println(" congratulations, you found the secret word:");
System.out.println(secret);
}
}

}

}
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll