CS010 Week 2 Assignment

Hangman

Due: January 17, 2001, 10 AM

This assignment should be turned in for evaluation. To turn in the assignment, you will use the turnin command:

turnin -c 010 hangman.c

You may turn in the same file more than once. This will overwrite the earlier version. You may want to do this if you discover a mistake or make an improvement after turning in your assignment.

Write a C program to play Hangman. Your program should randomly select one of no fewer than 10 possible words to be guessed and play an interactive game of hangman. It should have at least the same features as my version, which you can run at /shared/terescoj/shared/cs010/hangman.

Be sure to comment your program well and be careful how you use your pointers. Use fixed-size character arrays to hold words only when you don't know how large the word is - all other times you should allocate the correct amount of memory using malloc(). Be sure to free all the memory that you allocate before exiting the program. Call the program hangman.c when you turn it in.

You should definitely use gdb and/or ddd to help in the debugging process. You should also start early, since this project is just large enough to be a bit tricky.

The following functions will be helpful to you, and I recommend using them:

void ignore_junk() {

  while (getchar()!='\n');
}

void randomize() {

  struct timeval tm;

  gettimeofday(&tm, 0);
  srand(tm.tv_sec);
}
The ignore_junk() function will read any extra input (such as newline characters) after you read in guesses. I recommend calling it after each scanf() or getchar() call you may use.

You can use the randomize() function to initialize the C random number generator. Call it at the beginning of your main program. After that, you can use the C library function rand() to get a random integer between 0 and the constant RAND_MAX. You can use this to get a random number in any range. Use man pages to find out what header files you'll need to include for the prototypes for the gettimeofday() function used in randomize and the srand() and rand() functions.