代码: 全选
#include<stdio.h>
#include<ctype.h>
#include<stdbool.h>
#include<stdlib.h>
#include<time.h>
int main (void)
{
char another_game='Y';
int i;
int number;
int correct=false;
int counter=0;
int sequence_length=0;
time_t seed=0;
int now=0;
int time_taken;
printf("\n To play Simple Simon,");
printf("watch the screen for a sequence of digits.");
printf("\nWatch carefully,as the digits are only displayed for a second!");
printf("\nThe computer will remove them,and then prompt you");
printf("to enter the same sequence.");
printf("\nWhen you do, you must put spaces between the digits.\n");
printf("\nGood luck!\nPress Enter to play\n");
//scanf("%d",&another_game);
do {
correct=true;
counter=0;
sequence_length=2;
time_taken=clock();
printf("%d\n",time_taken);
while(correct)
{
sequence_length+=(counter++)%3==0;
seed=time(NULL);
now=clock();
printf("%d\n",now);
srand((unsigned int)seed);
for(i=1;i<=sequence_length;i++)
printf("%d ",rand()%10);
while(clock()-now<CLOCKS_PER_SEC*2);
printf("\r");
for(i=1;i<=sequence_length;i++)
printf(" ");
if(counter==1)
printf("\nNow you enter the sequence-don`t forget"
"the spaces\n");
else
printf("\n");
srand((unsigned int)seed);
for(i=1;i<=sequence_length;i++)
{
fflush(stdin);
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%d\n",time_taken);
printf("\n\nYour score is %d",(--counter)*100/time_taken);
fflush(stdin);
printf("\nDo you want to play again (y/n)\n");
scanf("%c",&another_game);
}while(toupper(another_game)=='Y');
return 0;
}
1、在程序输入三个随机数字的时候,应该是先停顿两秒,然后在运行“回车”和输入“空格“,但运行的时候却是先停顿两秒,然后再输入三个随机数,随后又很快输入回车和空格,这样就看不到三个随机数的输出。
2、fflush()函数,不知为何好像没有发挥作用。