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("%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