编程语言
首页 > 编程语言> > c++猜字符游戏

c++猜字符游戏

作者:互联网

因为上次那个代码在最后投票,200多人看不懂,所以这次出一个简单一点的;

猜字符(规则自己运行程序看);

主要也没什么特殊的东西,应该大部分人都能看懂;

代码:

#include<conio.h>
#include<bits/stdc++.h>
using namespace std;

void init()
{
	system("color F0");
	cout<<"_______________________\n";
	cout<<"|  查找字符小游戏     |\n";
	cout<<"|  按a查看规则,      |\n";
	cout<<"|  按b开始            |\n";
	cout<<"_______________________\n"; 
}

int main()
{
	init();
	while (1)
	{
		int ch = _getch();
		if (ch==97)
		{
			cout<<"规则:\n这是一个双人的游戏,\n首先,然后按b开始游戏,玩家A输入一\n串字符,按回车结束,游戏会自动清屏,由玩家B来猜\n(输入单个字符),\n15次中猜中8个则玩家A赢,否则玩家B赢(按c退出)";
		}
		if (ch==99)
		{
			system("cls");
			init();
		}
		if (ch==98)
		{
			system("cls");
			cout<<"游戏现在开始";
			cout<<endl<<"玩家A输入一串字符(大于15个)"<<endl;
			string strA;
			cin>>strA;
			if (strA.size()<15)
			{
				cout<<"请输入15个及以上的字符";
				while(true)
				{
					cin>>strA;
					if (strA.size()>=15)
					{
						cout<<"输入正确"<<endl;
						break;
					}
					if (strA.size()<15)
					{
						cout<<"请输入15个及以上的字符!";
					}
				}
			}
			system("cls");
			cout<<"玩家B开始猜";
			string strB;
			int strB_s = 0;
			for (int i=0;i<16;i++)
			{
				cin>>strB;
				int a = strA.find(strB);
				if (a==-1)
				{
					cout<<"查找不到,错误码:";
					cout<<string::npos<<endl; 
				}
				if (a>=0)
				{
					cout<<"猜中了!继续猜"<<endl;
					strB_s++;
				}
				if (strB_s>=8)
				{
					cout<<"player A is the winner!!!";
					break;
				}
				if (i==15&&strB_s<8)
				{
					cout<<"player B is the winner!!!";
					break;
				}
			}
		}
	}
	return 0;
}

标签:字符,游戏,cout,c++,strB,strA,include,size
来源: https://blog.csdn.net/m0_64036070/article/details/122375872