#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;
}
猜单词
最新推荐文章于 2022-01-06 18:01:47 发布