| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1211 人关注过本帖
标题:记忆测试小游戏,无法按逻辑正常运行,找不到原因
只看楼主 加入收藏
TXysilence
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2017-9-7
结帖率:50%
收藏
 问题点数:0 回复次数:2 
记忆测试小游戏,无法按逻辑正常运行,找不到原因
可以帮我看下吗?谢谢啦
//Program Simple Simon
#include<stdio.h>
#include<ctype.h>
#define bool int
#define true 1
#define false 0
#include<stdlib.h>
#include<time.h>

int main(void)
{
    char another_game = 'Y';   //Records if another game is to be played
    const unsigned int DELAY = 1; //Display period in seconds
    bool correct = true;
    unsigned int tries = 0;    //Number of successful enteries for sequence length
    unsigned int digits = 0;   //Number of digits in a sequence
    time_t seed = 0; //Seed value for random number sequence
    unsigned int number =0;     //Stores an input digit
    time_t wait_start = 0 ;   //Store current time
    clock_t start_time = 0;   //Game start time in clock ticks
    unsigned int score = 0 ;      //Game score
    unsigned int total_digits = 0;  //Total of digits entered in a game
    unsigned int game_time =0;   //Game time in seconds
    unsigned int i ;

    //Describe how the game is played
    printf("\nTo play Simple Simon,");
    printf(" watch the screen for a sequence of digits.");
    printf("\nWatch carefully , as the digits are only displayed"
          " for %u secomnd%s",DELAY,DELAY>1 ? "s!" : "!");
    printf("The 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_s("%c", &another_game);

    //Game loop-one outer loop iteration is a complete game

    do
    {
        //Initialize game
        correct = true ;   //Indicates correct sequence entered
        tries=0;   //Initialize count of successful tries
        digits = 2;        //Initial length of digit sequence
        start_time = clock();   //Record time at start of game
        
        //Inner loop continue as long as sequences are entered correctly
        while(correct)
        {
            ++tries;    //A new attempt
            wait_start = clock();     //record start time for sequence
            
            //Generate a sequence of digits and display them
            srand((unsigned)time(&seed));  //Initialize the radom sequence
            for(i=1;i <= digits;++i)
                printf(" %u ",rand() % 10);  //Output a random digit
                for (; clock()-wait_start < DELAY*CLOCKS_PER_SEC;);  //Wait DELAY seconds
            
            //Now overwrite the digit sequence
            printf("\r");    //Go to the begining of the line
            for ( i = 1;i <= digits; ++i)
                printf("  ");    //Output two spaces

            if (tries == 1)   //only output message for 1st try
                printf("\nNow you enter the sequence - don't forget"
                 " the spaces\n");
            else
                printf("\r");       //Back to the bigining of the line
            
            
            srand((unsigned)seed); //Reinitialize the random sequence
            srand((unsigned)time(&seed));   ///Intialize the random sequence
        
            for (i = 1;i<=digits;++i)
                //Read the input sequence & check against the original
            {
                scanf_s(" %u",&number);    //Read a digit
                if (number != rand() % 10)   //Compare with generated digit
                {
                    correct = false;
                    break;
                }
            }
            //On every third successful try , increase the sequence length
            if(correct && ((tries % 3)==0))
                ++digits;
            printf("%s\n",correct,correct ? "Correct!" : "Wrong!");
        }
        //Calculate and output the game score
        score = 10 * (digits - ((tries % 3 ) == 1));  //Points for sequence length
        total_digits = digits* (((tries %3)==0) ? 3 : tries % 3);
        if (digits > 2)
            total_digits += 3*((digits - 1)*(digits - 2)/2 - 1);
        game_time = (clock() - start_time)/CLOCKS_PER_SEC - tries *DELAY;
        if (total_digits > game_time )
            score += 10*(game_time-total_digits);  //Add points for speed
        printf("\n\nGame time was %u seconds. Your score is % u.", game_time,score);
        fflush(stdin);   //Clear the input buffer

        //Check if a new game is required
        printf("\nDo you want to play again(y/n)?");
        scanf_s(" %c", &another_game);
    }while(toupper(another_game=='Y'));
    return 0;
}
搜索更多相关主题的帖子: the unsigned for sequence printf 
2017-09-23 19:20
TXysilence
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2017-9-7
收藏
得分:0 
VS2010里无法使用<stdbool.h>,所以用了define,也不知道为什么
2017-09-23 19:22
TXysilence
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2017-9-7
收藏
得分:0 
//Simple Simon
#include<stdio.h>
#include<ctype.h>
#define bool int
#define true 1
#define false 0
#include<stdlib.h>
#include<time.h>

int main(void)
{
    char another_game = 'Y';
    const unsigned int DELAY = 1;
    bool correct = true;
    unsigned int tries = 0;
    unsigned int digits = 0;
    time_t seed = 0;
    unsigned int number = 0;
    time_t wait_start = 0;
    clock_t start_time = 0;
    unsigned int score = 0;
    unsigned int total_digits = 0;
    unsigned int game_time = 0;
    unsigned int i;

    printf("\nTo play Simple Simon,");
    printf(" watch the screen for a swquence of digits.");
    printf("\nWatch carefully , as the digits are only displayed"
        " for %u second%s ",DELAY,DELAY > 1 ? "s!" : "!");
    printf("\nThe computer will remove them , and then prompt you");
    printf(" to rnter the same sequemce.");
    printf("\nWhen you do , you must put spaces between the digits.\n");
    printf("\nGood Luck!\nPress Enter to play\n");

    scanf_s("%c",&another_game);

    do
    {
        correct = true ;
        tries = 0;
        digits = 2;
        start_time = clock();

        while(correct)
        {
            ++tries;
            wait_start = clock();

            srand((unsigned)time(&seed));
            for(i = 1; i <= digits;++i)
                printf("%u ",rand() % 10);
            for(; clock()-wait_start <DELAY*CLOCKS_PER_SEC;)
                printf("\r");
            for (i = 1; i<=digits;++i)
                printf("  ");
            if(tries==1)
                printf("\nNow you enter the sequence _ don't forget" " the spaces.\n");
            else
                printf("\r");
            srand((unsigned)seed);
            for (i = 1; i <= digits; ++i)
            {
                scanf_s("%u", &number);
                if(number != rand() % 10)
                {
                    correct = false;
                    break;
                }
            }
            if (correct && ((tries % 3) ==0))
                ++digits;
            printf("%s\n",correct ? "Correct!" : "Wrong!");
        }
        score = 10*( digits-((tries % 3)==1));
        total_digits = digits*(((tries % 3)==0) ? 3 : tries%3);
        if(digits > 2)
            total_digits +=3*((digits - 1) *(digits-2)/2 -1);
        game_time = (clock()- start_time)/CLOCKS_PER_SEC-tries*DELAY;
        if(total_digits > game_time)
            score +=10*(game_time - total_digits);
        printf("\n\nGame time was %u seconds.Your score is %u." ,game_time,score);
        fflush(stdin);
        printf("\nDo you want to play again(y/n)?");
        scanf_s("%c",&another_game);
    }while(toupper(another_game)=='Y');
    return 0;
}
2017-09-24 15:58
快速回复:记忆测试小游戏,无法按逻辑正常运行,找不到原因
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035466 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved