Please, if you have the time and run and compile this program, I am looking for further instruction from someone on game_loop variable to countdown from 3 to 0 (reset it). I've tried with a while loop before and after the for () loop with no success. After three successful tries followed by a failure, and an "y" to continue with another game, just says "you have 1 try left", i'm looking for a way to reset the game_loop variable to reset it to start at 3 again, but it's broke. I've tried with this:
Code:
if (game_loop <= 1)
{
game_loop += 3;
}
I don't even know if the above is valid C but i also tried messing around with a while loop similar to what I did in the main body of the program to no available. Please direct me further.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
void delay (int seconds) // delay function i got this source code from google
{
unsigned long int count=333333333, i, j;
for(i = 0;i<seconds;i++)
for(j = 0; j < count; j++);
}
int main (void)
{
char c = ' '; // fill console with blank space to clear screen
int successful_tries = 0;
//char newgame = 0;
int num = 0;
int input = 0;
int limit = 9;
char get_input;
int limit_2 = 1;
int game_loop = 3;
printf("This is a game to test your memory, with a random number of 1 digit that will be displayed a max of 3 successive times.\n");
do
{
clock_t t;
t = clock();
for (; game_loop > 0; --game_loop) // count down how many tries left until program ends
{
// code to compute random digits
srand(time(NULL));
num = limit_2 + rand() % limit;
printf("\n%d\n", num);
delay(1); // create 1 second program delay before clear screen with 'X's
// clear screen with x's
for (int ctr_1 = 1; ctr_1 < 7500; ctr_1++)
{
printf("%c", c);
}
printf("\n\n");
printf("\nYou have %d tr%s left.\n", game_loop, game_loop == 1 ? "y" : "ies");
printf("What sequence was displayed?: ");
scanf("%d", &input);
if (input == num) // if correct match
{
printf("Correct Answer!\n");
int i = 0;
while (i < 1) // per one match (one correct match)
{
successful_tries += 1; // Successful tries for end of program
++i;
}
if (successful_tries == 3) // sequence was inserted 3 times in a row
{
limit += 191; // make sequence 3 digits
limit_2 += 49; // limit_2 = limit_2 + 49;
}
if (successful_tries >= 3)
{
game_loop += 1;
}
}
if (input != num)
{
printf("You entered an incorrect match!\n");
break;
}
}
// The program will then calculate a score based on the number of successful tries and the time taken and invite the player to play again.
t = clock() - t;
double program_execution = ((double)t)/CLOCKS_PER_SEC; // in seconds
printf("You had %d successful tr%s. Program execution took %f seconds.\n", successful_tries, successful_tries == 1 ? "y" : "ies", program_execution);
printf("Do you want to play another game? y for yes n for no\n");
scanf(" %c", &get_input);
if (tolower(get_input) == 'n') // if 'n' is inserted
{
printf("You entered %c\nProgram will end...\n", get_input);
return 0;
}
} while (tolower(get_input) == 'y');
return 0;
}