一个单词猜词程序

// Word Jumble
// The classic word jumble game where the player can ask for a hint


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


using namespace std;


int main()
{
enum fields {WORD, HINT, NUM_FIELDS};  //借枚举初始化012
const int NUM_WORDS = 5;
const string WORDS[NUM_WORDS][NUM_FIELDS] = //二维数组
{
{"wall", "Do you feel you're banging your head against something?"},
{"glasses", "These might help you see the answer."},
{"labored", "Going slowly, is it?"},
{"persistent", "Keep at it."},
{"jumble", "It's what the game is all about."}
};


srand(static_cast<unsigned int>(time(0)));
int choice = (rand() % NUM_WORDS);
//int choice = 3;
string theWord = WORDS[choice][WORD];  // word to guess
string theHint = WORDS[choice][HINT];  // hint for word


string jumble = theWord;  // jumbled version of word
int length = jumble.size();
for (int i=0; i<length; ++i)
{
int index1 = (rand() % length);
int index2 = (rand() % length);
char temp = jumble[index1];
jumble[index1] = jumble[index2];
jumble[index2] = temp;
}


cout << "\t\t\tWelcome to Word Jumble!\n\n";
cout << "Unscramble the letters to make a word.\n";
cout << "Enter 'hint' for a hint.\n";
cout << "Enter 'quit' to quit the game.\n\n";
cout << "The jumble is: " << jumble;


string guess;
cout << "\n\nYour guess: ";
cin >> guess;


while ((guess != theWord) && (guess != "quit"))
{
if (guess == "hint")
{
cout << theHint;
}
else
{
cout << "Sorry, that's not it.";
}


cout <<"\n\nYour guess: ";
cin >> guess;
}


if (guess == theWord)
{
cout << "\nThat's it!  You guessed it!\n";
}


cout << "\nThanks for playing.\n";


return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值