⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 guessnumgame.cpp

📁 猜数字游戏和一个人工智能猜数字的游戏VC++
💻 CPP
字号:
/*
* 文件名称: GuessNumGame.cpp
* 程序描述:
*	常见的小游戏【猜数字】的模拟:
*	猜数字游戏:	即有四位十进制数字,一般可猜8次
*				每次返回aAbB(A表示数字正确并且位置正确,B表示数字正确但位置不正确)
*				如:假设要猜的数字是1234,如果游戏者猜0134即返回2A1B(3、4为A,1为B)
*
*	还有一个程序使用每次都进行计算的方法进行智能解【猜数字】,参见GuessNum.cpp
*	另外有一个程序使用决策树的方法进行智能解【猜数字】,参见GuessNumAll.cpp
* 编制日期: 2003-05-28
* 程序作者: realfun
* 联系方式: rzfemail@etang.com
*/
#pragma warning(disable:4786)

#include <iostream>
#include <string>
#include <vector>
#include <conio.h>
#include <ctime>
#include <cassert>

using namespace std;

string GetDigits(const int count);
int GetABValue(string strCode, string strGuess);
string ToAB(int ab);

int main(int argc, char *argv[])
{
	vector<string> vStr;
	string strCode;	//要猜的数字串

	srand((unsigned)time(NULL));	//随机数种子

	cout << "猜数字游戏介绍:" << endl;
	cout << "	有四位十进制数字,可猜8次" << endl;
	cout << "	每次返回aAbB(A表示数字正确并且位置正确,B表示数字正确但位置不正确)" << endl;
	cout << "	如:假设要猜的数字是1234,如果游戏者猜0134即返回2A1B(3、4为A,1为B)" << endl;
	cout << endl;
	cout << "    还有一个程序使用每次都进行计算的方法进行智能解【猜数字】,参见GuessNum.cpp" << endl;
	cout << "    另外有一个程序使用决策树的方法进行智能解【猜数字】,参见GuessNumAll.cpp" << endl;
	cout << "★★★★★★★★★★★★★★★★★★★★★★★★★★★" << endl;
	cout << "猜数字游戏开始..." << endl;
	cout << "	1.  系统随机产生要猜的数字[默认]" << endl;
	cout << "	2.  由另一位游戏者设定数字" << endl;
	if (getch() != '2')
	{
		bool valid = false;
		unsigned iCode;
		while (!valid)
		{
			iCode = rand();
			while (iCode > 9999) iCode /= 10;
			char a[20];
			sprintf(a, "%04u", iCode);
			valid = true;
			for (int i=0; i<4; i++)
				for (int j=i+1; j<4; j++)
					if (a[i] == a[j]) valid = false;
			if (valid) strCode = a;
		}
	}else
	{
		cout << "输入要别人猜测的数字..." << endl;
		strCode = GetDigits(4);
	}
	int i;
	for (i=0; i<24; i++) cout << endl;	//清屏
	for (i=0; i<8; i++)
	{
		cout << "第 " << i + 1 << " 次:";
		string strGuess = GetDigits(4);
		int iOut = GetABValue(strCode, strGuess);
		string strOut;
		strOut += "第 ";
		strOut += char(i + 1 + '0');
		strOut += " 次: ";
		strOut += strGuess;
		strOut += " -> ";
		strOut += ToAB(iOut);
		cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
		vStr.push_back(strOut);
		for (int j=0; j<vStr.size(); j++)
			cout << vStr[j] << endl;
		if (iOut == 40)
		{
			cout << "恭喜你,你现在学会猜数字了!!!" << endl;
			break;
		}
	}
	if (i == 8)
	{
		cout << "告诉你吧,谜底是。。。" << strCode << endl;
		cout << "8次都猜不对,唉。。。。。。。。。。。。" << endl;
	}
	return 0;
}///:~

//由cin获取count位并且各位不同数字,进行合法性判断
string GetDigits(const int count)
{
	string strResult;
	bool bLegal = false;
	while (!bLegal)
	{
		cout << "请输入" << count << "位不重复的十进制数字 :" << endl;
		cin >> strResult;
		if (strResult.length() == count)
		{
			int i;
			bLegal = true;
			for (i=0; i<count; i++)
			{
				for (int j=i+1; j<count; j++)	//是否重复?
					if (strResult[i] == strResult[j]) bLegal = false;
				if (!isdigit(strResult[i]))		//是否数字?
					bLegal = false;
			}//#for(j)
			if (!bLegal)
				cout << "输入不合法!" << endl;
		}else
		{
			cout << "输入不合法!" << endl;
		}
	}//#while
	return strResult;
}//#Get4Digits()

int GetABValue(string strCode, string strGuess)
{
	assert(strCode.length() == 4 && strGuess.length() == 4);
	int a = 0, b = 0;
	for (int i=0; i<4; i++)
	{
		for (int j=0; j<4; j++)
		{
			if (strCode[i] == strGuess[j])
			{
				if (i==j)
					a++;
				else
					b++;
			}
		}//#for(j)
	}//#for(i)
	return a * 10 + b;
}//#GetABValue()

string ToAB(int ab)
{
	char chA = (ab/10) + '0';
	char chB = (ab%10) + '0';
	string str;
	str += chA;
	str += "A";
	str += chB;
	str += "B";
	return str;
}//#ToAB()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -