猜单词

#include <iostream>  
#include <string>  
#include <ctime>  
#include <cstdlib>  

using namespace std;  

//用于打乱单词的函数  
string mixLetters(string word)  
{     
    string mixedWord;     
    int position(0);  

    //只要还没有抽完单词里的所有字母,就继续while循环  
    while (word.size() != 0)     
    {        
        //在单词中随机选择一个字母        
        position = rand() % word.size();    

        //将此字母添加到新的单词mixedWord中,也就是我们最后得到的打乱的单词        
        mixedWord += word[position];        

        //将此字母从字符串中删去        
        //为了不重复处理同一个字母        
        word.erase(position, 1);      
    }     

    //返回打乱后的单词     
    return mixedWord;  
}  

int main()  
{     
    string secretWord, mixedWord, userEnteredWord;   

    //初始化伪随机数的种子     
    srand(time(0));     

    //1 : 请求玩家1输入原始单词     
    cout << "输入一个单词" << endl;     
    cin >> secretWord;     

    //2 : 用函数mixLetters将玩家1输入的原始单词打乱顺序  
    mixedWord = mixLetters(secretWord);     

    //3 : 请求玩家2猜单词     
    do     
    {        
        cout << endl << "原先的单词是什么 ? " << mixedWord << endl;       

        cin >> userEnteredWord;     

        if (userEnteredWord == secretWord)        
        {           
            cout << "猜对了! 好棒!" << endl;        
        }  
        else  
        {           
            cout << "猜错了!" << endl;        
        }     
    } while (userEnteredWord != secretWord);//只要没猜对,就再进入while循环          
    return 0;  
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值