CS101 Lab Activity 10
Objectives
- Write a program for the Hangman game
- Use loops
- Use arrays and strings
- Use string functions
1. Requirements Specification (Problem Definition)
In this lab, you're going to design a Hangman game. The game works as
follows:
- The user selects between options : Hard, Moderate and Easy
- The computer assigns the maximum number of guesses based on
selected option
- The computer picks a word from an available word list
- The user is allowed to guess the letters in the word, one at a
time
- If a guessed letter is indeed in the word, its exact location in
the word
will be shown
- Otherwise the user loses a guess
- Only a limited number of guesses is allowed - a correct guess is
not
counted
- The user keeps guessing until:
- The complete word is shown (the user wins) OR
- The user runs out of guesses (the computer wins)
Download the file named hangdemo from the
directory ~cs101ta/lab10 into your home directory (use the unix cp
command). Open up a unix terminal and run the demo program
hangdemo (At the Unix prompt type: hangdemo). Watch how the
demo program
works. You're going to write a similar program.
2. Analysis---Refine, Generalize, Decompose the problem definition
We will give you the skeleton code in hang.c that includes the steps
that you need to go through in
your program (see step 4. Implementation below). But of course, you can
start to write the program from
scratch if
you prefer. You should consider using the following functions in your
program:
- rand() - to generate random numbers
- strlen() - to determine the length of a string
- strcpy() - to copy a string to another
- strcmp() - to compare two strings
- tolower() - to convert a character to lowercase (if it is an
uppercase letter)
3. Design---Develop Algorithm
The complete flow-chart for the program is shown below.



4. Implementation --- Write the "Program" (Code)
Download the Eclipse project
named lab10.zip (or you can download the C
file hang.c ).You have to import this
project into your Eclipse and begin adding your code into it. To do so,
go to File, Import..., and then select 'Existing Projects into
Workspace' from 'General'. Next, make sure the 'Select archive file'
radio button is selected. And, Browse for 'lab10.zip'. Click on
'Finish' button, expand the newly exported project, and locate the file
'hang.c'.
5. Run the code
Save to compile your program.
Note that whenever you save your source file, the compiler is run.
Any compile errors are shown under the "problems" tab at the bottom of
the
screen and highlighted in red. If there are no errors, your source code
is automatically compiled into an executable file.
To run a program in Eclipse you must create another 'launch
configuration' as you did in previous labs.
That is, click on the black down arrow of the run button and select the
binary
you want to run, or click run..., double click on C/C++ application,
and click
Search Project.
Once you've created the launch configuration, you can just select that
launch
configuration from the run button pull-down menu.
To see a demo of how your program should work download hangdemo
(right-click and choose "Save Link Target As" and name it hangdemo
without any extension.).
Run this program by typing
hangdemo
at the Unix prompt (Go to Applications, Utilities, Terminal to open a
Unix
prompt).
Make sure your program works properly before you proceed to the next
step.
After you've completed your program, show it to your TA and get
his/her
signature.