For this assignment you will extend the hangman program that you wrote last week. If you do not have a working program from last week, please contact me and I will provide you with my solution.
The first objective for this assignment is for you to practice using files by picking a random word for the hangman game from a file. For extra credit, you can use the program we wrote in class on Tuesday to create an encoded version of the file containing the words. Then your hangman program will have to decode the word after reading it in.
The second objective for this assignment is for you to learn how to use make to compile large programs that are separated into multiple files. You will convert your solution to Homework 5, which used graphics to draw the hangman scaffold and body parts, into a library to be used with your hangman game.
You may work with a partner for this assignment. Again, be sure to add a comment to the top of your programs with both partner's names. Only one partner needs to turn in the code.
Using emacs or vi, create a file called words.txt containing one hangman word per line as shown below. Once your program is working correctly, feel free to change these words and add more possibilities.
ORANGE MEOW TROUBLE
Our strategy for choosing a random word from this file will require two steps. First, we will open the file, count how many lines it contains, and close the file. Second, we will choose a random number between 1 and the number of lines, re-open the file, read lines until we reach the chosen word, and close the file.
int CountLines(char filename[]);
char *ChooseSecretWord(char filename[]); void ChooseSecretWord(char filename[], char secretWord[]);
Your function should call the CountLines function to determine how many lines the file contains. You may use either fgets or fscanf to read one line at a time from the file.
Input Body Part ----- --------- 0 Head 1 Torso 2 Left arm 3 Right arm 4 Left leg 5 Right leg 6 Left foot 7 Right foot
cp source destination
#ifndef _drawman_h #define _drawman_h // constant definitions and prototypes go here #endif
#include "drawman.h"This file should now only contain the includes and a series of function definitions.
gccx -o game drawman.c hangman.c
Edit a file called makefile and insert the following lines. We will discuss how to use this file in class.
# makefile for the hangman program game: drawman.o hangman.o gccx -o game drawman.o hangman.o hangman.o: drawman.h hangman.c gccx -c hangman.c drawman.o: drawman.h drawman.c gccx -c drawman.c clean: rm -f *~ *.o gameOnce you have created your make file, you need only type
makeand your program will be recompiled in the most efficient manner.
Make files can perform many useful actions in addition to compiling programs. For example, in the above file, I've included a clean action to remove several files, so that the next call to make will recompile everything from scratch. To execute this line, type:
make clean.
Use cs21handin to turn in the following files: