News:

Welcome to RetroCoders Community

Main Menu

computer guess human number game in C

Started by ron77, Jun 04, 2023, 05:31 PM

Previous topic - Next topic

ron77

okay here is it:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <windows.h>

const int MAX_NUMBER = 100;
const int MIN_NUMBER = 0;

int main() {
	int input = 0;
	int guess = 50;
	int max_number = MAX_NUMBER;
	int min_number = MIN_NUMBER;
	// bool isOver = false;
	int turns = 10;
	
	for (int i = 0 ; i < 10 ; i++) {
		system("cls");
		printf("computer guess human number\n\ni'll try to guess your number between 0 and 100\n\n");
		printf("number of turns left : %i\n\n", (11-turns));
		printf("press key 1 if guess is too high\n\npress key 2 if guess is too low\n\npress key 3 if guess is correct\n\n");
		
		printf("is your number - %i ?\n\n", guess);
		scanf("%i", &input);
		
		if (input < 1 || input > 3) {
			printf("\n\ninviled input! please select 1 2 3 only!");
		}else if (input == 1) {
			max_number = guess;
			guess = min_number + (max_number - min_number) / 2; 
		}else if (input == 2) {
			min_number = guess;
			guess = min_number + (max_number - min_number) / 2;
		}else if (input == 3) {
			printf("\n\nuessed correct number is %i", guess);
			break;
		}
		turns--;
		Sleep(5000);
		
		
	}
		printf("\n\nGAME OVER");
	
	return 0;
}