time or display problem I am not sure!

This is a discussion on time or display problem I am not sure! within the C Programming forums, part of the General Programming Boards category; Hi I have went through a tutorial teaching me time and rand. The game is simple simon and the user ...

  1. #1
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62

    time or display problem I am not sure!

    Hi I have went through a tutorial teaching me time and rand. The game is simple simon and the user tries to enter the same digits as the computer. This program works, ie If I match the computer's values it continues, with the list of entries getting longer. The problem is that the logic for displaying the sequence to match is supposed to be only displayed for 1 sec and then removed.
    The code is:

    Code:
    /*
       Name:    4.12_simpleSimon
       Date:    09-APR-2007 (Easter Monday)
       Version: 1.0
       Task:    A simple guessing game.
    */
    
    #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)
    {
      /* records if another game is to be played */
      char another_game = 'Y';
       
      /*  true if correct sequence entered */
      bool correct = true;            
       
      /* number of sequences entered successfully */
      int counter = 0;
       
      int sequence_length = 0;   /* number of digits in a sequence        */
      time_t seed = 0;           /* seed value for random number sequence */
      int number = 0;            /* stores an input digit                 */
      int i;
       
      time_t startTime = 0;      /* stores current time - seed for random values */
      int time_taken = 0;        /* time taken for game in seconds               */
       
      /* Instructions */
      printf("\nTo play Simple Simon, ");
      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 then remove them and prompt you");
      printf("\nto 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("&#37;c", &another_game); /* simply here to wait for return */
       
      /* one outer loop iteration is one game */
      do 
      {
        correct = true;          /* starting off the game sequence is correct      */
        counter = 0;             /* initialize count of number of successful tries */
        sequence_length = 2;     /* initial length of digit sequence               */
        time_taken = clock();    /* record current time at start of game           */
         
        while(correct)
        {
          /* on every third successful try, increase the sequence length  */
          sequence_length += counter++%3 == 0;
           
          /* set seed to be the number of seconds since Jan 1, 1970       */
          seed = time(NULL);
          startTime = clock();        /* record start time for sequence */
           
          /* generate a sequence of numbers and display the number        */
          srand((unsigned int)seed);    /* initialize the random sequence */
          for(i=1; i <= sequence_length; i++)
            printf("%d ", rand()%10);   /* output a random digit          */
           
          /* wait one second */
          for(; clock() - startTime < CLOCKS_PER_SEC; );  
            
          /* now overwrite the digit sequence */
          printf("\r");        /* go to beginning of line */
          for(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(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\nYour score is %d.", --counter * 100 / time_taken);
        fflush(stdin);  /* clear keyboard buffer */
         
        /* check if a new game is required */
        printf("\nDo you want to play again (y/n) ? ");
        scanf("%c", &another_game);
      } while(toupper(another_game) == 'Y');
      return 0;
    }
    /*
    output:
    */

    See the section labelled /* now overwrite the digit sequence */ above.
    This is done with /r and and then you are asked to enter it.

    What actually happens is that I don't see the entry at all. However if I remmed out the code
    under the section /* now overwrite the digit sequence */ I can see the entries as the following run shows:
    ------------------------------------------------------
    Press Enter to play

    8 3 6 my entries -> 8 3 6
    Correct!
    5 5 6 5 5 6
    Correct!
    7 4 2 7 4 2
    Correct!
    3 6 9 4 3 6 9 4
    Correct!
    9 0 9 9 9 0 9 9
    Correct!
    2 6 8 0 2 6 8 1
    Wrong!


    Your score is 83.
    Do you want to play again (y/n) ? hamster@freedom:~/storage/it/c/lessons/4_loops>

    BUT if I run the code as shown at the top here:
    ------------------------------------------------------
    Press Enter to play


    Now you enter the sequence - don't forget the spaces
    4 5 6
    Wrong!


    Your score is 0.
    Do you want to play again (y/n) ? hamster@freedom:~/storage/it/c/lessons/4_loops>
    ------------------------------------------------------

    See the line above that says "Now you enter the sequence " - I never got to see the numbers above it! Not sure what the problem is, but /r goes to the beginning of a line and " " two spaces are used for each digit to blank them and is supposed to happen after 1 sec.

    eg.
    /* wait one second */
    for(; clock() - startTime < CLOCKS_PER_SEC; );

    Now I am not sure if this is a time issue since the time calculation makes sense.... but If I rem it out as I said I can see the generated numbers and my entries. With the /r editing it is never seen... it sounds like 1 sec is not calculated correctly somehow. Hmmmm

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You have to flush the output buffer after displaying the sequence.
    Code:
          for(i=1; i <= sequence_length; i++)
            printf("%d ", rand()%10);   /* output a random digit          */
          fflush(stdout);
    This works fine for me ( anything but fflush(stdout); )
    BTW srand() should only be called once in a program.
    Kurt

  3. #3
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    Quote Originally Posted by ZuK View Post
    You have to flush the output buffer after displaying the sequence.
    Code:
          for(i=1; i <= sequence_length; i++)
            printf("%d ", rand()%10);   /* output a random digit          */
          fflush(stdout);
    This works fine for me ( anything but fflush(stdout); )
    BTW srand() should only be called once in a program.
    Kurt
    Thanks Kurt... I'll try fflush(stdout);

    Btw, was used twice - once to show the user and again to restart the same seed to compare...

    srand((unsigned int)seed); /* initialise the sequence */
    .... 4 5 6
    is shown to the user

    srand((unsigned int)seed); /* restart the random sequence */
    .... 4 5 6
    is checked against the user (ie, I re-used the same seed to make sure 4 5 6 appeared)
    srand is called a 2nd time to restart the sequence to check the user's matched the 1st time it was displayed.

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Ok. I didn't recognize that you never stored the random numbers. good idea.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with my time code
    By dol.beers in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2008, 01:37 PM
  2. Help: Display time and date
    By stansu in forum C++ Programming
    Replies: 5
    Last Post: 08-22-2003, 02:27 PM
  3. Display problem
    By RoD in forum C++ Programming
    Replies: 11
    Last Post: 10-09-2002, 05:25 PM
  4. time.h Display time
    By SlimDady in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2002, 09:04 PM
  5. display problem after retrieving from text
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 01-29-2002, 05:48 AM

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