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

用C语言实现猜单词的小游戏,第1张

用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)玩家记录
程序要求记录前三名比较好的成绩。所谓比较好的成绩是指”猜的次数/单词长度“越小越好。记录的时候要求有排名、玩家姓名、猜的次数/单词长度三项。这三条记录要求保存在硬盘上的文件中,在程序开始运行的时候就必须读入,以便随时供玩家查询、并且根据玩家的成绩进行更新。玩家退出系统的时候,最新记录也要存的硬盘中去。

效果


代码
#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 gamern");
    printf("select your optionrn");
    printf("1 开始游戏rn");
    printf("2 单词管理rn");
    printf("3 玩家记录rn");
    printf("4 退出rn");
    printf("option:rn");
    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("谜底:%srn", word_ch);
    char letter[MAX_LETTER_LEN];
    ret.len = strlen(word_en);
    ret.count = 0;
    int fl = 0;
    while (ret.count 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 %dn", 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 %sn", 
            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 %drn", 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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5698351.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存