我正在尝试用c ++制作一个刽子手游戏,我似乎无法跟踪已经在游戏中使用过的字母。我已经包含了下面的代码,所以你可以查看它,请告诉我如何解决这个问题。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<ctime>
using namespace std;
int main(){
string words[] =
{ "artistic",
"recreational",
"programmer",
"indistinct",
"sincere",
"pulchritude",
"legendary",
"scientific",
"oblivious",
"tomorrow"
};
string word,unknownword,wordrevelation;
char playername[100];
string usedletters;
int wordlength,numwrongofguesses,i,w;
char letter,playagain;
bool letterfound;
cout << "Please enter your name: ";
cin.getline(playername,100,'\n');
cout << endl << endl;
cout << "OKAY " << playername << " HERE WE GO!!!!!!!!!";
cout << endl << endl << endl;
srand(time(0));
w=rand()%10;
word=words[w];
wordrevelation=word;
wordlength=word.size();
for(i=0;i<wordlength;i++){
unknownword[i]=word[i];
word[i]='-';
}
cout << "OKAY HERES THE HIDDEN WORD "<< word << " AND IT IS " << wordlength << " LETTERS LONG" << endl << endl << endl;
numwrongofguesses=8;
int count=0;
int counter=0;
int x;
bool Gameover=false;
while(!Gameover){
while (1){
cout << "ENTER A LETTER " << playername << " : ";
cin >> letter;
for (x =0;x<counter;x++){
if (usedletters[x] ==letter){
cout << "Already used." << endl;
x=27;
break;
}
}if(x==27)
continue;
break;
} usedletters[counter]=letter;
counter++;
letterfound=false;
for(i=0;i<wordlength;i++){
if(unknownword[i]==letter){
letterfound=true;
word[i]=letter;
count++;
}
}
cout << endl << endl;
if(letterfound==true){
word[i]=letter;
cout << "THE LETTER " << letter << " EXISTS IN THE WORD" << endl << endl << endl;
cout << " ";
for(i=0;i<wordlength;i++){
cout << word[i];
}
cout << endl << endl;
}else{
cout << "SORRY " << playername << ", " << letter << " DOES NOT EXIST IN THIS WORD" << endl << endl << endl;
numwrongofguesses--;
}
if(numwrongofguesses==7){
cout << "------------------" << endl;
cout << "| |" << endl;
cout << endl << endl;
}else if(numwrongofguesses==6){
cout << "------------------" << endl;
cout << "| |" << endl;
cout << "| O" << endl;
cout << endl << endl;
}else if(numwrongofguesses==5){
cout << "------------------" << endl;
cout << "| |" << endl;
cout << "| O" << endl;
cout << "| X" << endl;
cout << endl << endl;
}else if(numwrongofguesses==4){
cout << "------------------" << endl;
cout << "| |" << endl;
cout << "| O" << endl;
cout << "| /X" << endl;
cout << "| /" << endl;
cout << endl << endl;
}else if(numwrongofguesses==3){
cout << "------------------" << endl;
cout << "| |" << endl;
cout << "| O" << endl;
cout << "| /X\\" << endl;
cout << "| / \\" << endl;
cout << endl << endl;
}else if(numwrongofguesses==2){
cout << "------------------" << endl;
cout << "| |" << endl;
cout << "| O" << endl;
cout << "| /X\\" << endl;
cout << "| / | \\" << endl;
cout << endl << endl;
}else if(numwrongofguesses==1){
cout << "------------------" << endl;
cout << "| |" << endl;
cout << "| O" << endl;
cout << "| /X\\" << endl;
cout << "| / | \\" << endl;
cout << "| /" << endl;
cout << "| /" << endl;
cout << endl << endl;
}else if(numwrongofguesses==0){
cout << "------------------" << endl;
cout << "| |" << endl;
cout << "| O" << endl;
cout << "| /X\\" << endl;
cout << "| / | \\" << endl;
cout << "| / \\" << endl;
cout << "| / \\" << endl;
cout << "|----------|" << endl;
cout << "|----------|" << endl;
cout << endl << endl;
cout << " " << playername << " YOU HAVE BEEN HANGED!!!!" << endl << endl << endl;
cout << " THE WORD WAS " << wordrevelation << endl << endl << endl;
Gameover=true;
}
if(numwrongofguesses==7 || numwrongofguesses==6 || numwrongofguesses==5 ||
numwrongofguesses==4 || numwrongofguesses==3 || numwrongofguesses==2 ||
numwrongofguesses==1 || numwrongofguesses==0){
cout << " ";
for(i=0;i<wordlength;i++){
cout << word[i];
}
}
cout << endl << endl;
if(count==wordlength){
cout << "CONGRATULATIONS " << playername << "! YOU GUESSED THE WORD!!! IT'S " << wordrevelation << endl << endl << endl;
Gameover=true;
}
while(Gameover){
cout << playername << " WOULD YOU LIKE TO PLAY AGAIN? : " << endl;
cout << "PRESS 'y' FOR YES OR 'n' FOR NO : ";
cin >> playagain;
cout << endl << endl << endl;
if(playagain=='y'){
numwrongofguesses=8;
count=0;
counter=0;
Gameover=false;
srand(time(0));
w=rand()%10;
word=words[w];
wordrevelation=word;
wordlength=word.size();
for(i=0;i<wordlength;i++){
unknownword[i]=word[i];
word[i]='-';
}
cout << "OKAY HERES THE HIDDEN WORD "<< word << " AND IT IS " << wordlength << " LETTERS LONG" << endl << endl << endl;
}else{
cout << "OKAY THEN,SEE YOU SOON." << endl;
}
break;
}
}
}
答案 0 :(得分:1)
要跟踪字母,请创建一个字符串并添加用户键入的字母。然后在每次输入后循环遍历它以查看输入之前是否已输入。如果是,则提示用户输入另一个输入。否则继续这个过程。
注意 - 我回答了你的问题,但我没有解决你代码中的一些错误。