大写的ZDQ 2017-04-15 01:04 采纳率: 0%
浏览 1032
已结题

《c语言入门经典》第四章最后simon游戏程序问题

c语言入门经典第四版这本书中第四章最后的范例是个simon游戏,要求计算机屏
幕上显示一串数字显示很短时间。玩家需在数字消失前记住他们。
但是我照书上敲了代码,可游戏开始就是显示不了数字呀,是不是随机数rand()那里有问题?请大神帮忙看下下面的代码:
#include
#include
#include
#include
#include

int main()
{
char another_game = 'Y';定义不止一次游戏
bool correct = true;定义猜对序列
int counter = 0;游戏次数统计
int sequence_length = 0;序列的数字长度
int seed = 0;rand随机数种子
int number = 0;初始化输入的数字
long now = 0;计时器
int time_taken = 0;用时
int i =1;

printf("\n To play simple simon, ");
printf("watch the screen for a sequence of digits.");
printf("\n Watch carefully, as the digits are only displayed 

for second!");
printf("\n The computer will remove them, and then prompt

you");
printf("to enter the same sequence.");
printf("\n When you do, you must put spaces between the

digits.\n");
printf("\n Good 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((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("\n Now you enter the 

sequence - do not 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) / 

CLOCKS_PER_SEC;

        printf("\n\n Your score is %d", --counter * 

100 / time_taken);

        fflush(stdin);

    printf("\n Do you want to play again (y/n)? ");
    scanf("%c", &another_game);
} while(toupper(another_game) == 'Y');


return 0;

}
不胜感激。。

  • 写回答

2条回答 默认 最新

  • 坤昱 《一起学习C语言》专栏作者 2017-04-15 01:24
    关注

    seed没见你打印显示出来

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?