C hw

mail@pastecode.io avatar
unknown
scheme
2 years ago
2.5 kB
5
Indexable
Never
#include<stdio.h>
#include<stdlib.h>
#include<time.h>


int level1(int); // declaring function for level 1 
int level2(int);
int main(){
	int nMin=1;
	int nMax=10;
	int numOfLives1 = 3;
	srand(time(NULL));
	level1(numOfLives1);
	
	return 0;

}
//defining the function
int level1(int numOfLives1){
	int nComputerSecretNumber;	
	int nUserInput;
	int nMin = 1;
	int nMax = 10;
	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;


	while(nComputerSecretNumber != nUserInput && numOfLives1 != 0){
		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 ){
		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. \n", nComputerSecretNumber);
	}
	return numOfLives1;

}

int level2(int numOfLives2){
	int nComputerSecretNumber;	
	int nUserInput;
	int nMin = 1;
	int nMax = 100;
	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){
			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 ( numOfLives1 == 0 ){
		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. \n", nComputerSecretNumber);
	}
	return numOfLives2;

}