记得去年公选haskell函数程序设计的时候老师让我们用haskell 语言实现过这个游戏,当时对于语言学得吃力,所以没有实现好,现在看到这个游戏的实现,觉得些许熟悉……
运用了string库函数,包括size(), length(), find(), constructor, operator== and so on.
// hangman 猜字游戏
// created by yun on 2016, 05, 04
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cctype>
using namespace std;
const int NUM = 26;
const string wordlist[NUM] = {
"apiary", "beetle", "cereal", "danger", "ensign",
"florid", "garage", "health", "insult", "jackal",
"keeper", "loaner", "manage", "nonce", "onset",
"plaid", "quilt", "remote", "stolid", "strain",
"useful", "valid", "whence", "xenon", "yearn",
"zippy"
};
int main() {
// 随机数
srand(time(0));
char play;
cout << "will you play a word game?<y/n>";
cin >> play;
play = tolower(play);
while (play == 'y') {
string target = wordlist[rand()%NUM];
int length = target.length();
string attempt(length, '-');
string badchars;
int guesses = 6;
cout << "Guess my secret word. It has " << length << "letters, and you" <<
" guess one letter at a time. You get " << guesses << "wrong guesses" << endl;
cout << "your word: " << attempt << endl;
while (guesses > 0&&attempt != target) {
char letter;
cout << "guess a letter:";
cin >> letter;
if (badchars.find(letter) != string::npos||attempt.find(letter) != string::npos) {
cout << "You already guess that. Try again." << endl;
continue;
}
int loc = target.find(letter);
if (loc == string::npos) {
cout << "Oh, bad guess!" << endl;
--guesses;
badchars += letter;
} else {
cout << "Good guess!" << endl;
attempt[loc] = letter;
loc = target.find(letter, loc+1);
while (loc != string::npos) {
attempt[loc] = letter;
loc = target.find(letter, loc + 1);
}
}
cout << "Your word:" << attempt << endl;
if (attempt != target) {
if (badchars.length() > 0)
cout << "Bad choices: " << badchars << endl;
cout << guesses << " guesses left" << endl;
}
}
if (guesses > 0) cout << "That is rigth" << endl;
else cout << "Sorry, the word is " << target << endl;
cout << "Will you play another?<y/n>";
cin >> play;
play = tolower(play);
}
cout << "bye" << endl;
return 0;
}
测试样例:
will you play a word game?<y/n>y
Guess my secret word. It has 6letters, and you guess one letter at a time. You get 6wrong guesses
your word: ------
guess a letter:e
Good guess!
Your word:----e-
6 guesses left
guess a letter:a
Good guess!
Your word:--a-e-
6 guesses left
guess a letter:t
Oh, bad guess!
Your word:--a-e-
Bad choices: t
5 guesses left
guess a letter:r
Good guess!
Your word:--a-er
Bad choices: t
5 guesses left
guess a letter:y
Oh, bad guess!
Your word:--a-er
Bad choices: ty
4 guesses left
guess a letter:i
Oh, bad guess!
Your word:--a-er
Bad choices: tyi
3 guesses left
guess a letter:p
Oh, bad guess!
Your word:--a-er
Bad choices: tyip
2 guesses left
guess a letter:m
Oh, bad guess!
Your word:--a-er
Bad choices: tyipm
1 guesses left
guess a letter:g
Oh, bad guess!
Your word:--a-er
Bad choices: tyipmg
0 guesses left
Sorry, the word is loaner
注:npos变量是string类的静态变量,它的值是string对象所能存储的最大字符数,由于索引从0开始,所以它比最大的索引值大1,因此可以使用它来表示没有查找到字符或者字符串
if (badchars.find(letter) != string::npos||attempt.find(letter) != string::npos)
srand(time(0)); // 先设置种子
rand(); // 产生随机数