C语言如何编写Api(C语言实现Hangman)
C语言如何编写Api(C语言实现Hangman)两腿登直源码面前,了无秘密。顺带说一句,scanf()不友好。#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <time.h> #define MAX_wordLEN 32 // 单词最大长度 void print_used(const char *used int used_count) { if (used_count > 0) { printf("["); for (int i = 0; i < used_count; i ) { char c = toupper(used[i]
Hangman相当于一个猜单词游戏,初始时程序内部会随机选择一个单词,比如"HUNTER",并向用户呈现出单词长度特征。对于 "HUNTER" 就是 "------",即以6个字符 '-' 来表示。
初始状态
接下来接收用户的输入,猜对时,显示对应的字符;否则,错误计数加一。在每猜错一次后,程序会输出一幅图像ASCII图,给出一个终结的提示,当两腿登直时,你就失败了。
猜单词过程
两腿登直
源码面前,了无秘密。顺带说一句,scanf()不友好。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#define MAX_wordLEN 32 // 单词最大长度
void print_used(const char *used int used_count)
{
if (used_count > 0)
{
printf("[");
for (int i = 0; i < used_count; i )
{
char c = toupper(used[i]);
printf("'%c'" c);
if (i < used_count -1)
printf(" ");
}
printf("]\n");
}
else
{
printf("[]\n");
}
}
int main()
{
srand((unsigned int)time(0x0));
const char *hangma[] =
{
#include "hangman.txt"
};
const char *words[] = {"RUSH" "HUNTER" "HUMAN" "DELICIOUS" "PALETTE"};
int max_wrong = sizeof(hangma) / sizeof(hangma[0]) - 1;
// 随机选择一个单词
int num_of_word = sizeof(words) / sizeof(words[0]);
const char *word = words[rand() % num_of_word];
char used[MAX_WORDLEN] = {0}; // 记录已使用的字符
char so_far[MAX_WORDLEN] = {0}; // 当前已经匹配的字符
int used_count = 0; // used计数
int wrong = 0; // 错误次数
for (int len = strlen(word) i = 0; i < len; i )
so_far[i] = '-';
printf("Welcome to Hangma. Good luck!\n\n");
// 猜测循环
while (wrong < max_wrong && _stricmp(so_far word) != 0)
{
printf(hangma[wrong]);
printf("\nYou've used the following letters:\n");
print_used(used used_count);
printf("\nSo far the word is:\n%s\n" so_far);
// 读取猜测字符
char guess;
printf("\n\nEnter your guess: ");
while (scanf_s("%c%*c" &guess 1) != 1);
guess = toupper(guess);
// 如果这个字符已经使用过则继续读取直到一个未使用字符
char *pos = strchr(used guess);
while (pos)
{
printf("You've already guessed the letter %c\n" guess);
printf("\nEnter your guess: ");
while (scanf_s("%c%*c" &guess 1) != 1);
guess = toupper(guess);
pos = strchr(used guess);
}
// 记录当前字符
used[used_count ] = guess;
// 所选单词中存在当前字符
pos = strchr(word guess);
if (pos)
{
// 单词对应的位显示出来
printf("\nYes! %c is in the word!\n" guess);
for (int len = strlen(word) i = 0; i < len; i )
{
if (word[i] == guess)
so_far[i] = guess;
}
}
else
{
printf("\nSorry %c isn't in the word.\n" guess);
wrong ;
}
}
if (wrong == max_wrong)
{
printf(hangma[wrong]);
printf("\nYou've been hanged!\n");
}
else
{
printf("\nYou guessed it!");
}
printf("\nThe word was %s\n" word);
return 0;
}
注意的地方
Hangman的ASCII图像存储 hangman.txt 中,我不想让代码中字符串占据太大的篇幅,就用 #include 处理了。主函数中下列代码相当于填充数据了。
const char *hangma[] =
{
#include "hangman.txt"
};
Hangman.txt的内容
" ------ \n"
" | | \n"
" | \n"
" | \n"
" | \n"
" | \n"
" | \n"
" | \n"
" | \n"
" ---------- \n"
" ------ \n"
" | | \n"
" | O \n"
" | \n"
" | \n"
" | \n"
" | \n"
" | \n"
" | \n"
" ---------- \n"
" ------ \n"
" | | \n"
" | O \n"
" | - - \n"
" | \n"
" | \n"
" | \n"
" | \n"
" | \n"
" ---------- \n"
" ------ \n"
" | | \n"
" | O \n"
" | /- - \n"
" | \n"
" | \n"
" | \n"
" | \n"
" | \n"
" ---------- \n"
" ------ \n"
" | | \n"
" | O \n"
" | /- -/ \n"
" | \n"
" | \n"
" | \n"
" | \n"
" | \n"
" ---------- \n"
" ------ \n"
" | | \n"
" | O \n"
" | /- -/ \n"
" | | \n"
" | \n"
" | \n"
" | \n"
" | \n"
" ---------- \n"
" ------ \n"
" | | \n"
" | O \n"
" | /- -/ \n"
" | | \n"
" | | \n"
" | | \n"
" | | \n"
" | \n"
" ---------- \n"
" ------ \n"
" | | \n"
" | O \n"
" | /- -/ \n"
" | | \n"
" | | \n"
" | | | \n"
" | | | \n"
" | \n"
" ---------- \n"