Help need to understand simple simon game

This is a discussion on Help need to understand simple simon game within the C Programming forums, part of the General Programming Boards category; Hi, This is Deva. i am right now end of the loop chapter. Using compiler GNU-GCC -std-gnu99 Host OS= WIN7 ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    1

    Help need to understand simple simon game

    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.
    Code:
    /*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");
                    else
                    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);
            fflush(stdin);
                                                /*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*/
    }
    Last edited by Salem; 11-13-2011 at 01:16 AM. Reason: Move prose outside of tags

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    Almost I understood the code. But except CLOCKS_PER_SEC,
    While declaring variables at the start of block.
    Did you try entering CLOCKS_PER_SEC into the input field of the search engine of your choosing and clicking the button? See, what you do then is when the links come up, you click on a few of those and read them. For clock, try the same thing with "man clock" as your search term.

    Is there anybody can help me understanding with this code
    Just about every single line is commented...what more could you want?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    "help in understanding" => "I found this code, and I'm a clueless dolt".
    somewhere on the web

    Start writing your own code and stop trying to google your answers.

    > i am right now end of the loop chapter.
    Which means you didn't come up with sequence_length += counter++%3 == 0; all by yourself.

    You can't learn to cook simply from reading other people's recipe books.
    Until you've been in the kitchen for a while, cut yourself with a knife, had something boil over and perhaps even set fire to something, you will know NOTHING about the reality of cooking.

    As it is with programming, you're simply wasting time unless you actually WRITE YOUR OWN CODE.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone help me understand this very simple loop?
    By matthayzon89 in forum C Programming
    Replies: 2
    Last Post: 04-17-2010, 10:12 AM
  2. Simple question, trying to understand this code...
    By matthayzon89 in forum C Programming
    Replies: 11
    Last Post: 03-23-2010, 02:04 PM
  3. a game of Simon Says
    By skorman00 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 04-22-2005, 08:32 PM
  4. They Cut Simon's Head Off In Lord Of The Flies
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 10-19-2003, 05:25 PM
  5. Trying to understand game speed regulation
    By Vorok in forum Game Programming
    Replies: 3
    Last Post: 01-13-2003, 07:23 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21