diff --git a/README.md b/README.md index e69de29..6eb3912 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,3 @@ +# LIDL PLU Codes + +This is very unfinished! diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..f8cf975 --- /dev/null +++ b/build.sh @@ -0,0 +1,6 @@ + #!/bin/bash + +set -xe +CFLAGS="" + +gcc $CFLAGS -o plu main.c diff --git a/main.c b/main.c new file mode 100644 index 0000000..911b65f --- /dev/null +++ b/main.c @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +#include + +#define MAX_KEYS 256 +#define MAX_MALLOC_VALUE 256 + +typedef struct key_value { + int key; + char* value; +} key_value; + +void print_key_values(key_value* kv, const int MAX); +int find_key_index(key_value* kv, const char* STRING, const int MAX); + +int main(int argc, char *argv[]){ + /* Seed time */ + srand(time(NULL)); + + /* create key - value structure */ + key_value* kv = malloc(sizeof(key_value) * MAX_KEYS); //returns void pointer. + if (kv == NULL){ + perror("Malloc failed"); + exit(EXIT_FAILURE); + } + + //read from file + FILE* file; + char* buf = (char*)malloc(MAX_MALLOC_VALUE * sizeof(char)); + + char* tmp = (char*)malloc(MAX_MALLOC_VALUE * sizeof(char)); + char* tmp_value = (char*)malloc(MAX_MALLOC_VALUE * sizeof(char)); + + int number_of_items = 0; + + file = fopen("plu.csv", "r"); + if(!file){ + printf("Cannot open file\n"); + exit(EXIT_FAILURE); + } + + while(fgets(buf, MAX_MALLOC_VALUE, file) != NULL){ + if ((strlen(buf)>0) && (buf[strlen (buf) - 1] == '\n')) + buf[strlen (buf) - 1] = '\0'; + + tmp = strtok(buf, ","); + tmp_value = strdup(tmp); + + 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 + + tmp = strtok(NULL, ","); + kv[number_of_items].key = atoi(tmp); + //printf("KEY-->\ttmp[%d]: %s\n", size, tmp); //debug + + ++number_of_items; + + } + 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; + + 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); + + if (fgets(line, sizeof(line), stdin)) { + if (sscanf(line, "%d", &input) == 1) { + if(input == code_to_guess){ + printf("Correct!\n"); + } else { + printf("Nope! It's: %d\n", code_to_guess); + } + } + else { + printf("AWW WHAT DID YOU DOOOOO"); + exit(EXIT_FAILURE); + } + } + } + + /* free memory! */ + free(kv); + free(tmp); + free(tmp_value); + return EXIT_SUCCESS; +} + +void print_key_values(key_value* kv, const int MAX){ + printf("| %20s | %4s |\n", "Value", "Key"); + printf("| -------------------- | ---- |\n"); + for(int i = 0; i < MAX; i++){ + printf("| %20s | %4d |\n", kv[i].value, kv[i].key); + } +} + +int find_key_index(key_value* kv, const char* STRING, const int MAX){ + int i = 0; //index + while(strcmp(STRING, kv[i].value)){ + ++i; + if(i > MAX){ + i = -1; + break; + } + } + return i; +} + + diff --git a/plu b/plu new file mode 100755 index 0000000..b946b06 Binary files /dev/null and b/plu differ diff --git a/plu.csv b/plu.csv new file mode 100644 index 0000000..4836aa1 --- /dev/null +++ b/plu.csv @@ -0,0 +1,8 @@ +Carrots,342 +Limes,11 +Cabbage,64 +Apple,26 +Sweet Potatoe,24 +Spring Onion,317 +Croissant,70 +Choc Croissant,170