用C语言实现猜单词的小游戏

题目

程序设计思想:该程序将从单词库文件中随机地选择一个单词,显示该单词的中文名,用户输入对应的英文单词。系统显示猜中的字母,用户可以最多有8次猜测的机会。如果用户在规定次数中内猜中了该单词,则该次游戏用户胜利,否则用户失败。用户可以选择是否继续重复进行游戏。主要基本功能有:开始猜单词、榜单(记录每个用户历史记录,并排名次)、单词管理(对单词库中的单词进行增删改)。
程序设计题:猜单词
1.主菜单:
1.游戏开始2.单词管理3.玩家记录
4.退出
实现猜单词的游戏。游戏规则如下:
(1)玩家必须在限定次数内猜出单词的全部字母才算成功。否则失败:
(2)玩家每次只能猜一个字母:
(3)假设玩家猜的字母在单词中,单词中所有的该字母都被视为已猜出,例如:假设原单词是“Hello”,玩家猜字母”1,则程序认为玩家两个1”都猜出来了,不需要玩家猜次;
(4)不区分大小写字母。例如:假设原单词是“hello”,玩家猜字母“1*与1”,程序应当为都是字母1。
2.功能说明1)猜词过程
a)系统首先确定谜底单词,同时在屏幕上显示该单词的中文意思及单词的个数。
b)玩家输入该单词的英文进行猜测,如果输入字母不在单词中,系统提示玩家不对:如果猜对某些字母,比如玩家输入了一个“Hallo”,则在屏幕上输出h_11o”,表示还有一个字母没猜对。
c)重复b,知道玩家在规定次数内猜出了单词或者超过次数游戏失败。
d)显示玩家猜对与猜错次数等统计信息。
e)如果玩家猜出单词,计算猜的次数/单词长度,如果成绩好,将其记录,并提示玩家。
f)询问玩家是否开始新的一轮猜词,如果玩家选“否”,则系统退到外面的菜单。
8)猜词的次数在程序一开始运行的时候设定为默认值。玩的时候,可以对其进行修改。
2)单词管理
程序中用来做谜题的单词必须存放在硬盘的文件中。可以增加单词。单词增加要做到快速导入3)玩家记录
程序要求记录前三名比较好的成绩。所谓比较好的成绩是指”猜的次数/单词长度“越小越好。记录的时候要求有排名、玩家姓名、猜的次数/单词长度三项。这三条记录要求保存在硬盘上的文件中,在程序开始运行的时候就必须读入,以便随时供玩家查询、并且根据玩家的成绩进行更新。玩家退出系统的时候,最新记录也要存的硬盘中去。

效果

用C语言实现猜单词的小游戏_第1张图片
用C语言实现猜单词的小游戏_第2张图片

代码

#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 

#define FILE_NAME_RANDOM "words.txt"
#define FILE_NAME_HISTORY "history.txt"
#define CHINESE 0
#define ENGLISH 1
#define MAX_LETTER_LEN 32 
#define MAX_OPTIONS 8
typedef struct HISTORY {
    char name[MAX_LETTER_LEN];
    int count;
    int len;
}history_t;
//功能选择
int page_0() {
    system("cls");
    printf("welecome to the game\r\n");
    printf("select your option\r\n");
    printf("1 开始游戏\r\n");
    printf("2 单词管理\r\n");
    printf("3 玩家记录\r\n");
    printf("4 退出\r\n");
    printf("option:\r\n");
    int ret;
    scanf("%d", &ret);
    return ret;
}
//一次游戏 返回一个历史记录
history_t play_a_game(char word_ch[MAX_LETTER_LEN], char word_en[MAX_LETTER_LEN]) {
    history_t ret;
    printf("输入玩家姓名:");
    scanf("%s", ret.name);
    system("cls");
    printf("谜底:%s\r\n", word_ch);
    char letter[MAX_LETTER_LEN];
    ret.len = strlen(word_en);
    ret.count = 0;
    int fl = 0;
    while (ret.count <MAX_OPTIONS) {
        ret.count++;
        int i;
        scanf("%s", letter);
        for ( i= 0; i < ret.len; i++) {
            if (letter[i] != word_en[i]) {
                letter[i] = '-';
            }
        }
        letter[ret.len] = '\0';
        if (strcmp(letter, word_en) == 0)break;
        else {
            printf("%s\r\n", letter);
        }
    }
    return ret;
}
//开始游戏
//返回历史记录的长度
int page_play(char words[128][2][MAX_LETTER_LEN], int count_words,\
    history_t historys[128],int count_history) {
    int r = rand();
    r %= count_words;
    history_t h= play_a_game(words[r][CHINESE], words[r][ENGLISH]);
    for (int i = 0; i < count_history; i++) {
        if (strcmp(h.name, historys[i].name) == 0) {//用户存在
            if (h.count / h.len < historys[i].count / historys[i].len) {
                historys[i] = h;
            }
            return count_history;
        }
    }
    //用户不存在
    historys[count_history] = h;
    return count_history+1;
}
//读取history.txt文件
//返回历史信息的条数
int read_history(history_t historys[128]) {
    FILE* fp;
    int ret_count = 0;
    fp = fopen(FILE_NAME_HISTORY, "r");
    while (fscanf(fp, "%s %d %d",\
        historys[ret_count].name,&historys[ret_count].count,&historys[ret_count].len) != EOF) {
        ret_count++;
    }
    fclose(fp);
    return ret_count;
}

//读取words.txt文件
//返回单词的条数
int read_word(char words[128][2][MAX_LETTER_LEN]) {
    FILE* fp;
    int ret_count = 0;
    fp = fopen(FILE_NAME_RANDOM, "r");
    while (fscanf(fp, "%s %s", \
        words[ret_count][ENGLISH], words[ret_count][CHINESE]) != EOF) {
        ret_count++;
    }
    fclose(fp);
    return ret_count;
}
//对单词库增删改
int manage_words(char words[128][2][MAX_LETTER_LEN], int count_words) {
    //system("cls");
    printf("单词管理\r\n");
    printf("1 增\r\n");
    printf("2 删\r\n");
    printf("3 改\r\n");
    printf("4 退出\r\n");
    printf("option:\r\n");
    int ret;
    scanf("%d", &ret);
    char en[MAX_LETTER_LEN], ch[MAX_LETTER_LEN];
    switch (ret) {
        case 1: {
            printf("请输入英文和中文");
            scanf("%s %s", words[count_words][ENGLISH], words[count_words][CHINESE]);
            return count_words+1;
        }
        case 2:
            printf("请输入要删除的中文");
            scanf("%s", ch);
            for (int i = 0; i < count_words; i++) {
                if (strcmp(ch, words[i][CHINESE]) == 0) {
                    for (int j = i; j < count_words - 1; j++) {
                        strcpy(words[j][CHINESE], words[j + 1][CHINESE]);
                        strcpy(words[j][ENGLISH], words[j + 1][ENGLISH]);
                    }
                    return count_words-1;
                }
            }
            return count_words;
        case 3:
            printf("请输入英文和中文");
            scanf("%s %s", en, ch);
            for (int i = 0; i < count_words; i++) {
                if (strcmp(ch, words[i][CHINESE]) == 0) {
                    strcpy(words[i][ENGLISH], en);
                }
            }
        case 4:
            return count_words;
        default:
            break;
    }
}
//排序回调函数
int callback(const void* a, const void* b) {
    history_t* x = (history_t*)a;
    history_t* y = (history_t*)b;
    if (x->count / x->len < y->count / y->len)return 1;
    else if (x->count / x->len == y->count / y->len)return 0;
    else return 1;
}
//保存历史记录
void save_history(history_t historys[128], int count_history) {
    FILE* fp;
    fp = fopen(FILE_NAME_HISTORY, "w");
    for (int i = 0; i < count_history; i++) {
        fprintf(fp, "%s %d %d\n", historys[i].name, historys[i].count, historys[i].len);
    }
    fclose(fp);
}
//存储单词
void save_words(char words[128][2][MAX_LETTER_LEN], int count_words) {
    FILE* fp;
    fp = fopen(FILE_NAME_RANDOM, "w");
    for (int i = 0; i < count_words; i++) {
        fprintf(fp, "%s %s\n", \
            words[i][ENGLISH], words[i][CHINESE]);
    }
    fclose(fp);
}
int main()
{
    int ret, idx_history = 0;
    history_t historys[128];
    char words[128][2][MAX_LETTER_LEN];
    int count_history=read_history(historys);
    int count_words = read_word(words);
    while (1) {
        int opt_0 = page_0();
        switch (opt_0){
        case 1: {
            count_history = page_play(words, count_words,historys, count_history);
            save_history(historys, count_history);
            break;
        }
        case 2:
            count_words = manage_words(words, count_words);
            save_words(words, count_words);
            break;
        case 3:
            qsort(historys, count_history, sizeof(history_t), callback);
            for (int i = 0; i < 3 && i< count_history; i++) {
                printf("%s %d %d\r\n", historys[i].name, historys[i].count, historys[i].len);
            }
            system("pause");
            break;
        case 4:
            return 0;
        default:
            break;
        }
    }
    return 0;
}

另外需要两个txt文档,history.txt和words.txt

你可能感兴趣的:(C/C++,c语言)