#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 main() { random_device rd; mt19937 gen(rd()); uniform_int_distribution<> dis(0, NUM_WORDS - 1); char playAgain = 'y'; while (tolower(playAgain) == 'y') { string word = WORDS[dis(gen)]; 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" << lettersGuessed << endl; cout << "So far, the word is:\n" << guessedSoFar << endl; char guess; cout << "\nEnter your guess: "; cin >> guess; guess = toupper(guess); if (lettersGuessed.find(guess) != string::npos) { 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"; for (int i = 0; i < word.size(); ++i) { if (word[i] == guess) { guessedSoFar[i] = 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; cout << "Do you want to play again? (y/n): "; cin >> playAgain; } return 0;}优化这段代码
时间: 2023-12-10 22:42:00 浏览: 125
#include_iostream.h_
4星 · 用户满意度95%
以下是对这段代码的一些优化建议:
1. 对于随机数生成器的使用,可以将其封装成一个函数,减少代码重复。
2. 可以将常量 MAX_WRONG 和 WORDS[] 定义为 const 类型,避免被误修改。
3. 使用 range-based for 循环来遍历 lettersGuessed 字符串。
4. 可以将判断 guess 是否已经猜过的代码抽象成一个函数。
5. 对于字符串的查找和替换操作,可以使用 STL 中提供的相应函数,如 find() 和 replace(),而不是手动实现。
6. 对于输入的字符,可以先使用 cin.get() 获取一个字符,再使用 toupper() 函数将其转换成大写字母。
7. 可以将游戏逻辑封装成一个函数,提高代码复用性。
下面是优化后的代码:
```
#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;
}
```
阅读全文