固定版猜单词(可以自己改)
#include<iostream>
#include<algorithm>
#include<string>
#define N 50//每个单词最长的长度
using namespace std;
int LIFE=5;
int main(){
string ans="enter";
bool ok[N];
fill(ok,ok+N,0);
while(1){
cout<<"Welcome to Word Guessing Game"<<endl;
cout<<"Your secret word is: "<<endl;
int cnt=0;
for(int j=0;j<ans.size();j++)
if(ok[j]) {
cout<<" "<<ans[j];
cnt++;
}
else cout<<" _";
cout<<endl<<endl;
if(cnt==ans.size())
break;
cout<<"Guess a letter ["<<LIFE<<"]: ";
char ch;
cin>>ch;
int hit=0;
for(int j=0;j<ans.size();j++)
if(ans[j]==ch) {
ok[j]=1;
hit=1;
}
if(!hit) LIFE--;
if(LIFE==0) break;
system("cls");
}
cout<<"The answer is : "<<endl;
for(int j=0;j<ans.size();j++)
cout<<" "<<ans[j];
return 0;
}