|
|
|
@ -4,9 +4,14 @@ |
|
|
|
|
#include <math.h> |
|
|
|
|
#include <string.h> |
|
|
|
|
#include <stdbool.h> |
|
|
|
|
#include <unistd.h> |
|
|
|
|
|
|
|
|
|
#define MAX_KEYS 256 |
|
|
|
|
#define MAX_MALLOC_VALUE 256 |
|
|
|
|
#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */ |
|
|
|
|
#define BOLDRED "\033[1m\033[31m" /* Bold RED */ |
|
|
|
|
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */ |
|
|
|
|
#define RESET "\033[0m" |
|
|
|
|
|
|
|
|
|
typedef struct key_value { |
|
|
|
|
int key; |
|
|
|
@ -47,59 +52,54 @@ int main(int argc, char *argv[]){ |
|
|
|
|
buf[strlen (buf) - 1] = '\0'; |
|
|
|
|
|
|
|
|
|
tmp = strtok(buf, ","); |
|
|
|
|
tmp_value = strdup(tmp); |
|
|
|
|
tmp_value = strdup(tmp); //create copy of the value and store that in to tmp_value
|
|
|
|
|
|
|
|
|
|
kv[number_of_items].value = tmp_value; |
|
|
|
|
strcpy(kv[number_of_items].value, tmp_value); |
|
|
|
|
|
|
|
|
|
//printf("VAL-->\ttmp[%d]: %s\n", size, tmp_value); //debug
|
|
|
|
|
strcpy(kv[number_of_items].value, tmp_value); //copy that value into the kv
|
|
|
|
|
|
|
|
|
|
tmp = strtok(NULL, ","); |
|
|
|
|
kv[number_of_items].key = atoi(tmp); |
|
|
|
|
//printf("KEY-->\ttmp[%d]: %s\n", size, tmp); //debug
|
|
|
|
|
|
|
|
|
|
++number_of_items; |
|
|
|
|
++number_of_items; //keep count of items, helps us with generating random numbers
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
fclose(file); |
|
|
|
|
free(buf); |
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* "Game" Logic |
|
|
|
|
* In this game, we need to print the fruit/veg name from the key/value's VALUE |
|
|
|
|
* and then we need to compare the INT value the user inputs |
|
|
|
|
* with the int value returned from the key/value's KEY |
|
|
|
|
* |
|
|
|
|
* random int ---> kv[r].key (int) and kv[r].value (char[]) |
|
|
|
|
* user int --> compare(kr[r].key, user_int); |
|
|
|
|
* 0 if match, 1 if not |
|
|
|
|
*/ |
|
|
|
|
//print_key_values(kv, size); //debug
|
|
|
|
|
|
|
|
|
|
// MAIN GAME LOOP
|
|
|
|
|
char line[256]; |
|
|
|
|
int input; |
|
|
|
|
int score = 0; |
|
|
|
|
|
|
|
|
|
system("clear"); |
|
|
|
|
printf(BOLDYELLOW "CTRL-D to leave the PLU test\n" RESET); |
|
|
|
|
while(1){ |
|
|
|
|
int random = rand() % number_of_items; //generate a random number with the number of items
|
|
|
|
|
|
|
|
|
|
char* item_to_guess = kv[random].value; |
|
|
|
|
int code_to_guess = kv[find_key_index(kv, item_to_guess, number_of_items)].key; |
|
|
|
|
|
|
|
|
|
printf("--> %s: ", item_to_guess); |
|
|
|
|
printf("[%d]--> %s: ", score, item_to_guess); |
|
|
|
|
|
|
|
|
|
if (fgets(line, sizeof(line), stdin)) { |
|
|
|
|
if (fgets(line, sizeof(line), stdin) && input != EOF) { |
|
|
|
|
if (sscanf(line, "%d", &input) == 1) { |
|
|
|
|
if(input == code_to_guess){ |
|
|
|
|
printf("Correct!\n"); |
|
|
|
|
printf(BOLDGREEN "✓ Correct!\n" RESET); |
|
|
|
|
++score; |
|
|
|
|
} else { |
|
|
|
|
printf("Nope! It's: %d\n", code_to_guess); |
|
|
|
|
printf(BOLDRED "✗ Nope! It's: %d\n" RESET, code_to_guess); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
printf("AWW WHAT DID YOU DOOOOO"); |
|
|
|
|
exit(EXIT_FAILURE); |
|
|
|
|
printf(BOLDYELLOW "\nOnly whole number integers can be entered, Sorry!\n" RESET); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
sleep(2); |
|
|
|
|
system("clear"); |
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
printf(BOLDYELLOW "\n\nYour score was %d\nBye!" RESET, score); |
|
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|