已经彻底无语,做了那么多的版本没看出来hang打成hand了,对各位追猜单词系列的粉丝表示十足的歉意,对不起。只改动最后一个版本,想玩以前版本的可以查看专栏,自己修改。同时增加了一些单词并做了一些改动,版本号暂时不增加了。猜单词系列暂时停更。
#include
#include
#include
#include
#include
using namespace std;
const int NUM = 105;
const string wordlist[NUM] = { "racket","badminton","panda","deep","early","prepare","gift","chocolate","fashion","designer","keyboard","feel","medal","modern","middle","quilt","dictionary","difficult","future","mainly","autumn","butterfly","greedy","useful","dancer","businessman","view","best","interesting","view","space","travel","bridge","telephone","tree","sing","player","change","juice","soccer","tennis","morning","afternoon","night","evening","season","spring","summer","winter","fall","leave","monday","tuesday","thursday","friday","wednesday","saturday","sunday","january","february","march","april","may","june","july","augest","september","october","november","december","favourite","factory","sale","male","trousers","skirt","million","desert","noon","were","often","never","seldom","son","daughter","danger","just","hurt","pronounce","creative","transportation",""};
int main()
{
cout << "----------------Hang Man----------------\n";
srand(unsigned(time(NULL)));
char play;
cout << "Will you play a word game?";
cin >> play;
play = tolower(play);
while (play == 'y')
{
play_again:;
string target = wordlist[rand() % NUM];
if (target == "")
{
cout << "Well,you are so luck,you win,haha!" << endl;
cout << "Will you play again?";
cin >> play;
play = tolower(play);
continue;
}
size_t length = target.length();
string attempt(length, '-');
string badchars;
int guesses = 6;
cout << "Guess my secret word.It has " << length << " letters ,and you guess\none letter at a time .You get " << guesses << " wrong guesses.\n";
cout << "Your word: " << attempt << endl;
while (guesses && attempt != target)
{
char letter;
cout << "Guess a letter: ";
cin >> letter;
if(letter == '0')
{
cout << "You really want to win now?";
cin >> play;
play = tolower(play);
if(play == 'y')
{
cout << "Well,the word is " << target << ".\nYou win.";
cout << "Will you play another?";
cin >> play;
play = tolower(play);
goto play_again;
}
}
if (badchars.find(letter) != string::npos || attempt.find(letter) != string::npos)
{
cout << "You already guessed that.Try again.\n";
continue;
}
size_t loc = target.find(letter);
if (loc == string::npos)
{
cout << "Oh,bad guess!\n";
guesses--;
badchars += letter;
}
else
{
cout << "Good guess!\n";
attempt[loc] = letter;
loc = target.find(letter, static_cast, allocator>::size_type>(loc) + 1);
while (loc != string::npos)
{
attempt[loc] = letter;
loc = target.find(letter, static_cast, allocator>::size_type>(loc) + 1);
}
}
cout << "Your word: " << attempt << endl;
if (attempt != target)
{
if (badchars.length())
cout << "Bad choice: " << badchars << endl;
cout << guesses << " bad guess left\n";
}
}
if (guesses)
cout << "That's right!\n";
else
cout << "Sorry,the word is " << target << ".\n";
cout << "Will you play again?";
cin >> play;
play = tolower(play);
}
cout << "Thanks for your playing.Bye!\n";
return 0;
}