Hangman Program -- Need Help!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ilogboy
    New Member
    • Mar 2008
    • 1

    #1

    Hangman Program -- Need Help!

    Below is a copy of my Hangman project... I am missing a couple more functions and I don't know how to enhance it... Here are the things I want it to do: 1) I want it to determine the scores for the player. 2). I want it to display the letter that had already been guessed. 3) and I want it to determine when the winner wins...

    TRY THE CODE FORYOURSELF:

    import java.io.*;
    import java.util.Rando m;

    public class Pgm34
    {

    public static char[] badGuesses = new char[13];

    public static int badGuess = 0;

    public static String [] word =
    {
    "daily inspiration",
    "light of salvation",
    "faithful",
    "living hope",
    "holy",
    "peaceful mind",
    "truth prevails",
    "everlastin g love",
    };


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


    Random randomGenerator =new Random();


    for(int ii=0; ii<10; ii++)
    {
    int random=randomGe nerator.nextInt (word.length);

    playhangman(wor d[random]);

    }
    }

    //PRIMARY METHOD/LOOP

    public static void playhangman(Str ing word) throws IOException
    {


    for (;;)
    {
    String mask = createMask(word );

    while (!allDone(mask) )
    {
    System.out.prin tln();
    GBoard.plotBody (badGuess);
    GBoard.displayB oard();

    if(badGuess == 13)
    {
    System.out.prin tln();
    System.out.prin tln();
    System.out.prin tln
    ("TOO MANY WRONG GUESSES: "+
    "Your frog is totalled.");
    System.out.prin tln();
    System.exit(0);
    }

    System.out.prin tln("\t\t\t\t "+mask);
    System.out.prin tln();
    System.out.prin tln();
    System.out.prin t("\t\t\t\t Enter your guess: ");

    char guess=getGuess( word, mask);

    mask=checkLette r(word,guess,ma sk);

    if(alreadyBeenT ried(guess, mask)==true)
    {
    System.out.prin tln("try again");
    //System.out.prin tln("\07"); //THE BEEP
    continue;
    }
    else
    {
    System.out.prin tln("Not already been tried");
    }

    if(badGuessChec k(word, guess)==false)
    {
    badGuesses[badGuess]=guess;
    badGuess++;
    }

    System.out.prin tln(badGuessChe ck(word, guess));
    System.out.prin tln(badGuess);
    }
    }
    }


    public static boolean badGuessCheck(S tring word, char Guess)
    {
    boolean temp = false;

    for (int ii = 0; ii < word.length(); ii++)
    {

    if (Guess == word.charAt(ii) )
    {
    temp = true;
    }
    }
    return temp;
    }


    public static String createMask(Stri ng mask)
    {
    String result="";

    for (int ii = 0; ii < mask.length(); ii++)
    {
    if (mask.charAt(ii )==' ')
    {
    result=result+' ';
    } else {
    result=result+'-';
    }

    }return result;
    }



    public static char getGuess(String word, String mask) throws IOException
    {
    int result;

    for (;;)
    {
    result= System.in.read( );

    if (result=='!')
    {
    getProposal(wor d, mask);
    break;
    }

    if ((result>='a')& &(result<='z '))
    {
    break;
    }

    if ((result==13)|| (result==10))
    {
    continue;
    }


    System.out.prin t(" invalid input\n try again...");

    }return (char)result;
    }

    public static String readLn() throws IOException
    {
    StringBuffer sb = new StringBuffer();
    int temp = 0;

    for (;;)
    {
    temp = System.in.read( );

    if ((temp!=10) || (temp!=13))
    {
    sb.append((char )temp);
    }
    if ((temp==10) || (temp==13))
    {
    break;
    }
    }
    return sb.toString().t rim();
    }


    public static void getProposal(Str ing word, String mask) throws IOException
    {
    String response;

    System.out.prin tln();
    System.out.prin t("\t\t\t\t Enter your solution: ");

    for(;;)
    {
    response=readLn ();
    checkProposal(r esponse, word, mask);
    }
    }

    public static void checkProposal(S tring response, String word, String mask)
    {

    if (response.equal s(word))
    {
    System.out.prin tln("Your answer is correct!");
    }
    }


    public static boolean alreadyBeenTrie d(char guess, String mask)
    {

    boolean result = false;

    for(int ii=0; ii<badGuess; ii++)
    {
    if(guess==badGu esses[ii])
    {
    result = true;
    break;
    }
    }

    if (result == false)
    {
    for (int ii = 0; ii<mask.length( ); ii++)
    {
    if(mask.charAt( ii)==guess)
    {
    result=true;
    break;
    }
    }
    }
    return result;
    }


    public static String checkLetter(Str ing word,char guess, String mask)
    {
    String z = "";

    for(int ii=0; ii<word.length( ); ii++)
    {
    if((mask.charAt (ii)=='-')&&(word.charA t(ii)==guess))
    {
    z=z+guess;
    }

    else
    {
    if (mask.charAt(ii )!='-')
    {
    z=z+mask.charAt (ii);
    }

    else
    {
    z=z+'-';
    }
    }
    }
    return z;
    }

    public static boolean allDone(String mask)
    {
    for (int ii=0; ii < mask.length(); ii++)
    {
    if(mask.charAt( ii)=='-')
    {
    return false;
    }
    }
    return true;
    }
    }
    Last edited by ilogboy; Mar 18 '08, 07:43 PM. Reason: errata - wrong code was posted
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by ilogboy
    Below is a copy of my Hangman project... I am missing a couple more functions and I don't know how to enhance it... Here are the things I want it to do: 1) I want it to determine the scores for the player. 2). I want it to display the letter that had already been guessed. 3) and I want it to determine when the winner wins...

    TRY THE CODE FORYOURSELF:

    import java.io.*;
    import java.util.Rando m;

    public class Pgm34
    {

    public static char[] badGuesses = new char[13];

    public static int badGuess = 0;

    public static String [] word =
    {
    "daily inspiration",
    "light of salvation",
    "faithful",
    "living hope",
    "holy",
    "peaceful mind",
    "truth prevails",
    "everlastin g love",
    };


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


    Random randomGenerator =new Random();


    for(int ii=0; ii<10; ii++)
    {
    int random=randomGe nerator.nextInt (word.length);

    playhangman(wor d[random]);

    }
    }

    //PRIMARY METHOD/LOOP

    public static void playhangman(Str ing word) throws IOException
    {


    for (;;)
    {
    String mask = createMask(word );

    while (!allDone(mask) )
    {
    System.out.prin tln();
    GBoard.plotBody (badGuess);
    GBoard.displayB oard();

    if(badGuess == 13)
    {
    System.out.prin tln();
    System.out.prin tln();
    System.out.prin tln
    ("TOO MANY WRONG GUESSES: "+
    "Your frog is totalled.");
    System.out.prin tln();
    System.exit(0);
    }

    System.out.prin tln("\t\t\t\t "+mask);
    System.out.prin tln();
    System.out.prin tln();
    System.out.prin t("\t\t\t\t Enter your guess: ");

    char guess=getGuess( word, mask);

    mask=checkLette r(word,guess,ma sk);

    if(alreadyBeenT ried(guess, mask)==true)
    {
    System.out.prin tln("try again");
    //System.out.prin tln("\07"); //THE BEEP
    continue;
    }
    else
    {
    System.out.prin tln("Not already been tried");
    }

    if(badGuessChec k(word, guess)==false)
    {
    badGuesses[badGuess]=guess;
    badGuess++;
    }

    System.out.prin tln(badGuessChe ck(word, guess));
    System.out.prin tln(badGuess);
    }
    }
    }


    public static boolean badGuessCheck(S tring word, char Guess)
    {
    boolean temp = false;

    for (int ii = 0; ii < word.length(); ii++)
    {

    if (Guess == word.charAt(ii) )
    {
    temp = true;
    }
    }
    return temp;
    }


    public static String createMask(Stri ng mask)
    {
    String result="";

    for (int ii = 0; ii < mask.length(); ii++)
    {
    if (mask.charAt(ii )==' ')
    {
    result=result+' ';
    } else {
    result=result+'-';
    }

    }return result;
    }



    public static char getGuess(String word, String mask) throws IOException
    {
    int result;

    for (;;)
    {
    result= System.in.read( );

    if (result=='!')
    {
    getProposal(wor d, mask);
    break;
    }

    if ((result>='a')& &(result<='z '))
    {
    break;
    }

    if ((result==13)|| (result==10))
    {
    continue;
    }


    System.out.prin t(" invalid input\n try again...");

    }return (char)result;
    }

    public static String readLn() throws IOException
    {
    StringBuffer sb = new StringBuffer();
    int temp = 0;

    for (;;)
    {
    temp = System.in.read( );

    if ((temp!=10) || (temp!=13))
    {
    sb.append((char )temp);
    }
    if ((temp==10) || (temp==13))
    {
    break;
    }
    }
    return sb.toString().t rim();
    }


    public static void getProposal(Str ing word, String mask) throws IOException
    {
    String response;

    System.out.prin tln();
    System.out.prin t("\t\t\t\t Enter your solution: ");

    for(;;)
    {
    response=readLn ();
    checkProposal(r esponse, word, mask);
    }
    }

    public static void checkProposal(S tring response, String word, String mask)
    {

    if (response.equal s(word))
    {
    System.out.prin tln("Your answer is correct!");
    }
    }


    public static boolean alreadyBeenTrie d(char guess, String mask)
    {

    boolean result = false;

    for(int ii=0; ii<badGuess; ii++)
    {
    if(guess==badGu esses[ii])
    {
    result = true;
    break;
    }
    }

    if (result == false)
    {
    for (int ii = 0; ii<mask.length( ); ii++)
    {
    if(mask.charAt( ii)==guess)
    {
    result=true;
    break;
    }
    }
    }
    return result;
    }


    public static String checkLetter(Str ing word,char guess, String mask)
    {
    String z = "";

    for(int ii=0; ii<word.length( ); ii++)
    {
    if((mask.charAt (ii)=='-')&&(word.charA t(ii)==guess))
    {
    z=z+guess;
    }

    else
    {
    if (mask.charAt(ii )!='-')
    {
    z=z+mask.charAt (ii);
    }

    else
    {
    z=z+'-';
    }
    }
    }
    return z;
    }

    public static boolean allDone(String mask)
    {
    for (int ii=0; ii < mask.length(); ii++)
    {
    if(mask.charAt( ii)=='-')
    {
    return false;
    }
    }
    return true;
    }
    }
    If you like our experts here to post the complete code for you, will it is restricted....

    Just ask a specific question....Wit h your best effort....

    kind regards, ;-)
    sukatoa

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      *ahem* read reply #8 from this thread.

      kind regards,

      Jos

      Comment

      Working...