• 《悬挂小人游戏-版本1.1》


    /*=======dictionary.h=========*/

    #ifndef DICTIONARY_H
    #define DICTIONARY_H

    int chooseWord(char *wordChosen);
    int randomNum(int maxNum);

    #endif

    /*=======dictionary.cpp=========*/

    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h> //需要随机函数
    #include<string.h> //需要strlen这个函数计算字符串的长度
    #include"dictionary.h"

    //定义int chooseWord(char *wordChosen)函数
    int chooseWord(char *wordChosen)
    {
    FILE *dictionary = NULL; //指向dictionary的文件指针
    int wordNum = 0; //单词总数
    int chosenWordNum = 0; //选取的单词编号
    int i=0;
    int characterRead = 0; //读入的字符
    if((dictionary=fopen("dictionary.txt","r"))==NULL)
    //dictionary = fopen(‪"f:\dictionary.txt","r"); //以只读模式打开词库(dictionary)文件
    if(dictionary == NULL) //如果打开文件不成功
    {
    printf(" 无法装载词库 ");
    return 0; //返回0表示出错
    }
    //统计词库中的单词总数,也就是统计回车符 的数目
    do
    {
    characterRead = fgetc(dictionary);
    if(characterRead == ' ')
    wordNum++;
    }while(characterRead!=EOF);
    chosenWordNum = randomNum(wordNum); //随机选取一个单词(编号)
    //重新从文件开始处读取(rewind函数),直到遇到选中的那个单词
    rewind(dictionary);
    while(chosenWordNum>0)
    {
    characterRead = fgetc(dictionary);
    if(characterRead == ' ')
    chosenWordNum--;
    }

    //文件已经指向正确额位置,我们就用fgets来读取那一行(也就是那个选中的单词)
    fgets(wordChosen,100,dictionary);
    //放置字符用于表示字符串结束
    wordChosen[strlen(wordChosen)-1] = '';
    fclose(dictionary);
    return 1; //一切顺利,返回1
    }

    //定义randomNum(int maxNum)函数
    int randomNum(int maxNum)
    {
    srand(time(NULL));
    return (rand()%maxNum);
    }

    /*==========悬挂小人游戏主代码============*/
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<string.h>
    #include"dictionary.h"

    //函数声明
    int win(int letterFound[],long wordSize);
    int researchLetter(char letter,char secretWord[],int letterFound[]);
    char readCharacter();


    //定义int win(int letterFound[],long wordSize)函数
    int win(int letterFound[],long wordSize)
    {
    int i= 0;
    int win = 1; //1为胜利,0位失败
    for(i=0;i<wordSize;i++)
    {
    if(letterFound[i]==0)
    win = 0;
    }
    return win;
    }


    //定义researchLetter(char letter,char secretWord[],int letterFound[])函数
    int researchLetter(char letter,char secretWord[],int letterFound[])
    {
    int i = 0;
    int correctLetter = 0; //0表示字母不在单词中,1表示字母在单词中
    //遍历单词数组secretWord,以判断所猜字母是否在单词中
    for(i=0;secretWord[i]!='';i++)
    {
    if(letter == secretWord[i]) //如果字母在单词中
    {
    correctLetter = 1; //表示猜对了一个字母
    letterFound[i] = 1; //对于所有等于所猜字母的数组的位置,都将其数组变为1
    }
    }
    return correctLetter;
    }

    //定义char readCharacter()函数
    char readCharacter()
    {
    char character = 0;
    character = getchar(); //读取一个字母
    character = toupper(character); //把这个字母转换为大写
    //读取其他字符,直到 (为了忽略它)
    while(getchar()!=' ')
    ;
    return character; //返回读到的第一个字母
    }

    //主函数
    int main(int argc,char *argv[])
    {
    char letter = 0; //存储用户输入的字母
    char secretWord[100] = {0}; //要猜测的单词
    int *letterFound; //布尔值的数组,数组的每一个元素对应猜测单词的一个字母,
    //0表示还没猜到此字母,1表示已猜到此字母
    int leftTimes = 7; //剩余猜测次数(0=失败)
    int i = 0;
    long wordSize = 0; //单词的长度(字母数目)
    printf(" ************************************* ");
    printf(" 欢迎来到悬挂小人游戏! ");
    printf(" ************************************* ");

    //从词库(文件dictionary.txt)中随机选取一个单词
    if(!chooseWord(secretWord))
    exit(0);
    //获取单词的长度
    wordSize = strlen(secretWord);
    letterFound = (int*)malloc(wordSize*sizeof(int)); //动态分配数组大小
    if(letterFound == NULL)
    exit(0);

    //初始化布尔值数组,都置为0,表示还没有字母被猜到
    for(i=0;i<wordSize;i++)
    letterFound[i] = 0;
    //主while循环,如果还有猜测机会并且还没有胜利,继续
    while(leftTimes > 0 && !win(letterFound,wordSize))
    {
    printf(" 你好剩余 %d 此机会",leftTimes);
    printf(" 神秘单词是什么呢?");
    for(i=0;i<wordSize;i++)
    {
    if(letterFound[i])
    printf("%c",secretWord[i]); //打印出来
    else
    printf("*");
    }
    printf(" 输入一个字母:");
    letter = readCharacter();
    if(!researchLetter(letter,secretWord,letterFound))
    {
    leftTimes--;
    }
    }
    if(win(letterFound,wordSize))
    printf(" =**=胜利了=**=神秘单词是:%s",secretWord);
    else
    printf(" ====失败了====神秘单词是:%s",secretWord);
    printf(" ");
    return 0;
    }

  • 相关阅读:
    Keepass无法导入密钥文件
    数据库交互方式之一
    Grey encoder and decoder
    SystemVerilog 序列运算符与属性运算符
    避免int 与 pointer的隐式类型转换
    xxx.lib(xxx.obj)fatal error LNK1103: debugging information corrupt; recompile module 的解决方案
    c++ static 关键字用法
    影响中国软件开发的人(摘录)
    链接
    有助于事业发展和幸福感提升的四个约定
  • 原文地址:https://www.cnblogs.com/sun-/p/4900648.html
Copyright © 2020-2023  润新知