tweaks n stuff

master
simonkellet 2 years ago
parent d70ef7f26d
commit e758838d54
  1. 1
      .gitignore
  2. 48
      main.c
  3. 1
      plu.csv

1
.gitignore vendored

@ -0,0 +1 @@
plu

@ -4,9 +4,14 @@
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <unistd.h>
#define MAX_KEYS 256 #define MAX_KEYS 256
#define MAX_MALLOC_VALUE 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 { typedef struct key_value {
int key; int key;
@ -47,59 +52,54 @@ int main(int argc, char *argv[]){
buf[strlen (buf) - 1] = '\0'; buf[strlen (buf) - 1] = '\0';
tmp = strtok(buf, ","); 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; kv[number_of_items].value = tmp_value;
strcpy(kv[number_of_items].value, tmp_value); strcpy(kv[number_of_items].value, tmp_value); //copy that value into the kv
//printf("VAL-->\ttmp[%d]: %s\n", size, tmp_value); //debug
tmp = strtok(NULL, ","); tmp = strtok(NULL, ",");
kv[number_of_items].key = atoi(tmp); 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); fclose(file);
free(buf); 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 // MAIN GAME LOOP
char line[256]; char line[256];
int input; int input;
int score = 0;
system("clear");
printf(BOLDYELLOW "CTRL-D to leave the PLU test\n" RESET);
while(1){ while(1){
int random = rand() % number_of_items; //generate a random number with the number of items int random = rand() % number_of_items; //generate a random number with the number of items
char* item_to_guess = kv[random].value; char* item_to_guess = kv[random].value;
int code_to_guess = kv[find_key_index(kv, item_to_guess, number_of_items)].key; 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 (sscanf(line, "%d", &input) == 1) {
if(input == code_to_guess){ if(input == code_to_guess){
printf("Correct!\n"); printf(BOLDGREEN "✓ Correct!\n" RESET);
++score;
} else { } else {
printf("Nope! It's: %d\n", code_to_guess); printf(BOLDRED "Nope! It's: %d\n" RESET, code_to_guess);
} }
} }
else { else {
printf("AWW WHAT DID YOU DOOOOO"); printf(BOLDYELLOW "\nOnly whole number integers can be entered, Sorry!\n" RESET);
exit(EXIT_FAILURE); continue;
} }
sleep(2);
system("clear");
} else {
printf(BOLDYELLOW "\n\nYour score was %d\nBye!" RESET, score);
exit(EXIT_SUCCESS);
} }
} }

@ -5,4 +5,5 @@ Apple,26
Sweet Potatoe,24 Sweet Potatoe,24
Spring Onion,317 Spring Onion,317
Croissant,70 Croissant,70
Large Avacados,365
Choc Croissant,170 Choc Croissant,170

1 Carrots 342
5 Sweet Potatoe 24
6 Spring Onion 317
7 Croissant 70
8 Large Avacados 365
9 Choc Croissant 170
Loading…
Cancel
Save