关于猜数字游戏,有点bug求解决
游戏规则:模拟文曲星上的猜数游戏,先由计算机随机生成一个各位相异的4位数字,由用户来猜,每次猜测四个数字(包括了顺序),根据用户猜测的结果给出提示:xAyB其中,A前面的数字表示有几位数字不仅数字猜对了,而且位置也正确,B前面的数字表示有几位数字猜对了,但是位置不正确。
int randomNumber[4], guessNumber[4];
srand((unsigned)time(NULL));
again:for(int i = 0; i < 4; i++)
{
randomNumber[i] = rand() % 10;
echo(randomNumber,i);
}
int count = 0;
for(int i = 0; i < 4; i++)
{
cout << randomNumber[i];
}
while(count <= 20)
{
cout << "Please enter an number : ";
int InputNumber;
cin >> InputNumber;
for(int i = 0; i <= 3; i++)
{
guessNumber[i] = InputNumber % 10;
InputNumber = InputNumber / 10;
}
int A = 0, B = 0;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < i; j++)
{
if(guessNumber[j] == randomNumber[i]) B++;
}
}
for(int i = 0; i < 4; i++)
{
if(guessNumber[i] == randomNumber[i]) A++;
}
if(A == 4){
cout << "Congrations!" << endl;
break;
}
else{
count++;
cout << A << "A" << B << "B" <<endl;
}
}
char Input;
cout << "Do you want to play again?(Y/N) : ";
cin >> Input;
if(Input == 'Y') goto again;
else exit(0);
}
void echo(int *randomNumber, int n) //检测数字是否重复
{
for(int i = 0; i < n; i++)
{
if(randomNumber[i] == randomNumber[n])
{
randomNumber[n] = rand() % 10;
echo(randomNumber,n);
}
}
}
srand((unsigned)time(NULL));
again:for(int i = 0; i < 4; i++)
{
randomNumber[i] = rand() % 10;
echo(randomNumber,i);
}
int count = 0;
for(int i = 0; i < 4; i++)
{
cout << randomNumber[i];
}
while(count <= 20)
{
cout << "Please enter an number : ";
int InputNumber;
cin >> InputNumber;
for(int i = 0; i <= 3; i++)
{
guessNumber[i] = InputNumber % 10;
InputNumber = InputNumber / 10;
}
int A = 0, B = 0;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < i; j++)
{
if(guessNumber[j] == randomNumber[i]) B++;
}
}
for(int i = 0; i < 4; i++)
{
if(guessNumber[i] == randomNumber[i]) A++;
}
if(A == 4){
cout << "Congrations!" << endl;
break;
}
else{
count++;
cout << A << "A" << B << "B" <<endl;
}
}
char Input;
cout << "Do you want to play again?(Y/N) : ";
cin >> Input;
if(Input == 'Y') goto again;
else exit(0);
}
void echo(int *randomNumber, int n) //检测数字是否重复
{
for(int i = 0; i < n; i++)
{
if(randomNumber[i] == randomNumber[n])
{
randomNumber[n] = rand() % 10;
echo(randomNumber,n);
}
}
}
应该是红色部分有错误吧
具体要怎么改进呢。。真是让人摸不着头脑
[此贴子已经被作者于2017-3-12 15:58编辑过]