2020-02-27 22:42:14 +01:00
|
|
|
#include <stdio.h>
|
2020-03-01 02:06:06 +01:00
|
|
|
#include <time.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
void print_menu(){
|
|
|
|
printf("\n[*] guess a number from 0 to 10\n");
|
|
|
|
printf( "[*] press:\n"
|
|
|
|
"\t1 to play\n"
|
|
|
|
"\t2 to try again\n"
|
|
|
|
"\t...or anything else to exit\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int lemmego(){
|
|
|
|
printf("[*] you wished to go, quitting...\n");
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_rand(){
|
|
|
|
int ar [10];
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
int dat_num = 1+ (rand() % 10);
|
|
|
|
ar[i] = dat_num;
|
|
|
|
}
|
|
|
|
int r = ar[rand() % 10];
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_userguess(){
|
|
|
|
int x = -1;
|
|
|
|
int ok = -1;
|
|
|
|
while( ok != 1){
|
|
|
|
ok = scanf("%d", &x);
|
|
|
|
if(ok == EOF){
|
|
|
|
printf("[*] please, come on...\n");
|
|
|
|
}
|
|
|
|
if(ok == 0){
|
|
|
|
while (fgetc(stdin) != '\n'){
|
|
|
|
}
|
|
|
|
printf("[*] give me a correct value\n");
|
|
|
|
printf("[*] stop gibbering around\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2020-03-01 21:46:53 +01:00
|
|
|
void compare_guess_with_reality(int userguess, int reality){
|
2020-03-01 02:06:06 +01:00
|
|
|
printf("[*] guess was: %d\n", userguess);
|
|
|
|
printf("[*] reality is: %d\n", reality);
|
|
|
|
|
2020-03-01 21:46:53 +01:00
|
|
|
if(userguess == reality){
|
|
|
|
printf("[*] you won because you've got luck. try me next time!\n");
|
|
|
|
}else if(userguess < reality){
|
|
|
|
printf("[*] you lost!\n");
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
}
|
2020-03-01 02:06:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void guess(){
|
|
|
|
srand(time(0));
|
|
|
|
int userchoice = -1;
|
|
|
|
printf("\nwelcome to the guessing game\n");
|
|
|
|
|
|
|
|
while (userchoice != 3){
|
|
|
|
int userguess = -1;
|
|
|
|
int nurand = -1;
|
|
|
|
int verdict = -1;
|
|
|
|
int ok = -1;
|
|
|
|
|
|
|
|
print_menu();
|
|
|
|
|
|
|
|
while(ok != 1){
|
|
|
|
printf("[*] you: ");
|
|
|
|
ok = scanf("%d", &userchoice);
|
|
|
|
if(ok == EOF){
|
|
|
|
printf("[*] please, come on...\n");
|
|
|
|
}
|
|
|
|
if(ok == 0){
|
|
|
|
while (fgetc(stdin) != '\n'){
|
|
|
|
}
|
|
|
|
// printf("[*] give me a correct value next time (an integer)\n");
|
|
|
|
lemmego();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch( userchoice ) {
|
|
|
|
case 1:
|
|
|
|
nurand = get_rand();
|
|
|
|
printf( "[?] what is your guess then? pick a number from 0 to 10\n" );
|
|
|
|
printf("\tmy guess: ");
|
|
|
|
|
|
|
|
userguess = get_userguess();
|
2020-03-01 21:46:53 +01:00
|
|
|
compare_guess_with_reality(userguess, nurand);
|
2020-03-01 02:06:06 +01:00
|
|
|
|
|
|
|
if (verdict == 0){
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
printf("[*] guess again\n");
|
|
|
|
userchoice = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
userchoice = 3;
|
|
|
|
lemmego();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-27 22:42:14 +01:00
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2020-03-01 02:06:06 +01:00
|
|
|
guess();
|
2020-02-27 22:42:14 +01:00
|
|
|
return 0;
|
|
|
|
}
|