#include <iostream>#include <random>#include <string>#include <algorithm>#include <cctype>using namespace std;const int MAX_WRONG = 6;const string WORDS[] = {"HELLO", "WORLD", "COMPUTER", "PROGRAMMING", "LANGUAGE"};const int NUM_WORDS = sizeof(WORDS) / sizeof(string);// 生成随机数int getRandomNumber(int min, int max) { random_device rd; mt19937 gen(rd()); uniform_int_distribution<> dis(min, max); return dis(gen);}// 判断字符是否已经猜过bool isGuessAlreadyMade(char guess, const string& lettersGuessed) { return (lettersGuessed.find(guess) != string::npos);}// 猜词游戏逻辑void playGame() { string word = WORDS[getRandomNumber(0, NUM_WORDS - 1)]; int wrongGuesses = 0; string guessedSoFar(word.size(), '_'); string lettersGuessed; cout << "Welcome to Hangman. Good luck!\n\n"; while (wrongGuesses < MAX_WRONG && guessedSoFar != word) { cout << "You have " << MAX_WRONG - wrongGuesses << " incorrect guesses left.\n"; cout << "You've used the following letters:\n"; for (char letter : lettersGuessed) { cout << letter << " "; } cout << endl; cout << "So far, the word is:\n" << guessedSoFar << endl; char guess; cout << "\nEnter your guess: "; cin.get(guess); guess = toupper(guess); if (isGuessAlreadyMade(guess, lettersGuessed)) { cout << "You've already guessed " << guess << endl; continue; } lettersGuessed += guess; if (word.find(guess) != string::npos) { cout << "That's right! " << guess << " is in the word.\n"; replace(guessedSoFar.begin(), guessedSoFar.end(), '_', guess); } else { cout << "Sorry, " << guess << " isn't in the word.\n"; ++wrongGuesses; } } if (wrongGuesses == MAX_WRONG) { cout << "\nYou've been hanged!"; } else { cout << "\nYou guessed it!"; } cout << "\nThe word was " << word << endl;}int main() { char playAgain = 'y'; while (tolower(playAgain) == 'y') { playGame(); cout << "Do you want to play again? (y/n): "; cin >> playAgain; } return 0;}改良这段代码
时间: 2023-12-10 12:42:02 浏览: 105
这段代码已经比较简洁明了,但还有一些可以改良的地方:
1. 可以在输入时避免出现多余的回车符:
```
cout << "\nEnter your guess: ";
cin >> guess;
guess = toupper(guess);
```
2. 可以在猜中字母后直接输出猜测过的字母,而不需要再次遍历字符串:
```
if (word.find(guess) != string::npos) {
cout << "That's right! " << guess << " is in the word.\n";
replace(guessedSoFar.begin(), guessedSoFar.end(), '_', guess);
cout << "So far, the word is:\n" << guessedSoFar << endl;
} else {
cout << "Sorry, " << guess << " isn't in the word.\n";
++wrongGuesses;
cout << "So far, the word is:\n" << guessedSoFar << endl;
}
```
3. 可以将一些常量定义为全局变量,方便修改和维护:
```
const int MAX_WRONG = 6;
const string WORDS[] = {"HELLO", "WORLD", "COMPUTER", "PROGRAMMING", "LANGUAGE"};
const int NUM_WORDS = sizeof(WORDS) / sizeof(string);
```
改良后的代码如下:
```
#include <iostream>
#include <random>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;
const int MAX_WRONG = 6;
const string WORDS[] = {"HELLO", "WORLD", "COMPUTER", "PROGRAMMING", "LANGUAGE"};
const int NUM_WORDS = sizeof(WORDS) / sizeof(string);
int getRandomNumber(int min, int max) {
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(min, max);
return dis(gen);
}
bool isGuessAlreadyMade(char guess, const string& lettersGuessed) {
return (lettersGuessed.find(guess) != string::npos);
}
void playGame() {
string word = WORDS[getRandomNumber(0, NUM_WORDS - 1)];
int wrongGuesses = 0;
string guessedSoFar(word.size(), '_');
string lettersGuessed;
cout << "Welcome to Hangman. Good luck!\n\n";
while (wrongGuesses < MAX_WRONG && guessedSoFar != word) {
cout << "You have " << MAX_WRONG - wrongGuesses << " incorrect guesses left.\n";
cout << "You've used the following letters:\n";
for (char letter : lettersGuessed) {
cout << letter << " ";
}
cout << endl;
cout << "So far, the word is:\n" << guessedSoFar << endl;
char guess;
cout << "\nEnter your guess: ";
cin >> guess;
guess = toupper(guess);
if (isGuessAlreadyMade(guess, lettersGuessed)) {
cout << "You've already guessed " << guess << endl;
continue;
}
lettersGuessed += guess;
if (word.find(guess) != string::npos) {
cout << "That's right! " << guess << " is in the word.\n";
replace(guessedSoFar.begin(), guessedSoFar.end(), '_', guess);
cout << "So far, the word is:\n" << guessedSoFar << endl;
} else {
cout << "Sorry, " << guess << " isn't in the word.\n";
++wrongGuesses;
cout << "So far, the word is:\n" << guessedSoFar << endl;
}
}
if (wrongGuesses == MAX_WRONG) {
cout << "\nYou've been hanged!";
} else {
cout << "\nYou guessed it!";
}
cout << "\nThe word was " << word << endl;
}
int main() {
char playAgain = 'y';
while (tolower(playAgain) == 'y') {
playGame();
cout << "Do you want to play again? (y/n): ";
cin >> playAgain;
}
return 0;
}
```
阅读全文