【c++】猜单词游戏

/*在d盘的game目录下,建立一个文本文档answer.txt ,文件内容为用户设定的单词答案。(文件的内容可以直接给定)。如:
University
建立c++程序,读回answer.txt文件的内容,放入一维数组a中,判断单词的长度n。然后产生一个0到n-1的随机数i。
向用户显示a[0]..a[n-1]中除a[i]之外的全部元素,而a[i]位置用一个*填补,让用户猜想*处应该是什么字母,用户输入正确则给予祝贺,错误就给予鼓励。例如:
Uni*ersity
v
congratulation
√思考题1:能否在文件中给定多个单词,每个单词占一行(或以空格隔开),程序开始后,将文件中的单词读入到能够存储多个单词的字符数组中,用户答对了,则显示下一个单词继续游戏。
√思考题2:能否将单词的显示顺序随机化,让用户每次玩儿的都是不确定的单词。
思考题3:能否建立生词本功能,用户猜过的单词可以选择加入生词本,用户可以选择查看生词本(生词本存在另一个文件中,避免游戏退出后消失的问题)。
思考题4:能否设计简单实用的附加功能,让用户用着方便,玩得开心,学习单词的效果更好。
提示:可以随时决定显示猜错的单词是什么单词,汉语意思是什么;可以进行多种游戏模式,比如顺序显示记忆,猜单词记忆,生词本记忆,做英译汉或汉译英的选择题等等。
*/
#include <iostream>
#include <fstream>
#include<ctime>
#include<string>    
using namespace std;
int main()
{
    srand((unsigned)time(0));
    ifstream in;
    int ijudge;
    int iline=0,irand1,irand2,isize;
    int i=0;
    char s[100][100], *sline,s1,s2;
    //////////////打开文件并将数据存到数组中/////////////
    in.open("e:\\answer.txt",ios::in);
    if(in)
    {
        while(!in.eof())
        {
            in>>s[iline];
            iline++;
            if(iline>=100)
                break;
        }
    }
    else
        cout<<"file not found!"<<endl;
    ///////////////////随机选单词/////////////////////////
    irand1=rand()%iline;//随机选一个单词
    isize=strlen(s[irand1]);//得到该单词长度
    irand2=rand()%isize;//随机选取某个字母位
    sline=new char[irand2];
    strcpy(sline,s[irand1]);
    for(int i=0;i<isize;i++)
    {
        if(i==irand2)
        {
            s1=s[irand1][irand2];
            s[irand1][irand2]='*';
        }
    }
    ///////////////////用户界面/////////////////////////////
    while(1)
    {

        if(i==0)
        {
            cout<<" --------------------------------------------------"<<endl;
            cout<<"------------------猜单词游戏-----------------------"<<endl;
            cout<<" --------------------------------------------------"<<endl;
            i++;
        }
        cout<<endl;;
        cout<<s[irand1]<<endl;
        cout<<endl;
        cout<<"------------------请补全“*”位置的字母----------------"<<endl;
        cout<<endl;cout<<endl;
        cin>>s2;
        if(s1==s2)
        {
                cout<<"恭喜你答对了"<<endl;
                s[irand1][irand2]=s1;
        }
        else
        {
            cout<<"回答错误"<<endl;
            s[irand1][irand2]=s1;

        }
        cout<<"是否将该单词加入到生词本。1--是   2--否"<<endl;
        cin>>ijudge;
        if(ijudge==1)
        {
            ofstream input("e:\\list.txt",ios::app|ios::out);
            input<<s[irand1]<<endl;
            input.close();
        }
        cout<<"是否继续?1--是   2--否"<<endl;
        cin>>ijudge;
        if(ijudge==2)
            break;
        else
        {
            irand1=rand()%iline;//随机选一个单词
            isize=strlen(s[irand1]);//得到该单词长度
            irand2=rand()%isize;//随机选取某个字母位
            sline=new char[irand2];
            strcpy(sline,s[irand1]);
            for(int i=0;i<isize;i++)
            {
                if(i==irand2)
                {
                    s1=s[irand1][irand2];
                    s[irand1][irand2]='*';
                }
            }
        }

    }
    in.close();
}
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的猜单词游戏代码: #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> #include <time.h> #define MAX_WORDS 10 // 最大单词数 #define WORD_LEN 20 // 单词最大长度 char *words[MAX_WORDS] = { // 单词列表 "apple", "banana", "orange", "grape", "watermelon", "pineapple", "peach", "pear", "kiwi", "lemon" }; int main() { srand(time(NULL)); // 初始化随机数种子 int n = rand() % MAX_WORDS; // 随机选择一个单词 char *word = words[n]; int len = strlen(word); char *guess = malloc(len + 1); // 动态分配存储猜测的单词 memset(guess, '_', len); // 初始化猜测单词为下划线 guess[len] = '\0'; int tries = 0; // 猜测次数 int max_tries = 10; // 最大猜测次数 printf("Welcome to the Guess Word game!\n"); printf("The word has %d letters. You have %d tries.\n", len, max_tries); while (tries < max_tries) { printf("Guess a letter: "); char c; scanf(" %c", &c); c = tolower(c); // 将输入字母转换为小写字母 int found = 0; // 是否猜对了一个字母 for (int i = 0; i < len; i++) { if (word[i] == c && guess[i] == '_') { guess[i] = c; found = 1; } } if (found) { printf("Good guess! The word is now: %s\n", guess); if (strcmp(word, guess) == 0) { // 猜对了整个单词 printf("Congratulations! You guessed the word %s in %d tries.\n", word, tries + 1); break; } } else { printf("Sorry, %c is not in the word. The word is still: %s\n", c, guess); tries++; } } if (tries == max_tries) { // 猜测失败 printf("Sorry, you failed to guess the word %s. Better luck next time!\n", word); } free(guess); // 释放动态分配的内存 return 0; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值