C语言-简单的Simon游戏

游戏说明:计算机会在屏幕上将一串数字显示很短的时间。玩家必须在数字消失之前记住他们,然后输入这串数字。每次过关后,计算机会显示更长的一串数字,让玩家继续玩下去。玩家应尽可能使这个过程重复更多的次数。

#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#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;
    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;
    //how to play the game
     printf("To play Simple Simon,"
                        "watch the screen for a sequence 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"
                        "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{
        //Initialize game
        correct = true; 
        tries = 0;
        digits = 2;
        start_time = clock();
        //Inner loop continues as long as sequences are entered correctly
        while(correct){
            ++tries;
            wait_start = clock();
            //Generate a sequence of digits and display them
            srand(time(&seed));
            for(unsigned int i=1;i<=digits;++i){
                printf("%u ",rand() % 10);
            }
            /*Code to wait one second*/
            for(;clock() - wait_start < DELAY*CLOCKS_PER_SEC;);
            /*Code to overwrite the digit sequence*/
            printf("\r");       //Go to beginning of the line
            for(unsigned int i=1;i<=digits;++i)
                printf("  ");   //Output spaces to cover the number that already printf

            if(tries == 1)
                printf("\nNow you enter the sequence - don't forget"
                                        "the spaces\n");
            else
                printf("\r");

            /*Code to prompt for the input sequence*/
            srand(seed);
            for(unsigned int i=1;i<=digits;++i){
                //Read the input sequence & check against the original
                scanf("%u",&number);//read a digit
                if(number != rand()%10){//compare with generater digit
                    correct = false;
                    break;
                }
            } 
            /*on every third successful try,increase the sequence length*/
            if(correct && ((tries%3)==0))
                ++digits;
            printf("%s\n",correct?"Correct!" : "Wrong!");
        }
        /*output the score when the game is finished*/
        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 play again ?");
        scanf("%c",&another_game); 
    }while(toupper(another_game)=='Y');

    return 0;
} 

猜你喜欢

转载自blog.csdn.net/hahalalalalalala/article/details/81546232