小实例 hangman game

代码

#include <bits/stdc++.h>
using namespace std;
int bk[110];
string sj(int t)
{
	string ans="";
	for(int i=0;i<t;i++)
	ans=ans+char((rand()%26)+'a');
	return ans;
}
main()
{
	srand(time(0));
	cout<<"hangman游戏总共有6次猜的机会,可以选择难度\n";
	cout<<"easy--5个字母组成的单词\nnormal--8个字母组成的单词\nhard--12个字母组成的单词\nlunatic--26个字母组成的单词\n";
	while(1)
	{
		int sum;
		cout<<"你选择的难度为\n请输入easy,normal,hard或者lunatic\n";
		f1:
		string e;
		cin>>e;
		if(e=="easy")
		sum=5;
		else if(e=="normal")
		sum=8;
		else if(e=="hard")
		sum=12;
		else if(e=="lunatic")
		sum=26;
		else 
		{
			cout<<"你输入了非法的选项请重新输入!!!\n";
			goto f1; 
		}
		string a=sj(sum);
		int num=6;
		memset(bk,0,sizeof(bk));
		//cout<<a<<endl;
		cout<<"请输入一个字母\n";
	while(1)
	{
		f2:
		string t;
		cin>>t;
		if(t.size()>1)
		{
			cout<<"输入了多个字母请重新输入\n";
			goto f2;
		}
		int f,ff;
		f=1;
		ff=0;
		for(int i=0;i<a.size();i++)
		if(a[i]==t[0])
		{
			bk[i]++;
			if(bk[i]==1)
			f=0;
			else
			ff=1;
		}
		int ac=1;
		for(int i=0;i<a.size();i++)
		if(bk[i]==0)
		{
			ac=0;
			break;
		}
		if(ac)
		{
			cout<<"--------------------------------------------\n\n";
			cout<<"恭喜你完全猜出来了!!!你还剩余<<"<<num<<">>次机会\n";
			cout<<"原单词是\n";
			for(int i=0;i<a.size();i++)
			{
				if(bk[i]!=0)
				cout<<a[i]<<" ";
				else
				cout<<"_ ";
			}
			cout<<endl;
			cout<<"--------------------------------------------\n\n";
			break;	
		}	
		if(f)
		{
			num--;
			cout<<"--------------------------------------------\n";
			if(f&&!ff)
			cout<<"你猜的字母在原单词中没有出现!!!\n";
			else if(ff)
			cout<<"你猜了一个你已经猜过的正确的字母!!!\n"; 
			if(!num)
		{
			cout<<"--------------------------------------------\n\n";
			cout<<"你已经失败了!!!hangman已经完成了!!!\n";
			cout<<"原单词是\n";
			for(int i=0;i<a.size();i++)
			cout<<a[i]<<" ";
			cout<<endl;
			cout<<"你最终猜到的是\n";
			for(int i=0;i<a.size();i++)
			{
				if(bk[i]!=0)
				cout<<a[i]<<" ";
				else
				cout<<"_ ";
			}
			cout<<"\n";
			cout<<"--------------------------------------------\n\n";
			break;
		}
			cout<<"剩余次数为<<"<<num<<">>次\n\n";
			cout<<"--------------------------------------------\n\n";
			cout<<"请输入一个字母\n";
		}
		else 
		{
			cout<<"--------------------------------------------\n";
			cout<<"恭喜你猜对了一个正确的字母,此时的单词是\n";
			for(int i=0;i<a.size();i++)
			{
				if(bk[i]!=0)
				cout<<a[i]<<" ";
				else
				cout<<"_ ";
			}
			cout<<endl<<endl;
			cout<<"--------------------------------------------\n\n";
			cout<<"请输入一个字母\n";
		}
	}
	}
}
posted @ 2018-10-14 00:35  baccano!  阅读(268)  评论(0编辑  收藏  举报