#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int level1(int); // declaring function for level 1
int level2(int); // declaring function for level 2
int level3(int); // declaring function for level 3
int main(){
int numOfLives1 = 3;
int numOfLives2 = 5;
int numOfLives3 = 10;
srand(time(NULL)); // a formula found on google for random generator.
numOfLives2 = level1(numOfLives1); // calling the function and since i added the lives in the function, then the returned integer is the new lives for level 2
if ( numOfLives2 != 0 ){
numOfLives3 = level2(numOfLives2); // if user guesses the number within his lives, then he can go to level 3, which is also the same syntax as the second level live.
}
if ( numOfLives3 != 0){
level3(numOfLives3); // if user guesses the number within his lives, then he wins the game.
}
return 0;
}
//defining the function
int level1(int numOfLives1){ // defining the function for level 1
int nComputerSecretNumber; // variable for secret number
int nUserInput; // variable for user input
int nMin = 1; // setting the range between 1 and 10
int nMax = 10;
printf("Welcome to level 1 of the Number Guessing Game. \n");
printf("Enter the guess for computer's secret number: \n");
scanf("%d", &nUserInput);
numOfLives1--; // i did this because the user uses a guess when they guess, so the user loses 1 live.
nComputerSecretNumber = rand()%nMax+nMin; // setting a random to the computer's secret number
while(nComputerSecretNumber != nUserInput && numOfLives1 != 0){ // while the user input is not correct and the number of lives is not 0 execute
if (nUserInput < nComputerSecretNumber){
printf("The secret number is bigger than %d\n", nUserInput);
}
else{
printf("The secret number is smaller than %d\n", nUserInput);
}
printf("The number of the remaining guesses is: %d\n", numOfLives1);
numOfLives1--;
printf("The number is not correct. Try again.\n ");
scanf("%d", &nUserInput);
}
if ( numOfLives1 == 0){ // if is the last guess and he guesses in the last guess then we store that in the number of lives so that he can go to the second level
if (nUserInput == nComputerSecretNumber){
printf("Congratulations!!! %d was the correct number. \n", nComputerSecretNumber);
numOfLives1 += 5;
}
else{
printf(" You've used all your guesses. The game is over.\n"); //if he doesn't then game is over.
printf(" The correct number was %d", nComputerSecretNumber);
}
}
else{
printf("Congratulations!!! %d was the correct number. \n", nComputerSecretNumber); // or if he does before the last guess then add those plus the main lives for level 2
numOfLives1 += 5;
}
return numOfLives1;
}
int level2(int numOfLives2){
int nComputerSecretNumber; //same logic as level 1, except the range
int nUserInput;
int nMin = 1;
int nMax = 100;
printf("Welcome to level 2 of the Number Guessing Game. \n");
printf("Enter the guess for computer's secret number: \n");
scanf("%d", &nUserInput);
numOfLives2--; // i did this because the user uses a guess when they guess, so the user loses 1 live.
nComputerSecretNumber = rand()%nMax+nMin;
while(nComputerSecretNumber != nUserInput && numOfLives2 != 0){
if (nUserInput < nComputerSecretNumber){ //same logic as 1st level
printf("The secret number is bigger than %d\n", nUserInput);
}
else{
printf("The secret number is smaller than %d\n", nUserInput);
}
printf("The number of the remaining guesses is: %d\n", numOfLives2);
numOfLives2--;
printf("The number is not correct. Try again.\n ");
scanf("%d", &nUserInput);
}
if ( numOfLives2 == 0){ // and again same logic as in the first level, so if he guesses in the last live the game continues to level 3.
if (nUserInput == nComputerSecretNumber){
printf("Congratulations!!! %d was the correct number. \n", nComputerSecretNumber);
numOfLives2 += 10;
}
else{
printf(" You've used all your guesses. The game is over.\n"); // if not game is over
printf(" The correct number was %d", nComputerSecretNumber);
}
}
else{
printf("Congratulations!!! %d was the correct number. \n", nComputerSecretNumber); // or else if he finds before the last guess, add the lives plus the main lives in the 3rd level.
numOfLives2 += 10;
}
return numOfLives2;
}
int level3(int numOfLives3){
int nComputerSecretNumber; //same logic as in the upper levels, except the range
int nUserInput;
int nMin = 1;
int nMax = 1000;
printf("Welcome to level 3 of the Number Guessing Game. \n");
printf("Enter the guess for computer's secret number: \n");
scanf("%d", &nUserInput);
numOfLives3--; // i did this because the user uses a guess when they guess, so the user loses 1 live.
nComputerSecretNumber = rand()%nMax+nMin;
while(nComputerSecretNumber != nUserInput && numOfLives3 != 0){ //same logic as the upper levels
if (nUserInput < nComputerSecretNumber){
printf("The secret number is bigger than %d\n", nUserInput);
}
else{
printf("The secret number is smaller than %d\n", nUserInput);
}
printf("The number of the remaining guesses is: %d\n", numOfLives3);
numOfLives3--;
printf("The number is not correct. Try again.\n ");
scanf("%d", &nUserInput);
}
if ( numOfLives3 == 0){ // same logic as the upper levels
if (nUserInput == nComputerSecretNumber){
printf("Congratulations!!! %d was the correct number. You won the game!!. \n", nComputerSecretNumber);
}
else{
printf(" You've used all your guesses. The game is over.\n");
printf(" The correct number was %d", nComputerSecretNumber);
}
}
else{
printf("Congratulations!!! %d was the correct number. You won the game!! \n", nComputerSecretNumber);
}
}