![]() |
#2
rjsp2020-04-02 10:50
瞎写的,没检查,经供参考
![]() #include <iostream> #include <random> #include <array> #include <tuple> class foo { public: foo() noexcept { _rand_gen.seed( std::random_device()() ); } std::array<unsigned,4> generate_4digits_unique() const noexcept { unsigned buf[] = { 0,1,2,3,4,5,6,7,8,9 }; std::uniform_int_distribution<> gen; using param_type = std::uniform_int_distribution<>::param_type; std::swap( buf[0], buf[gen(_rand_gen,param_type{1,9})] ); std::swap( buf[1], buf[gen(_rand_gen,param_type{1,9})] ); std::swap( buf[2], buf[gen(_rand_gen,param_type{2,9})] ); std::swap( buf[3], buf[gen(_rand_gen,param_type{3,9})] ); return { buf[0], buf[1], buf[2], buf[3] }; } std::array<unsigned,4> input_4digits_unique() const noexcept { auto is_unique = [](unsigned n) { bool buf[10] = {}; for( ; n!=0; n/=10 ) { if( buf[n%10] ) return false; buf[n%10] = true; } return true; }; unsigned n; std::cout << "please input a 4 digits number:" << std::endl; for( ; !(std::cin>>n) || n<1000 || n>9999 || !is_unique(n); ) { std::cin.clear(); std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ); std::cout << "your input is wrong!! please input again :" << std::endl; } return { n/1000, n/100%10, n/10%10, n%10 }; } std::tuple<unsigned,unsigned> compare( const std::array<unsigned,4>& a, const std::array<unsigned,4>& b ) const noexcept { bool buf[10] = {}; unsigned x=0, y=0; for( size_t i=0; i!=size(a); ++i ) { buf[a[i]] = true; x += a[i]==b[i]; } for( size_t i=0; i!=size(b); ++i ) y += buf[b[i]]; return { x, y-x }; } bool game( size_t count=8 ) const noexcept { std::array<unsigned,4> key = generate_4digits_unique(); std::cout << key[0] << key[1] << key[2] << key[3] << std::endl; for( size_t i=count; i!=0; --i ) { std::array<unsigned,4> guess = input_4digits_unique(); std::tuple<unsigned,unsigned> result = compare( key, guess ); if( std::get<0>(result) == 4 ) { std::cout << "Bingo" << std::endl; return true; } std::cout << std::get<0>(result) << 'A' << std::get<1>(result) << 'B' << std::endl; std::cout << "you have " << (i-1) << " chance" << std::endl; } std::cout << "超过规定次数啦,重新来过吧" << std::endl; return false; } private: mutable std::mt19937 _rand_gen; }; int main( void ) { using namespace std; for( foo a; ; ) { a.game(8); cout << "是否继续(Y/N)? "; char ch; if( !(cin>>ch) || (ch!='Y' && ch!='y') ) break; } } |
先由计算机随机生成一个各位相异的4位数字,由用户来猜,每次猜测四个数字(包括了顺序),根据用户猜测的结果给出提示:xAyB
其中,A前面的数字表示有几位数字不仅数字猜对了,而且位置也正确,B前面的数字表示有几位数字猜对了,但是位置不正确。
我的代码
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
void Initia(int *a);
void Input(int *b);
int nCompare(int a[],int b[]);
void Output();
int main ()
{
Output();
return 0;
}
void Initia(int *a)//随机产生四位四位不同的数
{
int nrand=0;
int m,n,i,j;//个,十,百,千位
srand( (unsigned)time( NULL ) );
nrand=rand();
//cout<<nrand<<endl;
m = nrand%10;
nrand = nrand/10;
n = nrand%10;
nrand = nrand/10;
while (m==n)
{
m = rand()%10;
}
i = nrand%10;
nrand = nrand/10;
while ((m==i)||(n==i))
{
i = rand()%10;
}
j = nrand%10;
while ((j==m)||(j==n)||(j==i))
{
j = rand()%10;
}
a[0] = m;
a[1] = n;
a[2] = i;
a[3] = j;
}
void Input(int *b)//用户输入的四个数字字符
{
int number=0;
int mm,nn,ii,jj;//输入的数分成个、十、百、千位
cout<<"please input a 4 bit number:"<<endl;
for(;;)//要求用户只能输入四位不同的四位数
{
cin>>number;
mm = number%10;
number = number/10;
nn = number%10;
number = number/10;
ii = number%10;
number = number/10;
jj = number%10;
if((number>9999)||(mm==nn)||(mm==ii)||(mm==jj)||(nn==ii)||(nn==jj)||(ii==jj))
{
cout<<"your input error!! please input again :"<<endl;
cin.clear();
cin.ignore(1000,'/n');
}
else
{
break;
}
}
//把个十百千位存在数组里
b[0] = mm;
b[1] = nn;
b[2] = ii;
b[3] = jj;
}
int nCompare(int a[],int b[])//比较 。(数组作参数)
{
int count1 = 0,count2 = 0;//count1数相同的个数,count2数和位置都相同的个数
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(a[i] == b[j])
{
count1++;
if(i==j)
count2++;
}
}
}
cout<<count2<<"A"<<(count1-count2)<<"B"<<endl;
return count2;//返回位置和数都对的个数
}
void Output()//输出结果
{
int a[4]={0};//存随机产生的四位不同的数
int b[4]={0};//存用户输入的四位数
Initia(a);
for(int k=3;k>=0;k--)
{
cout<<a[k];
}
cout<<endl;
for(int i=8;i>=0;i--)
{
Input(b);
int r=nCompare(a,b);
if(4==r)
{
cout<<"你真聪明 = "<<b[3]<<b[2]<<b[1]<<b[0]<<endl;
break;
}
else
{
cout<<"you have "<<i<<" chance"<<endl;
if(0==i)
cout<<"超过规定次数啦,重新来过吧"<<endl;
}
}
}
请问大佬
如何在每次猜对或超过规定次数后,添加一个是否继续游戏(Y/N)