/*Simon游戏:这是一个记忆力测试游戏,计算机会在屏幕上将一串数字显示很短的时间,玩家必须在数字消失前记住他们
,然后输入这串数字。每次过关后,计算机会显示更长的一串数字,让玩家继续玩家继续玩下去*/
#include <stdio.h>/*for input and output*/
#include <ctype.h>/*for toupper() function*/
#include <stdbool.h>/*for bool,true,false*/
#include <stdlib.h>/*for rand() and srand()*/
#include <time.h>/*for rime() function*/
int main(void)
{
char another_game='Y';
bool correct=true;
int counter=0;
int sequence_length=0;
time_t seed=0;
int number=0;
time_t now=0;
printf("\nTo play Simple Siomon");
printf("watch the screen for 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 to enter the same sequence.");
printf("\nWhen you do, you ,udt put spaces between the digits.\n");
printf("Good luck!\nPress Enter to play\n");
scanf("%c",&another_game);
do
{
counter=0;
sequence_length=2;
correct=true;
while(correct)
{
sequence_length+=counter++%3==0;/*On every third sucessful try , increase the sequence length*/
seed=time(NULL);/*set seed to be the number of seconds since Jan 1970*/
now=clock();
srand((unsigned int)seed);/*Initialize the random sequence*/
for(int i=1;i<=sequence_length;i++)
printf("%d",rand()%10);
for(;clock()-now<CLOCKS_PER_SEC;);/*WAIT ONE SECOND*/
/*NOW OVERWRITE THE DIGIT SEQUENCE*/
printf("\r");
for(int i=1;i<=sequence_length;i++)
printf(" ");
if(counter==1)
printf("\nNow you enter the sequence-don't forget the spaces\n");
else
printf("\r");
srand((unsigned int)seed);
for(int i=1;i<=sequence_length;i++)
{
scanf("%d",&number);
if(number!=rand()%10)
{
correct =false;
break;
}
}
printf("%s\n",correct?"Correct!":"Wrong!");
}
printf("Do you want to play again(y/n)?");
scanf("%c",&another_game);
}
while(toupper(another_game=='Y'));
return 0;
}