一个简单的猜数字游戏

一个简单的猜数字游戏,程序随机生成一个不包含重复数数字的4位数,用户输入数字进行猜测,猜测的结果,程序以?A?B的形式给出,其中,A前面的数字表示猜测的数字中位置和数字都正确的个数,B前面的数字表示数字正确但位置不正确的个数,总共8次猜测机会。代码:

 

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
using namespace std;
//猜测最大次数
const int MAX_GUESS_TIME= 8;

void ShowMenu();
void ShowGameInfo();
string GetNum();
string CalcResult(string strResult, string strInput);
bool CheckInput(string strInput);

int main()
{
	string strResult;
	string strInput;
	int iGuessTime=1;
	cout<<"==***欢迎使用猜数字游戏***=="<<endl;
	ShowMenu();
	cin>>strInput;
	while (1)
	{		
		if (strInput == "1")
		{
			break;
		}
		else if (strInput == "2")
		{
			ShowGameInfo();
		}
		else if (strInput == "0")
		{
			cout<<"谢谢您的使用,再见!\n";
			return 0;
		}
		else
		{
			cout<<"请输入正确的数字编号\n";
			
		}
		ShowMenu();
		cin>>strInput;
	}
	
	

	srand(time(NULL));

	strResult = GetNum();
	cout<<"请输入4位数数字(exit退出):"<<endl;
	cin>>strInput;
	while (strInput != "exit")
	{
		if (!CheckInput(strInput))
		{
			cout<<"您的输入不正确,请输入4位数字,不能有重复!\n";
			cin>>strInput;
			continue;
		}

		if (strResult == strInput)
		{
			cout<<"哈!猜对了,您好棒!数字是:"<<strResult<<endl;
			cout<<"继续?(y/n)";
			cin>>strInput;
			if (!(strInput[0]=='y' || strInput[0]=='Y'))
			{
				break;
			}
			strResult = GetNum();
			iGuessTime=1;
		}
		else
		{
			if (MAX_GUESS_TIME >  iGuessTime)
			{
				cout<<"第"<<iGuessTime<<" 次猜测,输入"<<strInput<<",结果:"<<CalcResult(strResult, strInput)<<endl;
				++iGuessTime;				
			}
			else
			{
				cout<<"对不起,您猜得不对,数字是:"<<strResult<<endl;
				cout<<"继续?(y/n)";
				cin>>strInput;
				if (!(strInput[0]=='y' || strInput[0]=='Y'))
				{
					break;
				}

				strResult = GetNum();
				iGuessTime=1;
			}
		}

	
		cout<<"请输入4位数数字(exit退出):"<<endl;
		cin>>strInput;
		//char c=getchar(); 
	}
	

	cout<<"谢谢您的使用,再见!\n";

	return 0;
}

string GetNum()
{
	char cTmp[2] = {0};
	string strTmp;
	int iTmp=0;	

	while (strTmp.length() != 4)
	{
		iTmp = rand() % 10;
		itoa(iTmp, cTmp, 10);
		int iIndex = strTmp.find(cTmp);
		if ( iIndex < 0)
		{
			strTmp += cTmp;
		}
	}

	return strTmp;
}


string CalcResult(string strResult, string strInput)
{
	int iA=0,iB=0;
	int i=0,j=0;
	char cResult[5]={0};
	for (;i < strResult.length();++i)
	{
		for (j=0;j < strInput.length();++j)
		{
			char strA=strResult[i];
			char strB=strInput[j];
			if (strA == strB)
			{
				if (i == j)
				{
					++iA;
				}
				else
				{
					++iB;
				}
			}

		}
	}

	sprintf(cResult,"%dA%dB", iA, iB);

	return cResult;
}

bool CheckInput(string strInput)
{
	int i=0;
	if (strInput.length() != 4)
	{
		return false;
	}

	for (;i < 4; i++)
	{
		char cTmp = strInput[i];
		if ( cTmp > '9' || cTmp < '0')
		{
			return false;
		}
	}

	return true;
}

void ShowMenu()
{
	cout<<"请输入对应数字进行选择"<<endl;
	cout<<"1-->玩游戏"<<endl;
	cout<<"2-->游戏介绍<<endl<<endl;
	cout<<"0-->退出游戏"<<endl;
}

void ShowGameInfo()
{
	cout<<"这里是游戏介绍信息\n";
	system("pause");
}


程序运行截图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值