1. 问题描述:这是一个记忆测试游戏。屏幕上显示很短时间的数字。玩家必须在数字消失之前记住他们,然后输入这串数字。每次过后,计算机显示更长一串数字,让玩家继续玩下去。
2.问题分析:
程序产生 0~9 的整数,屏幕上显示1秒钟,之后删除它们。然后提示玩家输入之前的整数。若输入成功,游戏继续。并且数字会随游戏变长,直到输入错误,游戏结束。
程序逻辑流程图:
源代码:
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
char another_game = 'Y';
bool correct = false;
int counter = 0;
int sequence_length = 0;
time_t seed = 0;
int number = 0;
time_t now = 0;
int time_taken = 0;
printf("\nTo play Simple Simon, "
"watch the screen for a sequence of dights.");
printf("\nWatch carefully, as the dights are only displayed"
" for a second! ");
printf("\nThe computer will remove them, and then promt 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);
int i = 0;
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("\nNow you enter the sequence - don't 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);
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');
return 0;
}