1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help need to understand simple simon game

Discussion in 'C' started by devraj_01, Nov 13, 2011.

  1. devraj_01

    devraj_01 New Member

    Nov 13, 2011
    Likes Received:
    Trophy Points:
    Hi, This is Deva.

    i am right now end of the loop chapter.

    Using compiler GNU-GCC -std-gnu99

    Host OS= WIN7

    Almost I understood the code. But except CLOCKS_PER_SEC,

    While declaring variables at the start of block.

    Its time_t now = 0;

    But while in the inner loop

    now = clock();
    seed = time(NULL);

    Is there anybody can help me understanding with this code.

    I have to start Arrays. After completing this. Your help will greatly appreciated.

    /*Program 4.12 Simple Simon*/
    #include<stdio.h>                           /*For input and output*/
    #include<ctype.h>                           /*For toupper() function*/
    #include<stdbool.h>                         /*For bool, true, false*/
    #include<stdlib.h>                          /*For rand() and srand()*/
    #include<time.h>                            /*For time() and clock()*/
    int main(void)
        char another_game = 'Y';                /*Records if another game is to be played*/
        int correct = true;                     /*true if correct sequence entered , false otherwise*/
        int counter = 0;                        /*Number of sequences entered successfully*/
        int sequence_length = 0;                /*Number of degit in a sequence*/
        time_t seed = 0;                        /*Seed value for random number sequence*/
        int number = 0;                         /*Stores an input digit*/
        time_t now = 0;                         /*Stores current time - seed for random values*/
        int time_taken = 0;                     /*Time taken for game in seconds*/
        printf("\n To play Simple Simon, ");    /*Describe how the game is played*/
        printf("watch the screen for a sequence of digits.");
        printf("\nWatch carefully, as the digits are only displayed" "for a second! ");
        printf("\nThe computer will remove them, and then prompt you ");
        printf("to enter the same sequence.");
        printf("\nWhen you do, you must put spaces between the degits. \n");
        printf("\nGood Luck!\nPress Enter to play\n");
        scanf("%c", &another_game);
        do                                      /*One outer loop iteration is one game*/
            correct = true;                     /*By default indicates correct sequence entered*/
            counter = 0;                        /*Initialize count of number of successful tries*/
            sequence_length = 2;                /*Initial length of a digit sequence*/
            time_taken = clock();               /*Record current time at start of game*/
            while(correct)                      /*Inner loop continues as long as a sequence are entered correctly*/
                sequence_length += counter++%3 == 0;
                                                /*on every third successful try, increase the sequence length*/
                seed = time(NULL);
                                                /*Set seed to be the number of seconds sine Jan 1,1970*/
                now = clock();                  /*record start time for sequence*/
                                                /*Generate a sequence of numbers and display the number*/
                srand((unsigned int)seed);      /*Initialize the random sequence*/
                for(int i = 1; i <= sequence_length; i++)
                    printf("%d", rand()%10);    /*Output a random digit*/
                                                /*Wait one second*/
                for(;clock() - now < CLOCKS_PER_SEC;);
                                                /*Now ouerwrite the digit sequence*/
                printf("\r");                   /*go to beginning of the line*/
                for(int i = 1; i <= sequence_length; i++)
                printf("  ");  /*Output two spaces*/
                if(counter == 1)                /*Only output message for the first try*/
                    printf("\nNow you enter the sequence - don't forget" " the spaces\n");
                    printf("\r");               /*Back to the beginning of the line*/
                                                /*Check the input sequence of digits against the original */
                srand((unsigned int)seed);      /*Restart the random sequence*/
                for(int i = 1; i<= sequence_length; i++)
                    scanf("%d", &number);       /*Read an input number*/
                    if(number != rand()%10)     /*Compare against random digit*/
                        correct = false;        /*Incorrect entry*/
                        break;                  /*No need to check further...*/
                printf("%s\n", correct? "Correct!" : "Wrong!");
            }                                   /*Calculate total time to play the game in seconds*/
            time_taken = (clock() - time_taken)/CLOCKS_PER_SEC;
                                                /*Output the game score*/
            printf("\n\n Your score is %d", --counter*100/time_taken);
                                                /*Check if new game required*/
            printf("\nDo you want to play again(y/n)? ");
            scanf("%c", &another_game);
        }while(toupper(another_game)== 'Y');
        return 0;                               /*Returns control to the operating system*/

Share This Page