c语言入门经典第四版这本书中第四章最后的范例是个simon游戏,要求计算机屏
幕上显示一串数字显示很短时间。玩家需在数字消失前记住他们。
但是我照书上敲了代码,可游戏开始就是显示不了数字呀,是不是随机数rand()那里有问题?请大神帮忙看下下面的代码:
#include
#include
#include
#include
#include
int main()
{
char another_game = 'Y';定义不止一次游戏
bool correct = true;定义猜对序列
int counter = 0;游戏次数统计
int sequence_length = 0;序列的数字长度
int seed = 0;rand随机数种子
int number = 0;初始化输入的数字
long now = 0;计时器
int time_taken = 0;用时
int i =1;
printf("\n To play simple simon, ");
printf("watch the screen for a sequence of digits.");
printf("\n Watch carefully, as the digits are only displayed
for second!");
printf("\n The computer will remove them, and then prompt
you");
printf("to enter the same sequence.");
printf("\n When you do, you must put spaces between the
digits.\n");
printf("\n Good luck!\npress enter to play\n");
scanf("%c", &another_game);
do
{
correct = true;
counter = 0;
sequence_length = 2;
time_taken = clock();
while(correct)
{
sequence_length += counter++%3 ==0;
seed = time(NULL);
now = clock();
srand((unsigned int)seed);
for(i = 1; i <= sequence_length; i++)
printf("%d", rand() % 10);
for( ; clock() - now < CLOCKS_PER_SEC;
);
printf("\r");
for(i = 1; i <= sequence_length; i++)
printf(" ");
if(counter == 1)
printf("\n Now you enter the
sequence - do not forget the spaces\n");
else
printf("\r");
srand((unsigned int)seed);
for(i =1; i <= sequence_length; i++)
{
scanf("%d", &number);
if(number != rand() % 10)
{
correct = false;
break;
}
}
printf("%s\n", correct ? "Correct!" :
"Wrong!");
}
time_taken = (clock() - time_taken) /
CLOCKS_PER_SEC;
printf("\n\n Your score is %d", --counter *
100 / time_taken);
fflush(stdin);
printf("\n Do you want to play again (y/n)? ");
scanf("%c", &another_game);
} while(toupper(another_game) == 'Y');
return 0;
}
不胜感激。。