Problem00
unknown
scheme
3 years ago
4.7 kB
1
Indexable
Never
#include<stdio.h> #include<stdlib.h> #include<time.h> //defining functions int nComputerGuess(int, int); // function for computers' guess int nUserGuess(int, int); // function for user's guess int nRandomNumber(int, int); // function for random number void nScores(int, int); // and function for the scores int main () { int nMin = 1; // setting the range from 1 to 101 because the secret number can be 100, and so the range also includes 100 int nMax= 101; int nComputerScore, nUserScore; // variables for scores of each player printf("Welcome to Guess The Number Game. Play and Win. \n"); nComputerScore = nComputerGuess(nMin, nMax); // called the function for computers guess and set its score to be the returned variable nUserScore = nUserGuess(nMin, nMax); // called the function for users guess and set its score to be the returned variable nScores(nComputerScore, nUserScore); // and called the function for the scores return 0; } //function declaration int nRandomNumber(int nMin, int nMax){ // declared the function for random number return (rand()%(nMax - nMin))+nMin; // you subtract the max number (say 101) buy the min number (say 1) so that we gett 100 numbers starting with the number 1 } int nComputerGuess(int nMin, int nMax){ int nCompGuess; // this is the variable for computer guess int nUserInput; // this is the variable for user's secret number int nCounter = 1; // set counter to 1 so that if it guesses in the first try it has at least 1 as a score in the end. srand(time(NULL)); // to get random set of numbers whenever we run the program printf("Please enter your secret number: \n"); scanf("%d", &nUserInput); nCompGuess = nRandomNumber(nMin, nMax); //so the computer initial guess is random number from 1-100 printf("\n The computer guessed: %d\n", nCompGuess); while( nCompGuess != nUserInput ){ // while he doesn't guess the number if (nCompGuess < nUserInput){ // and if the computer guess guesses a number smaller than the user's input printf("\n The secret number is bigger than %d\n ", nCompGuess); // then its a bigger number nMin = nCompGuess + 1; // to make the computer smarter, we make the minimum to the number he already guessed plus 1 so he wouldn't guess that number } else { printf("\n The secret number is smaller than %d\n", nCompGuess); nMax = nCompGuess; // to make the computer smarter, we make the maximum value to that guess so that its a range between minimum and that number } nCounter++; // increase the counter to keep track of how many guesses nCompGuess = nRandomNumber(nMin, nMax); // calling the function again, so he guesses again but with new parameters printf(" The computer guessed: %d \n", nCompGuess); } printf("Correct. Indeed %d is the user's secret number. \n", nCompGuess); printf("The computer's guesses are : %d \n", nCounter); return nCounter; } int nUserGuess(int nMin, int nMax){ int nUserGuess1; // variable for user's guess int nCompSecretNum; // variable for computer secret number int nCounter1 = 1; // set counter to 1 so that if he guesses in the first try to have at least 1 guess. srand(time(NULL)); //to get random set of numbers whenever we run the program printf("Please enter your guess for computer's secret number: \n"); scanf("%d", &nUserGuess1); nCompSecretNum = nRandomNumber(nMin, nMax); // so the computer secret number is random number from 1-100 while( nUserGuess1 != nCompSecretNum){ // while user hasnt guessed it if ( nUserGuess1 < nCompSecretNum ){ // if he entered a smaller number than the computer secret number printf("The secret number is bigger than %d\n", nUserGuess1); // then the number is bigger } else{ printf("The secret number is smaller than %d\n", nUserGuess1); // else is smaller } nCounter1++; // increase the counter to keep score printf("Please guess again: \n"); scanf("%d", &nUserGuess1); } printf("Correct. Indeed %d is the computer's secret number. \n", nUserGuess1); printf("The user's guesses are: %d \n", nCounter1); return nCounter1; } void nScores(int nComputerScore, int nUserScore){ // function to print the scores if ( nComputerScore < nUserScore ){ // if the users score is biggger than the computer score printf("The winner is the computer. \n"); // computerr won } else if ( nComputerScore > nUserScore ){ // if the computers score is bigger than the computer score printf("The winner is the user. \n"); // user won } else{ printf("The result is a tie. We have two winners. \n"); // else its a tie } }