parent
f9f5c4bcd1
commit
8cbf03e493
@ -0,0 +1,6 @@ |
||||
#!/bin/bash |
||||
|
||||
set -xe |
||||
CFLAGS="" |
||||
|
||||
gcc $CFLAGS -o plu main.c |
@ -0,0 +1,133 @@ |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <time.h> |
||||
#include <math.h> |
||||
#include <string.h> |
||||
#include <stdbool.h> |
||||
|
||||
#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; |
||||
} |
||||
|
||||
|
Loading…
Reference in new issue