CS3 Project                   - HANGMAN
Objective:

    r    Create a Hangman console game
    o    Create a console application that allows the user to add/delete words to be used in the game

Deadline:

    r    Second week of January 2011 (first meeting of the week)

Other details:

    o    Lab pairings will be in effect.
    o    I   will give you the entire 1" week of January to do this but you might want to start during the
         brea k
    o    There will be 2 modules so you can work independently at the start then combine the modules
         at the end or work on both modules together.
    o    The emphasis will be handling inputs through console and external file     a-s   well as proper error
         handling.
    o    For any questions, email me at: paolo.santos@pshs.edu.ph or paolo.niccolo.santos@gmail.com



lnstructions:

    1.   Create a menu that displays:


         1. Manage Word Library
         2. Play Hdngman
         J. tlx] f,

         >l
    2.   When L is selected, the user is directed to module 1 (See next page).
    3.   When 2 is selected, the user is directed to module 2 (See page 3).
    4.   Selecting 3 ends the program.
Module 1: Creating the Word Library (File Reading/Writing Module)

     L.   Start by manually creating a text file containing the default list of words (each word
          separated by a new line). Name it wordlist.txt. The file should look like this:

          Merry
          Christmas
          Rudolph the red nosed reindeer



     2.   At the start of the module, load all the words into an array of Strings
     3.   Then, display the following menu:


          1. Display word library
          2. Add a word
          3. Delete a word
          4. Save current list
          5. Exit
     4.   When f. is selected, print the entire array of words.
     5.   When 2 is selected, prompt the user for a word then add it to the array.
     6.   When 3 is selected, print the entire array of words in this manner:


          I.   Merry
          2.   Christmas
          3.   Rudolph the red nosed reindeer



          Then prompt the user for a NIJMBER. Delete the word that corresponds to that number
          (example if the user enters 1, delete Merry from the array).


     7.   When 4 is selected, save the words contained in the array to wordlist.txt
     8.   When 5 is selected, return the user to the previous menu.
Module 2: Creating the game (Console Inputs Module)

        1.   You will need the file HangmanWord.java (located at the end of this document) for this
             module.
        2.   Start with an arrav of words and use the random class to generate a random number to
             randomly pick a word from the list.
        3.   Once a word is selected, you now need to transform it into a HangmanWord
                 a. To create a HangmanWord, simply input the String into the constructor:
                       new HangmanWord(<String value>);
                 b.    How to use a HangmanWord Object:
                           i.    When the object is created, it automatically geneterates a String that
                                 contains the blanks of the word. Meaning if you entered new
                                 HangmanWord("hello"), it generates "           ".
                                                                         ----
                           ii.   To output the String of blanks, use the outputBlanksfl function.
                          iii.   You can use the use the guess(char letter)or guess(String letter)function to
                                 guess a letter that is contained in the word. lt outputs true if the letter is
                                 contained in the word, false otherwise. lt is CASE SENSITIVE. lf the letter is
                          .      contained in the word, the blank is replaced by the letter.
                          iv.    Calling the isGuessed0 function returns true if the word is successfully
                                 guessed, and false otherwise.
        4.   OnCe yOu've made       your hangman wOrd, set hOw many "lives" your user has tO guess the
             word.
        5.   Output the blanks

                                                               (example the word is snowman)

        6.   Prompt the user for an input (either String or char)
             >s
        7.   Use the guessfl     function of the HangmanWord o.bject
                 a.    lf it returns true, output the blanks


                 b.    lf it returns false, subtract a "life" from the user then output the blanks
                 c.    lf the word is fully guessed (no more blanks)go back to 1.
                 d.    lf the user is out of lives, output the word then go back to the main menu.
    '            e,    lf the user is not out of lives and the word still contains blanks, go back to 5.
public cfass Hanqmanword {
    private String inword;
    nriv,atc Strino outword;
     nri vafe i nt I etters I eft;

    nrrl.rlin       llanam:nfaTnrd/af   Vvgl4rrY
                                                    rina       r^rnrrl   t

             inword : word;
             outword : "";
             Ietters feft : 0;
             for(int i:0;     i < word.IengthO; i++;1
                  1f((word.charAt(i):: ' ') I | (word.charAt(i):= '. ',                                    I   I


                    (word.charAt(i):: ', ') | I (word.charAt(i):: 'e'1 |                               t


                    (word.charAt(i)::'l')   | I (word.charAt(i):: "")   |                              l


                    (word.charAt (i)::'"  )){
                       outword : outv/ord + word.charAt(i) ;
                  ) else {
                     outword : outword         ;
                     Ietters_left++;
                          )

                ]
     )


    public boofean giress (char letLer)                                       {

       boofean hasletter : false;
        for(int i = 0; i < inword.lengtho; i++11
             if ( (lnword. charAt (i) := letter) && (outword. charAt (i) :: '_')                                   )i
                   outhrord: outword.substring(0,i) + fetter +
                                                         outword. substri-ng ( i+1, outword. lenqth ( ) ) ;
                              L--T                   *     +vr,^.
                              IIOJ!V ^tt^, LgI
                                        L
                              letters_left--;

                ]


         .   return has{.etter;
     t
     t


     pub-Iic boo}ean guess (St ring letter ) {
             if(letter.lengthO    != 1) t
                   SysLem.out.println ("lnvalid guess. ")                           ;
                    return false;
                ]


             boolean hasLetter : false;
             for(int i:0;        i < inword.lengthO; i++11
                  if  ( (Ietter.equals (inword.substring(i, i+1) ) )                          &&
                     ("_".equals (outword. substring (i,i+t1 ; I 1                       1




                       outword : outword. substring (0, i) + fetter +
                              outword. substring (i+1, outword. length ( ) ) ;
                              hasLetter : true;
                              fetters_left--;
/

                                                                                       -


            return hasLetter;
    )



    public void seLWorl (String word)          {

        inword : word;
        outword : "";
             letters_left : O;
            for(int   i = 0; i < word.lengthO; i++11
                    if((word.charAt(i):= ' ') ll (word.charAt-(i)::'.')       ll
                            (word.charAt(i):= ', ', I I (word.charAt(i):: '?') i
                                                                               i


                            (word.charAt(i):= ttt; ll (word.charAt(i):: "'t, 1l
                            (word'charAt (i)::' " ) ) {
                      outword: outword + word.charAt(i) ;
                 l else   1


                    outword : outword + "_" ;
                    f ette rs_le ft ++;
                      l
            I
    )


    n'ht in qts,,n^ ^rll-^ufBlanks()       {

         String   out : tr tt -

            for(int i:0;    i < outworcl.lengthO; i++;1
                 our : our + outword. charAt (i) + rr II'
            )



            return out. trin   (   )   ;
        )



    public boolean isGuessedO {
        return (letters left- :: 0);
        l




                                                                                   5

Cs hangman

  • 1.
    CS3 Project - HANGMAN Objective: r Create a Hangman console game o Create a console application that allows the user to add/delete words to be used in the game Deadline: r Second week of January 2011 (first meeting of the week) Other details: o Lab pairings will be in effect. o I will give you the entire 1" week of January to do this but you might want to start during the brea k o There will be 2 modules so you can work independently at the start then combine the modules at the end or work on both modules together. o The emphasis will be handling inputs through console and external file a-s well as proper error handling. o For any questions, email me at: paolo.santos@pshs.edu.ph or paolo.niccolo.santos@gmail.com lnstructions: 1. Create a menu that displays: 1. Manage Word Library 2. Play Hdngman J. tlx] f, >l 2. When L is selected, the user is directed to module 1 (See next page). 3. When 2 is selected, the user is directed to module 2 (See page 3). 4. Selecting 3 ends the program.
  • 2.
    Module 1: Creatingthe Word Library (File Reading/Writing Module) L. Start by manually creating a text file containing the default list of words (each word separated by a new line). Name it wordlist.txt. The file should look like this: Merry Christmas Rudolph the red nosed reindeer 2. At the start of the module, load all the words into an array of Strings 3. Then, display the following menu: 1. Display word library 2. Add a word 3. Delete a word 4. Save current list 5. Exit 4. When f. is selected, print the entire array of words. 5. When 2 is selected, prompt the user for a word then add it to the array. 6. When 3 is selected, print the entire array of words in this manner: I. Merry 2. Christmas 3. Rudolph the red nosed reindeer Then prompt the user for a NIJMBER. Delete the word that corresponds to that number (example if the user enters 1, delete Merry from the array). 7. When 4 is selected, save the words contained in the array to wordlist.txt 8. When 5 is selected, return the user to the previous menu.
  • 3.
    Module 2: Creatingthe game (Console Inputs Module) 1. You will need the file HangmanWord.java (located at the end of this document) for this module. 2. Start with an arrav of words and use the random class to generate a random number to randomly pick a word from the list. 3. Once a word is selected, you now need to transform it into a HangmanWord a. To create a HangmanWord, simply input the String into the constructor: new HangmanWord(<String value>); b. How to use a HangmanWord Object: i. When the object is created, it automatically geneterates a String that contains the blanks of the word. Meaning if you entered new HangmanWord("hello"), it generates " ". ---- ii. To output the String of blanks, use the outputBlanksfl function. iii. You can use the use the guess(char letter)or guess(String letter)function to guess a letter that is contained in the word. lt outputs true if the letter is contained in the word, false otherwise. lt is CASE SENSITIVE. lf the letter is . contained in the word, the blank is replaced by the letter. iv. Calling the isGuessed0 function returns true if the word is successfully guessed, and false otherwise. 4. OnCe yOu've made your hangman wOrd, set hOw many "lives" your user has tO guess the word. 5. Output the blanks (example the word is snowman) 6. Prompt the user for an input (either String or char) >s 7. Use the guessfl function of the HangmanWord o.bject a. lf it returns true, output the blanks b. lf it returns false, subtract a "life" from the user then output the blanks c. lf the word is fully guessed (no more blanks)go back to 1. d. lf the user is out of lives, output the word then go back to the main menu. ' e, lf the user is not out of lives and the word still contains blanks, go back to 5.
  • 4.
    public cfass Hanqmanword{ private String inword; nriv,atc Strino outword; nri vafe i nt I etters I eft; nrrl.rlin llanam:nfaTnrd/af Vvgl4rrY rina r^rnrrl t inword : word; outword : ""; Ietters feft : 0; for(int i:0; i < word.IengthO; i++;1 1f((word.charAt(i):: ' ') I | (word.charAt(i):= '. ', I I (word.charAt(i):: ', ') | I (word.charAt(i):: 'e'1 | t (word.charAt(i)::'l') | I (word.charAt(i):: "") | l (word.charAt (i)::'" )){ outword : outv/ord + word.charAt(i) ; ) else { outword : outword ; Ietters_left++; ) ] ) public boofean giress (char letLer) { boofean hasletter : false; for(int i = 0; i < inword.lengtho; i++11 if ( (lnword. charAt (i) := letter) && (outword. charAt (i) :: '_') )i outhrord: outword.substring(0,i) + fetter + outword. substri-ng ( i+1, outword. lenqth ( ) ) ; L--T * +vr,^. IIOJ!V ^tt^, LgI L letters_left--; ] . return has{.etter; t t pub-Iic boo}ean guess (St ring letter ) { if(letter.lengthO != 1) t SysLem.out.println ("lnvalid guess. ") ; return false; ] boolean hasLetter : false; for(int i:0; i < inword.lengthO; i++11 if ( (Ietter.equals (inword.substring(i, i+1) ) ) && ("_".equals (outword. substring (i,i+t1 ; I 1 1 outword : outword. substring (0, i) + fetter + outword. substring (i+1, outword. length ( ) ) ; hasLetter : true; fetters_left--;
  • 5.
    / - return hasLetter; ) public void seLWorl (String word) { inword : word; outword : ""; letters_left : O; for(int i = 0; i < word.lengthO; i++11 if((word.charAt(i):= ' ') ll (word.charAt-(i)::'.') ll (word.charAt(i):= ', ', I I (word.charAt(i):: '?') i i (word.charAt(i):= ttt; ll (word.charAt(i):: "'t, 1l (word'charAt (i)::' " ) ) { outword: outword + word.charAt(i) ; l else 1 outword : outword + "_" ; f ette rs_le ft ++; l I ) n'ht in qts,,n^ ^rll-^ufBlanks() { String out : tr tt - for(int i:0; i < outworcl.lengthO; i++;1 our : our + outword. charAt (i) + rr II' ) return out. trin ( ) ; ) public boolean isGuessedO { return (letters left- :: 0); l 5