simple simon游戏....(已更改)
发表于 : 2016-07-06 19:55
我按照《c语言入门经典》里第四章最后那个例子写了个simple simon 的游戏,就是显示几个数字,一秒后删掉数字,然后让玩家输入,看对不对。
(已成功更改错误)
(已成功更改错误)
代码: 全选
#include<stdio.h>
#include<ctype.h>
#include<stdbool.h>
#include<stdlib.h>
#include<time.h>
int main()
{
//以下都是声明****************************
char another_game; //是否开始新游戏
const int DELAY=1; //数字显示的时间
bool correct =true; //玩家输入是否正确
time_t seed=0;
unsigned int tries; //玩家尝试的次数
unsigned int number; //存储玩家输入的数字
unsigned int digit; //它用来确定显示几位数
time_t wait_start; //存储当前的时间
//以下是游戏开始前的提示语*****************
printf("simple simon is beginning,are you ready?(click y to begin)\n");
printf("等会儿数字要一个个输入\n");
setbuf(stdin,NULL);
scanf("%c",&another_game);
if(tolower(another_game)!='y')
return 0 ;
//以下是游戏的核心部分********************
do
{
tries=0;
digit=2;
do
{
tries++;
wait_start=clock();
srand(time(&seed));
for(int i=1;i<=digit;i++)
{
setbuf(stdout,NULL);
printf("%d",rand()%10);
} //这个for显示数字
for(;clock()-wait_start<DELAY*CLOCKS_PER_SEC;)
; //持续DELAY秒
for(int i=1;i<=digit;i++)
printf("\b");
for(int i=1;i<=digit;i++)
printf(" ");
if(tries==1)
printf("Now you enter\n");
srand(seed);
for(int i=1;i<=digit;i++)
{
setbuf(stdin,NULL); //linux里要用这个清除缓冲区
scanf("%u",&number); //读取玩家输入的数字
if(number!=rand()%10)
{
correct=false;
break;
}
}
if(tries==3)
{digit++;tries=0;}
printf("%s\n",correct ? "correct!!" : "wrong!!");
}
while(correct==true);
printf("another new game?(y or N)\n");
setbuf(stdin,NULL);
scanf("%c",&another_game);
}
while(tolower(another_game)=='y');
}