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>
|
|
|
|
|
|
|
|
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"
|
2020-03-02 13:58:56 +01:00
|
|
|
"\t...or anything else to exit\n\n");
|
2020-03-01 02:06:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2020-03-05 06:04:38 +01:00
|
|
|
return 9000;
|
2020-03-01 02:06:06 +01:00
|
|
|
}
|
|
|
|
if(ok == 0){
|
|
|
|
while (fgetc(stdin) != '\n'){
|
|
|
|
}
|
|
|
|
printf("[*] give me a correct value\n");
|
2020-03-02 13:58:56 +01:00
|
|
|
printf("[*] stop gibbering around\n my guess >>> ");
|
2020-03-01 02:06:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2020-03-01 21:46:53 +01:00
|
|
|
void compare_guess_with_reality(int userguess, int reality){
|
|
|
|
if(userguess == reality){
|
|
|
|
printf("[*] you won because you've got luck. try me next time!\n");
|
|
|
|
}else if(userguess < reality){
|
|
|
|
printf("[*] you lost!\n");
|
2020-03-02 13:58:56 +01:00
|
|
|
printf("\thint: your guess was just a tad lower than the secret number\n");
|
2020-03-01 21:46:53 +01:00
|
|
|
}else{
|
2020-03-02 13:58:56 +01:00
|
|
|
printf("[*] you lost!\n");
|
|
|
|
printf("[i] hint: your guess was just a tad higher than the secret number\n");
|
2020-03-01 21:46:53 +01:00
|
|
|
}
|
2020-03-01 02:06:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void guess(){
|
|
|
|
srand(time(0));
|
|
|
|
int userchoice = -1;
|
2020-03-02 13:58:56 +01:00
|
|
|
int nurand = -1;
|
2020-03-01 02:06:06 +01:00
|
|
|
printf("\nwelcome to the guessing game\n");
|
|
|
|
|
|
|
|
while (userchoice != 3){
|
|
|
|
int userguess = -1;
|
|
|
|
int ok = -1;
|
|
|
|
|
|
|
|
print_menu();
|
|
|
|
|
|
|
|
while(ok != 1){
|
2020-03-02 13:58:56 +01:00
|
|
|
printf("[*] you >>> ");
|
2020-03-01 02:06:06 +01:00
|
|
|
ok = scanf("%d", &userchoice);
|
|
|
|
if(ok == EOF){
|
|
|
|
printf("[*] please, come on...\n");
|
2020-03-05 06:04:38 +01:00
|
|
|
return;
|
2020-03-01 02:06:06 +01:00
|
|
|
}
|
|
|
|
if(ok == 0){
|
|
|
|
while (fgetc(stdin) != '\n'){
|
|
|
|
}
|
|
|
|
lemmego();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch( userchoice ) {
|
|
|
|
case 1:
|
|
|
|
nurand = get_rand();
|
|
|
|
printf( "[?] what is your guess then? pick a number from 0 to 10\n" );
|
2020-03-02 13:58:56 +01:00
|
|
|
printf(" my guess >>> ");
|
2020-03-01 02:06:06 +01:00
|
|
|
|
|
|
|
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
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2020-03-02 13:58:56 +01:00
|
|
|
if(nurand == -1){
|
|
|
|
printf("[!] there wasn't a first time yet, mate...\n");
|
|
|
|
break;
|
|
|
|
}
|
2020-03-01 02:06:06 +01:00
|
|
|
printf("[*] guess again\n");
|
2020-03-02 13:58:56 +01:00
|
|
|
printf( "[?] what is your guess then? pick a number from 0 to 10\n" );
|
|
|
|
printf(" my guess >>> ");
|
|
|
|
|
|
|
|
userguess = get_userguess();
|
|
|
|
compare_guess_with_reality(userguess, nurand);
|
2020-03-01 02:06:06 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
userchoice = 3;
|
|
|
|
lemmego();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-27 22:42:14 +01:00
|
|
|
|
2020-03-02 13:58:56 +01:00
|
|
|
void print_rand_bs(){
|
|
|
|
printf("\n\n[*] printing rand\n");
|
|
|
|
printf("seed\trand1\trand2\trand3\trand4\trand5\trand6\trand7\n");
|
|
|
|
for (unsigned int i = 1; i < 6; i++){
|
|
|
|
unsigned int seed = i;
|
|
|
|
srand(seed);
|
|
|
|
printf("%d\t", seed);
|
|
|
|
for (int j = 0; j < 7; j++){
|
|
|
|
printf("%d\t", rand() % 101);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 06:04:38 +01:00
|
|
|
void read_selection(int selection){
|
|
|
|
int ok;
|
|
|
|
while(ok != 1) {
|
|
|
|
ok = scanf("%d", &selection);
|
|
|
|
if(ok == EOF){
|
|
|
|
printf("[*] please, come on...\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(ok == 0){
|
|
|
|
while (fgetc(stdin) != '\n'){
|
|
|
|
}
|
|
|
|
lemmego();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void default_flow(char operator, int a, int b, int result){
|
|
|
|
int answer = 0;
|
|
|
|
printf("a: %d\tb: %d\n", a, b);
|
|
|
|
printf("what is %d %c %d?\n>> ", a, operator, b);
|
|
|
|
answer = get_userguess();
|
|
|
|
if (answer == result){
|
|
|
|
printf("splendid. you win %d czk!\n", rand());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("incorrect, %d %c %d is %d.\n", a, operator, b, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void another_bs(){
|
|
|
|
int a, b, result, answer, selection;
|
|
|
|
int upper_limit = 10;
|
|
|
|
|
|
|
|
srand(time(NULL));
|
|
|
|
|
|
|
|
printf("\n\nwelcome to the math game\n");
|
|
|
|
while ( selection != 8 ) {
|
|
|
|
printf("\npick a number\n");
|
|
|
|
printf("1 - addition\n"
|
|
|
|
"2 - subtraction\n"
|
|
|
|
"3 - multiplication\n"
|
|
|
|
"4 - division\n"
|
|
|
|
"5 - modulo\n"
|
|
|
|
"6 - set upper_limit for x = rand() mod upper_limit\n"
|
|
|
|
"7 - XOR\n"
|
|
|
|
"8 - quit\n"
|
|
|
|
">> ");
|
|
|
|
|
|
|
|
selection = get_userguess();
|
|
|
|
|
|
|
|
a= 1 + (rand() % upper_limit);
|
|
|
|
b = 1 + (rand() % upper_limit);
|
|
|
|
|
|
|
|
switch ( selection ) {
|
|
|
|
case 1:
|
|
|
|
result = a + b;
|
|
|
|
default_flow('+', a, b, result);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
result = a - b;
|
|
|
|
default_flow('-', a, b, result);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
result = a * b;
|
|
|
|
default_flow('*',a , b, result);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
result = a / b;
|
|
|
|
default_flow('/', a, b, result);
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
printf("a: %d\tb: %d\n", a, b);
|
|
|
|
result = a % b;
|
|
|
|
printf("what is %d mod %d?\n>> ", a, b);
|
|
|
|
answer = get_userguess();
|
|
|
|
if (answer == result)
|
|
|
|
printf("splendid. you win %d czk!\n", rand());
|
|
|
|
else
|
|
|
|
printf("incorrect, %d mod %d je %d.\n", a, b, result);
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
printf(" default limit: 10\n"
|
|
|
|
" * current limit: %d\n", upper_limit);
|
|
|
|
printf(" new limit\t>> ");
|
|
|
|
upper_limit = get_userguess();
|
|
|
|
printf("\n * new limit: %d\n", upper_limit);
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
result = a ^ b;
|
|
|
|
default_flow('^',a, b, result);
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
lemmego();
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
printf("invalid option\n");
|
|
|
|
selection = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
2020-02-27 22:42:14 +01:00
|
|
|
{
|
2020-03-02 13:58:56 +01:00
|
|
|
print_rand_bs();
|
2020-03-05 06:04:38 +01:00
|
|
|
guess();
|
|
|
|
another_bs();
|
2020-02-27 22:42:14 +01:00
|
|
|
return 0;
|
|
|
|
}
|