学习用C++开发你的第一个游戏(英文)
# include <iostream>
# include <string> // 引入string 库
using namespace std; // 引入命名空间std,之后的cout 和 endl 就不用再加上std:: 了
void PrintIntro();
void PlayGame();
string GetGuess();
bool AskToPlayAgain();
// 程序的入口
int main()
{
bool bPlayAgain = false;
do
{
PrintIntro();
PlayGame();
bPlayAgain = AskToPlayAgain();
} while (bPlayAgain);
return 0;
}
void PlayGame()
{
// 可猜测的循环次数
constexpr int NUMBER_OF_TRUNS = 5;
for (int cout = 1; cout <= NUMBER_OF_TRUNS; ++cout)
{
GetGuess();
}
}
// 介绍游戏
void PrintIntro()
{
constexpr int WORLD_LENGTH = 9; // 定义一个变量让单词的长度可变
cout << "Welocm to Bulls and Cows, a fun word game." << endl;
cout << "Can you guess the " << WORLD_LENGTH;
cout << " letter isogram I'm thinking of?\n";
cout << endl;
return;
}
// 让玩家猜一个
string GetGuess()
{
cout << "输入你猜测的结果: ";
string Guess = "";
getline(cin, Guess);
cout << "你猜测的是:" << Guess << endl;
cout << endl;
return Guess;
}
bool AskToPlayAgain()
{
cout << "你想再玩一次吗?";
string Response = "";
getline(cin, Response);
return (Response[0] == 'y')|| (Response[0] == 'Y');
}