diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..90902d4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +plu diff --git a/main.c b/main.c index 911b65f..352a494 100644 --- a/main.c +++ b/main.c @@ -4,9 +4,14 @@ #include #include #include +#include #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); } } diff --git a/plu.csv b/plu.csv index 4836aa1..175ddfc 100644 --- a/plu.csv +++ b/plu.csv @@ -5,4 +5,5 @@ Apple,26 Sweet Potatoe,24 Spring Onion,317 Croissant,70 +Large Avacados,365 Choc Croissant,170