■ C 言語による作成例
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
struct GAME_STATE
{
char Answer[30];
char Correct[30];
char Alphabet[30];
int Mistake;
};
void initializeGame(struct GAME_STATE* pState)
{
char* aAnswerWord[] = {
"TIGER", "CAT", "DOG", "LION", "FOX",
"WOLF", "HORSE", "COW", "GOAT", "ZEBRA",
"ELEPHANT", "PENGUIN", "GIRAFFE", "ANTEATER" };
int i;
char* pAnswer;
char ch;
pAnswer = aAnswerWord[rand() % 14];
for (i = 0; pAnswer[i]; i++){
pState->Answer[i] = pAnswer[i];
pState->Correct[i] = 0;
}
pState->Answer[i] = 0;
for (i = 0, ch = 'A'; ch <= 'Z'; i++, ch++)
pState->Alphabet[i] = ch;
pState->Alphabet[i] = 0;
pState->Mistake = 0;
}
void checkAnswer(struct GAME_STATE* pState, char ch)
{
int i;
int bMatch = 0;
for (i = 0; pState->Answer[i]; i++){
if (pState->Answer[i] == ch){
pState->Correct[i] = 1;
bMatch = 1;
}
}
for (i = 0; pState->Alphabet[i]; i++){
if (pState->Alphabet[i] == ch)
pState->Alphabet[i] = ' ';
}
if (bMatch == 0)
pState->Mistake++;
}
int checkCorrect(struct GAME_STATE* pState)
{
int i;
for (i = 0; pState->Answer[i]; i++){
if (pState->Correct[i] == 0)
return 0;
}
return 1;
}
void showHangman(int mistake)
{
printf(" +-----+\n");
printf(" | |\n");
if (mistake == 0){
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf("/|\n");
}
else if (mistake == 1){
printf(" | (+_+;\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf("/|\n");
}
else if (mistake == 2){
printf(" | (+_+;\n");
printf(" | |Y|\n");
printf(" |\n");
printf(" |\n");
printf("/|\n");
}
else if (mistake == 3){
printf(" | (+_+;\n");
printf(" | o/|Y|\n");
printf(" |\n");
printf(" |\n");
printf("/|\n");
}
else if (mistake == 4){
printf(" | (+_+;\n");
printf(" | o/|Y|\o\n");
printf(" |\n");
printf(" |\n");
printf("/|\n");
}
else if (mistake == 5){
printf(" | (+_+;\n");
printf(" | o/|Y|\o\n");
printf(" | /\n");
printf(" | ~~\n");
printf("/|\n");
}
else if (mistake == 6){
printf(" | (@_@;\n");
printf(" | o/|Y|\o\n");
printf(" | / \\n");
printf(" | ~~ ~~\n");
printf("/|\n");
}
else{
printf(" |\n");
printf(" | (^_^)\n");
printf(" | o/|Y|\o\n");
printf(" | / \\n");
printf("/| ~~ ~~\n");
}
}
void showState(struct GAME_STATE* pState)
{
int i;
showHangman(pState->Mistake);
printf("%s\n", pState->Alphabet);
for (i = 0; pState->Answer[i]; i++){
if (pState->Correct[i] == 0)
printf("_ ");
else
printf("%c ", pState->Answer[i]);
}
printf("\n");
}
char inputAnswer()
{
char ch;
do{
char in[100];
printf("? ");
scanf("%s", in);
ch = in[0];
if ('a' <= ch && ch <= 'z')
ch -= ('a' - 'A');
} while (!('A' <= ch && ch <= 'Z'));
return ch;
}
void main(void)
{
int reply = 1;
srand((unsigned int)time(0));
printf("--- HANGMAN ---\n\n");
while (reply == 1){
struct GAME_STATE gameState;
initializeGame(&gameState);
while (1){
char ch;
showState(&gameState);
ch = inputAnswer();
checkAnswer(&gameState, ch);
if (checkCorrect(&gameState)){
showHangman(-1);
printf("%s\n", gameState.Answer);
break;
}
else if (gameState.Mistake == 6){
showHangman(gameState.Mistake);
printf("%s\n", gameState.Answer);
break;
}
}
printf("AGAIN (1=YES; 0=NO!)?");
scanf("%d", &reply);
}
}