50069. Hangman

I'm a slow walker, but I never walk backwards.

Problem Description

Write a program to play the Hangman game.

The Hangman game is a word guessing game. An answer string consisting of $L$ lower case letters is initially represented by $L$ asterisks (*). The player can guess a character each time. If the character exists in the answer string, the asterisk is replaced by the correct character. For example, if the answer string is apple then it is initially represented by *****. If the player guesses l, which is in the answer string, the string becomes ***l*. After that If the player guesses p, which is also in the answer string, the string becomes *ppl*. If the guess is not in the answer string, nothing happens, but it will be counted as a wrong guess.

The game continues until the player wins by guessing the whole string successfully (the string contains no asterisks), or the player loses by making $G$ wrong guesses. If the player wins or loses, the rest of the guesses are ignored.

Note that a character may be guessed multiple times. If it is already guessed before and it is in the answer string, nothing happen. However, if it is not in the answer string, it will still be counted as another wrong guess even if it is guessed before. (同一個字母可能被猜很多次。如果這個字母是正確的且之前被猜過,不會有任何變動。但如果這個字母是錯誤的,就算已經被猜過了,每猜一次還是要再算一次 wrong guess.)

You need to write two files, main.c and hangman.c. File main.c reads in the answer string and the letters player guesses, and calls functions in hangman.c to play the game. The file hangman.c will implement the four functions in hangman.h. Note that you cannot define any new structure in main.c -- you can only use the functions defined in the hangman.h. Also note that you should define the structure in the hangman.c implementation hangman.c, and you may choose any implementation you wish.

We will test main.c and hangman.c separately. You will get 40 points if main.c is correct. You will get the other 60 points if hangman.c is correct.

The structure and functions you need to implement in hangman.c are defined in hangman.h.

hangman.h

1
2
3
4
5
6
7
8
9
10
#define MAXL 105
#define MAXGL 205
 
struct Hangman;
typedef struct Hangman Hangman;
 
Hangman* newGame(char *answer, int G);
int guess(Hangman* hangman, char ch);
void printStatus(Hangman* hangman);
int solved(Hangman* hangman);
  1. struct Hangman;

    Implement the structure and define the variables needed in this structure.

  2. Hangman* newGame(char *answer, int G);

    Returns a pointer of Hangman. $answer$ is the answer string and $G$ is the maximum number of wrong guesses before the player loses. Hint: Use malloc to create a Hangman structure and initialize the variables in Hangman here.

  3. int guess(Hangman* hangman, char ch);

    The player guesses a lower case letter $ch$.

    • Return $1$ if $ch$ is in the answer string.
    • Return $-1$ if $ch$ is not in the answer string and the number of wrong guesses is greater than or equals to $G$.
    • Return $0$ if $ch$ is not in the answer string and the number of wrong guesses is less than $G$.
  4. void printStatus(Hangman* hangman);

    Print the current status of the string. The alphabets have not been guessed in the answer string are printed as asterisks (*). For example, if the answer string is apple and the player has guesses e and p, than you should print *pp*e. Output a newline character (\n) after the string.

  5. int solved(Hangman* hangman);

    Return $1$ if the player has guessed all the letters, otherwise, return $0$.

Compile

You can compile both your main.c and hangman.c by

gcc -std=c99 -O2 hangman.c -c
gcc -std=c99 -O2 main.c -c
gcc -std=c99 -O2 main.o hangman.o

You can compile your main.c program with TA’s p10158-ta_hangman.o (download ta_hangman-linux64.o, ta_hangman-windows32.o, ta_hangman-windows64.o) file by

gcc -std=c99 -O2 main.c -c
gcc -std=c99 -O2 main.o p10158-ta_hangman.o

You can compile your hangman.c program with TA’s p10158-ta_main.o (download ta_main-linux64.o, ta_main-windows32.o, ta_main-windows64.o) file by

gcc -std=c99 -O2 hangman.c -c
gcc -std=c99 -O2 p10158-ta_main.o hangman.o

If you don't know how to write files according to the given .h file, here is an example.
如果不知道要怎麼根據 .h 檔寫另外兩個檔案,這裡有一個範例。

Input Format

The input file contains multiple test cases. The first line contains an integer $N$, indicating the number of test case.

Each test case has two lines. The first line contains a string $answer$ and an integer $G$, indicating the answer string and the maximum number of wrong guess before the player loses. The second line contains a string consisting of lower case alphabets, indicating the letters the player guesses.

Note that if the player wins by guessing the whole string or loses by making $G$ wrong guesses, the program ends and the following guesses are ignores.

  • $ 1 \leq L \leq 100$
  • $ 1 \leq \text{number of guessess} \leq 200$
  • $ 1 \leq G \leq 100 $

Output format

Print the return value of guess() and the current status after each guess. If the return value is $-1$, output “You Lose” and terminate the program. If the problem is solved, output “You Win” and terminate the program. If the program terminated without winning or losing, output “You Quit”.

Sample Input

3
apple 3
balbc
apple 3
baleeeecpdkz
apple 3
baleee

Sample Output

0 *****
1 a****
1 a**l*
0 a**l*
-1 a**l*
You Lose
0 *****
1 a****
1 a**l*
1 a**le
1 a**le
1 a**le
1 a**le
0 a**le
1 apple
You Win
0 *****
1 a****
1 a**l*
1 a**le
1 a**le
1 a**le
You Quit

Subtasks

  • Subtasks 0 tests your main.c program with the sample intput.
  • Subtasks 1 to 9 test your main.c program (total score = 40).
  • Subtasks 10 tests your hangman.c program with the sample input.
  • Subtasks 11 to 19 test your hangman.c program (total score = 60).

Discussion