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;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值