gpt4 book ai didi

c++ Hangman 游戏项目帮助如何在字符串中查找字母

转载 作者:行者123 更新时间:2023-11-28 04:53:44 26 4
gpt4 key购买 nike

<分区>

您好,我很久以前在编程课上开始使用 C++,我们的老师给了我们一个项目来完成。这是一个具有非常具体要求的刽子手游戏。我开始了,但我卡在了检查输入的字母是否在单词中的部分。

这是我目前所拥有的:

#include <iostream>
#include <string>
using namespace std;

int errorNb;
string Player1;
string Player2;
string UnknownWord;
string secretWord;
char letter;
int alive(1);

void displayUnknownWord(string UnknownWord)
{
for (int a(0); a<=secretWord.size(); a++)
{
if (secretWord[a] == letter)
{
UnknownWord[a+a]=letter;
}
}
cout << "The secret word is: " << UnknownWord << endl;
}

void displayHangman(int errorNb)
{
if (errorNb == 0)
{
cout << endl << endl
<<" _____ "<< endl
<<"| | "<< endl
<<"| "<< endl
<<"| "<< endl
<<"| "<< endl
<<"|____________"<< endl<< endl;
}
else if (errorNb == 1)
{
cout << endl << endl
<<" _____ "<< endl
<<"| | "<< endl
<<"| O "<< endl
<<"| "<< endl
<<"| "<< endl
<<"|____________"<< endl<< endl;
}
else if (errorNb == 2)
{
cout << endl << endl
<<" _____ "<< endl
<<"| | "<< endl
<<"| O "<< endl
<<"| | "<< endl
<<"| "<< endl
<<"|____________"<< endl<< endl;
}
else if (errorNb == 3)
{
cout << endl << endl
<<" _____ "<< endl
<<"| | "<< endl
<<"| _O "<< endl
<<"| | "<< endl
<<"| "<< endl
<<"|____________"<< endl<< endl;
}
else if (errorNb == 4)
{
cout << endl << endl
<<" _____ "<< endl
<<"| | "<< endl
<<"| _O_ "<< endl
<<"| | "<< endl
<<"| "<< endl
<<"|____________"<< endl<< endl;
}
else if (errorNb == 5)
{
cout << endl << endl
<<" _____ "<< endl
<<"| | "<< endl
<<"| _O_ "<< endl
<<"| | "<< endl
<<"| / "<< endl
<<"|____________"<< endl<< endl;
}
else if (errorNb == 5)
{
cout << endl << endl
<<" _____ "<< endl
<<"| | "<< endl
<<"| _O_ "<< endl
<<"| | "<< endl
<<"| / \\ "<< endl
<<"|____________"<< endl<< endl;
}
}

int main()
{
cout << "Welcome to HANGMAN!" << endl <<endl << "Player 1, enter your name."
<< endl;
cin >> Player1;
cout << "Player 2, enter your name." <<endl;
cin >> Player2;

cout << endl << Player1 << ". Please choose a secret word. "
"(Use lower case only)" <<endl;
cin >> secretWord;

for (int i(0); i< secretWord.size(); ++i)
{
UnknownWord += "_ ";
}

for (int s(0);s<30; ++s)
{
cout << "\n";
}

cout << "GAME START" << endl <<endl;
displayUnknownWord(UnknownWord);
displayHangman(errorNb);

while (alive = 1)
{
cout << Player2 << endl << "Please guess a letter (lower case only):" << endl;
cin >> letter;

if (secretWord.find(letter))
{
cout << "CORRECT!" << endl;
displayUnknownWord(UnknownWord);
displayHangman(errorNb);
}
else
{
cout << "WRONG!" << endl;
++errorNb;
displayUnknownWord(UnknownWord);
displayHangman(errorNb);
}
}

return 0;
}

这是老师给我们的指示: image

非常感谢您的帮助。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com