taeyounkim LOG

Guessing a Number game codes 본문

Career&Study/Coding Practice

Guessing a Number game codes

taeyounkim 2021. 7. 5. 08:29
728x90

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAXNUM 100

int secret_number; /*external value*/
int number_of_guess = 0; /*external value*/

void secret_num(){
srand((unsigned)time(NULL));

secret_number = rand() % MAXNUM + 1;
}

void process(){
int guess;


printf("Enter guess: ");
scanf("%d", &guess);

if(guess > secret_number){
printf("Too high. try again.\n");
number_of_guess++;
process();
}

else if(guess < secret_number){
printf("Too low. try again.\n");
number_of_guess++;
process();
}

else{
number_of_guess++;
printf("You won in %d guesses!\n\n", number_of_guess);
}
}

int main(void){

char response;

printf("Guess the secret number between 1 and 100: ");

do{
secret_num();
printf("\nA new number has been chosen\n");

number_of_guess = 0; /*this thread was needed in order to reset the number of guesses into zero every round. Otherwise, the number of guesses would be the aggregate of all the guesses from the previous rounds.*/
process();

printf("Play again? ");
scanf(" %c", &response);

} while(response=='y');

return 0;

}

728x90