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

📄 value.txt

📁 用C++寫的猜數字遊戲
💻 TXT
字号:
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;

bool test(const int&);
void input(int&, const bool&);
void calculation(const int&, const int&, const int&, vector<int>&, const bool&);
void div(vector<int>&, const int&);
void countAB(const vector<int>&, const int&, int&, int&);
void setAB(int&, int& );
void PRINT(const vector<int>&);

int main(){
char again='y';

cout << "-----------" << endl;
cout << "AB Games!! " << endl;
cout << "---------- " << endl << endl;

while(toupper(again) == 'Y')
{

int number=0, a=0, b=0;
bool firstTime = true;
vector<int>vCombi;

while(a != 4)
{
input(number, firstTime);
setAB(a, b);
calculation(number, a, b, vCombi, firstTime);
if(firstTime == true)
firstTime = false;
}
cout << "Congratulation!! you got it!" << endl;
cout << "play again?(y/n)";
cin >> again;
}
system("pause");
return 0;
}

void setAB(int& a, int& b)
{
do{
cout << "please tell me ?A?B : ";
cin >> a >> b;

if(a<0||b<0||a+b>4)
cout << "input error!! " << a << "A" << b <<"B is impossible!!" << endl;
}while(a<0||b<0||a+b>4);
}

void div(vector<int>& v, const int& N)
{
for(int i=3; i>=0; i--)
{
int p = static_cast<int>(pow(10.0, i));
v.push_back((N/p)%10);
}
}

void input(int& n, const bool& firstTime)
{
bool again = true;

while(again)
{
if(firstTime == true)
cout << "Please guess a number(ex. 1234): ";
else
cout <<"go on, choice a number form the table: ";

cin >> n;

if(test(n)||n>9999||n<1000)
cout <<"input error!!" << endl;
else
again = false;
}
}

bool test(const int& N)
{
vector <int>v;
div(v, N);
bool t = false;

for(int i = 0; i < 3; i++)
for(int j = i + 1; j < 4; j++)
if(v[i] == v[j])
t = true;
return t;
}

void countAB(const vector<int>& V, const int& I, int& acount, int& bcount)
{
vector <int>g;
div(g, I);

for(int i=0; i<4; i++)
{
if(g[i] == V[i])
acount++;
else if(g[i]!=V[i] && (g[i]==V[(i+1)%4]||g[i]==V[(i+2)%4]||g[i]==V[(i+3)%4]))
bcount++;
}
}

void calculation(const int& N, const int& A, const int& B,
vector<int>& v2, const bool& firstTime)
{
cout << A << "A" << B << "B" << " possible numbers are:" << endl;

vector <int> v1; div(v1, N);
vector <int> v3;

for(int i=1234; i<=9876; i++)
{
if(test(i))
continue;

int Acount = 0, Bcount = 0;
countAB(v1, i, Acount, Bcount);

if(Acount == A && Bcount == B)
{
if(firstTime == true)
{
v2.push_back(i);
}
else
{
vector<int>::iterator iter = find(v2.begin(), v2.end(), i);
if(iter != v2.end())
v3.push_back(*iter);
}
}
}

if(firstTime == true)
{
PRINT(v2);
}
else
{
PRINT(v3); v2 = v3;
}
}

void PRINT(const vector<int>& v)
{
for(unsigned int i=0; i<v.size(); i++)
{
cout << v[i] << " ";
if((i+1)%10 == 0)
cout << endl;
}

cout << "count=" << v.size();

if(!v.size())
{
cout << endl << "WARNNING!! u had input wrong A or B,"
<< endl << "Game is broken now!!"
<< endl << "Please close this program & start again!!" << endl;
}

cout << endl << endl;
}

⌨️ 快捷键说明

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