5
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: [email protected] or [email protected] 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.

Cs hangman

  • Upload
    iamkim

  • View
    590

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Cs   hangman

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: [email protected] or [email protected]

lnstructions:

1. Create a menu that displays:

1. Manage Word Library2. Play HdngmanJ. 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.

Page 2: Cs   hangman

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 wordseparated 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 library2. Add a word3. Delete a word4. Save current list5. 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.

Page 3: Cs   hangman

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.

Page 4: Cs   hangman

public cfass Hanqmanword {

private String inword;nriv,atc Strino outword;nri vafe i nt I etters I eft;

nrrl.rlin llanam:nfaTnrd/af rina r^rnrrl\ tV\vgl4rrY

inword : word;outword : "";Ietters feft : 0;

for(int i:0; i < word.IengthO; i++;11f((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) :: '_') ) iouthrord: outword.substring(0,i) + fetter +

outword. substri-ng ( i+1, outword. lenqth ( ) ) ;

L--T ^tt^, * +vr,^.IIOJ!V L LgI

letters_left--;

]

. return has{.etter;tt

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--;

Page 5: Cs   hangman

/

return hasLetter;)

public void seLWorl (String word) {

inword : word;outword : "";

letters_left : O;

for(int i = 0; i < word.lengthO; i++11if((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 ++;

lI

)

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

String out : tr tt -

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

)

return out. trin ( ) ;)

public boolean isGuessedO {

return (letters left- :: 0);l

-

5