From 204bf6c3c9a8c5d160a02d64ef463fa8beba78c0 Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Sun, 12 Jan 2025 19:50:11 -0500 Subject: [PATCH] Add words and their synonyms --- .gitignore | 2 +- 01_text_adventure/Makefile | 4 ++-- 01_text_adventure/README.org | 4 ++-- 01_text_adventure/data/words.txt | 2 ++ 01_text_adventure/game.c | 3 +++ 01_text_adventure/game.h | 2 ++ 01_text_adventure/main.c | 1 + 01_text_adventure/word.c | 27 +++++++++++++++++++++++++++ 01_text_adventure/word.h | 21 +++++++++++++++++++++ 9 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 01_text_adventure/data/words.txt create mode 100644 01_text_adventure/word.c create mode 100644 01_text_adventure/word.h diff --git a/.gitignore b/.gitignore index a11aa7f..b34e3e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ raylib.h game -data/*.c \ No newline at end of file +*/data/*.c \ No newline at end of file diff --git a/01_text_adventure/Makefile b/01_text_adventure/Makefile index 59a60bc..5bd52b0 100644 --- a/01_text_adventure/Makefile +++ b/01_text_adventure/Makefile @@ -3,10 +3,10 @@ CFLAGS=-Wall -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 .PHONY: clean run -game: rooms.data transitions.data *.c +game: data/rooms.c data/transitions.c data/words.c *.c $(CC) *.c $(CFLAGS) -o game -%.data: data/%.txt +data/%.c: data/%.txt echo -n "char *data_$*_txt = \"" > data/$*.c cat data/$*.txt | perl -pe 's/\n/\\n/g' >> data/$*.c echo "\";" >> data/$*.c diff --git a/01_text_adventure/README.org b/01_text_adventure/README.org index 21185ef..7be4bb3 100644 --- a/01_text_adventure/README.org +++ b/01_text_adventure/README.org @@ -70,7 +70,7 @@ NORTH | IN(other_room) | 2 | You enter the opulent room. | GOTO(opulent_room I'm sure we want a gallery of synonyms for most verbs and nouns. I assume we split the string by spaces, making an array of canonical words, then write the actions around those canonical words. -PULL | PULL,YANK,TUG -ROPE | ROPE,CORD,STRING,CABLE +PULL|PULL,YANK,TUG +ROPE|ROPE,CORD,STRING,CABLE With the above words, "PULL ROPE", "YANK CORD", or "TUG STRING" would all check for actions as "PULL ROPE". diff --git a/01_text_adventure/data/words.txt b/01_text_adventure/data/words.txt new file mode 100644 index 0000000..f3dd89d --- /dev/null +++ b/01_text_adventure/data/words.txt @@ -0,0 +1,2 @@ +PULL|PULL,YANK,TUG +ROPE|ROPE,CORD,STRING,CABLE diff --git a/01_text_adventure/game.c b/01_text_adventure/game.c index e85e48a..68fdcf1 100644 --- a/01_text_adventure/game.c +++ b/01_text_adventure/game.c @@ -30,6 +30,9 @@ Game *game_create(void) { g->input = input; + g->words = malloc(sizeof(Words)); + g->words->count = 0; + return g; } diff --git a/01_text_adventure/game.h b/01_text_adventure/game.h index cbfb9a4..648c7f1 100644 --- a/01_text_adventure/game.h +++ b/01_text_adventure/game.h @@ -18,6 +18,7 @@ typedef enum Command { #include "room.h" #include "transition.h" #include "log.h" +#include "word.h" struct Game { bool should_close; @@ -26,6 +27,7 @@ struct Game { Rooms *rooms; Room *current_room; Transitions *transitions; + Words *words; }; Game *game_create(void); diff --git a/01_text_adventure/main.c b/01_text_adventure/main.c index 55560d5..b607577 100644 --- a/01_text_adventure/main.c +++ b/01_text_adventure/main.c @@ -17,6 +17,7 @@ int main(void) { game_load_rooms(g); g->current_room = &g->rooms->rooms[0]; game_load_transitions(g); + game_load_words(g); game_run_until_close(g); CloseWindow(); diff --git a/01_text_adventure/word.c b/01_text_adventure/word.c new file mode 100644 index 0000000..26f39a4 --- /dev/null +++ b/01_text_adventure/word.c @@ -0,0 +1,27 @@ +#include +#include +#include +#include "word.h" +#include "game.h" +#include "util.h" + +void load_word(Game *g, char *line) { + char *token = strtok(line, "|"); + Word *word = &g->words->words[g->words->count]; + word->word = malloc(strlen(token) + 1); + strcpy(word->word, token); + + word->synonyms_count = 0; + while ((token = strtok(NULL, ",")) != NULL) { + word->synonyms[word->synonyms_count] = malloc(strlen(token) + 1); + strcpy(word->synonyms[word->synonyms_count], token); + word->synonyms_count++; + } + + g->words->count++; +} + +#include "data/words.c" +void game_load_words(Game *g) { + parse_multiline_string(g, data_words_txt, &load_word); +} diff --git a/01_text_adventure/word.h b/01_text_adventure/word.h new file mode 100644 index 0000000..111d1ae --- /dev/null +++ b/01_text_adventure/word.h @@ -0,0 +1,21 @@ +#ifndef _FD_WORD_ +#define _FD_WORD_ +typedef struct Word Word; +typedef struct Words Words; + +#include "game.h" + +struct Word { + char *word; + char* synonyms[100]; + int synonyms_count; +}; + +struct Words { + Word words[200]; + int count; +}; + +void game_load_words(Game *g); + +#endif