因为我从来没玩过,根本都不知道怎么玩...
所以无从下手...
肯请高手给代码我看看啦..
谢谢...
[CODE]#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void generate(int a[],int size){
srand((unsigned int) time(0));
for (int i = 0;i < size;++i){
int j;
do{
a[i] = rand() % 10;
for (j = i-1;j >= 0;--j){
if (a[i] == a[j])
break;
}
}
while (j >= 0);
}
}
int main(){
const int size = 4;
int random[size];
//get 4 different random numbers
generate(random,size);
int countA,countAB;
//guess the numbers and do process
do{
int temp,guess[size] ={0};
cout << "enter your guess(4 different integer) : ";
cin >> temp;
//make sure the input numbers is valid
if (cin.fail()){
cerr << "invalid input,guess again" << endl;
cin.clear();
cin.ignore();
continue;
}
if (temp > 9999 || temp < 0){
cerr << "invalid input,guess again" << endl;
continue;
}
for (int i = size-1;temp != 0;guess[i--] = temp % 10,temp /= 10);
//make sure the input numbers are different
bool is_same = 0;
for (int i = 0;i < size;++i){
for (int j = i+1;j < size;++j)
if (guess[i] == guess[j])
is_same = 1;
}
if (is_same){
cerr << "invalid input,guess again" << endl;
continue;
}
countA = 0,countAB = 0;
for (int i = 0;i < size;++i){
if (random[i] == guess[i])
++countA;
for (int j = 0;j < size;++j)
if (guess[i] == random[j])
++countAB;
}
cout << countA << "A" << countAB - countA << "B" << endl;
}
while (countA != 4);
cout << "right guess,you win" << endl;
system("pause");
}[/CODE]
以前写的,不保证没有BUG