#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#define TRUE 1
#define FALSE 0
int main()
{
char another_game = 'Y';
int correct = FALSE;
int counter = 0;
int sequence_length = 0;
int i = 0;
long seed = 0;
int number = 0;
long now = 0;
long time_taken = 0;
printf("\nTo play Simple Simon,");
printf("watch the screem for a sequence of digits.");
printf("\bWatch 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("%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((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("\nNow you enter the sequence - don't forget the spaces\n");
else
printf("\r");
srand((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("\nDo you want to play again (y/n)?");
scanf("%c",&another_game);
}while(toupper(another_game) == 'Y');
}
送妳個比較有趣的小遊戲,這個比較短,Simple Simon,給不給分無所謂
這裏面可以學到不少思想。
比如妳可以領會到用clock()函數如何進行類似俄羅斯方塊的遊戲的編程
比如怎麽“擦除”壹個東西