?? guessnumgame.cpp
字號:
/*
* 文件名稱: GuessNumGame.cpp
* 程序描述:
* 常見的小游戲【猜數(shù)字】的模擬:
* 猜數(shù)字游戲: 即有四位十進制數(shù)字,一般可猜8次
* 每次返回aAbB(A表示數(shù)字正確并且位置正確,B表示數(shù)字正確但位置不正確)
* 如:假設(shè)要猜的數(shù)字是1234,如果游戲者猜0134即返回2A1B(3、4為A,1為B)
*
* 還有一個程序使用每次都進行計算的方法進行智能解【猜數(shù)字】,參見GuessNum.cpp
* 另外有一個程序使用決策樹的方法進行智能解【猜數(shù)字】,參見GuessNumAll.cpp
* 編制日期: 2003-05-28
* 程序作者: realfun
* 聯(lián)系方式: 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; //要猜的數(shù)字串
srand((unsigned)time(NULL)); //隨機數(shù)種子
cout << "猜數(shù)字游戲介紹:" << endl;
cout << " 有四位十進制數(shù)字,可猜8次" << endl;
cout << " 每次返回aAbB(A表示數(shù)字正確并且位置正確,B表示數(shù)字正確但位置不正確)" << endl;
cout << " 如:假設(shè)要猜的數(shù)字是1234,如果游戲者猜0134即返回2A1B(3、4為A,1為B)" << endl;
cout << endl;
cout << " 還有一個程序使用每次都進行計算的方法進行智能解【猜數(shù)字】,參見GuessNum.cpp" << endl;
cout << " 另外有一個程序使用決策樹的方法進行智能解【猜數(shù)字】,參見GuessNumAll.cpp" << endl;
cout << "★★★★★★★★★★★★★★★★★★★★★★★★★★★" << endl;
cout << "猜數(shù)字游戲開始..." << endl;
cout << " 1. 系統(tǒng)隨機產(chǎn)生要猜的數(shù)字[默認]" << endl;
cout << " 2. 由另一位游戲者設(shè)定數(shù)字" << 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 << "輸入要別人猜測的數(shù)字..." << 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 << "恭喜你,你現(xiàn)在學(xué)會猜數(shù)字了!!!" << endl;
break;
}
}
if (i == 8)
{
cout << "告訴你吧,謎底是。。。" << strCode << endl;
cout << "8次都猜不對,唉。。。。。。。。。。。。" << endl;
}
return 0;
}///:~
//由cin獲取count位并且各位不同數(shù)字,進行合法性判斷
string GetDigits(const int count)
{
string strResult;
bool bLegal = false;
while (!bLegal)
{
cout << "請輸入" << count << "位不重復(fù)的十進制數(shù)字 :" << 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++) //是否重復(fù)?
if (strResult[i] == strResult[j]) bLegal = false;
if (!isdigit(strResult[i])) //是否數(shù)字?
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()
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -