diff --git a/.drone.yml b/.drone.yml index d2f37e9..4910f41 100644 --- a/.drone.yml +++ b/.drone.yml @@ -13,13 +13,29 @@ steps: - cmake CMakeLists.txt - make -- name: valgrind-test - pull: always +- name: valgrind-test0 + pull: if-not-exists image: immawanderer/archlinux-cdev commands: - - valgrind --log-file=valgrind_output ./pjc-0x02 < ./test_input > output + - valgrind --log-file=valgrind_output ./pjc-0x02 < ./test_input0 > /dev/null - cat valgrind_output - grep "no leaks are possible" valgrind_output + when: + status: + - success + - failure + +- name: valgrind-test1 + pull: if-not-exists + image: immawanderer/archlinux-cdev + commands: + - valgrind --log-file=valgrind_output ./pjc-0x02 < ./test_input1 > /dev/null + - cat valgrind_output + - grep "no leaks are possible" valgrind_output + when: + status: + - success + - failure --- diff --git a/main.c b/main.c index 6a7514b..da22964 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,7 @@ #include #include #include +#include void print_menu(){ printf("\n[*] guess a number from 0 to 10\n"); @@ -33,6 +34,7 @@ int get_userguess(){ ok = scanf("%d", &x); if(ok == EOF){ printf("[*] please, come on...\n"); + return 9000; } if(ok == 0){ while (fgetc(stdin) != '\n'){ @@ -73,6 +75,7 @@ void guess(){ ok = scanf("%d", &userchoice); if(ok == EOF){ printf("[*] please, come on...\n"); + return; } if(ok == 0){ while (fgetc(stdin) != '\n'){ @@ -126,9 +129,116 @@ void print_rand_bs(){ } } -int main() +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: + printf("quitting...\n"); + lemmego(); + return; + default: + printf("invalid option\n"); + selection = 0; + break; + } + } +} + + +int main(int argc, char *argv[]) { - guess(); print_rand_bs(); + guess(); + another_bs(); return 0; } diff --git a/test_input b/test_input0 similarity index 94% rename from test_input rename to test_input0 index 92a694c..97f2feb 100644 --- a/test_input +++ b/test_input0 @@ -11,3 +11,4 @@ ok1 2 2 ktxbai +8 diff --git a/test_input1 b/test_input1 new file mode 100644 index 0000000..1b6770d --- /dev/null +++ b/test_input1 @@ -0,0 +1,16 @@ +q +1 +323 +2 +3 +2 +4 +1 +5 +4 +6 +9000 +7 +9001 +gibberish +8