Get a bit nicer parsing going
This commit is contained in:
		
							parent
							
								
									204bf6c3c9
								
							
						
					
					
						commit
						842a6852cd
					
				| @ -2,6 +2,7 @@ | ||||
| #include "../raylib.h" | ||||
| #include "log.h" | ||||
| #include "input.h" | ||||
| #include "parse.h" | ||||
| 
 | ||||
| #include <stdlib.h> | ||||
| #include <stdio.h> | ||||
| @ -19,6 +20,10 @@ int main(void) { | ||||
|     game_load_transitions(g); | ||||
|     game_load_words(g); | ||||
| 
 | ||||
|     parse(g, "PULL ROPE"); | ||||
|     parse(g, "YANK CORD"); | ||||
|     parse(g, "TUG STRING"); | ||||
| 
 | ||||
|     game_run_until_close(g); | ||||
|     CloseWindow(); | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										30
									
								
								01_text_adventure/parse.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								01_text_adventure/parse.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,30 @@ | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| #include "game.h" | ||||
| #include "word.h" | ||||
| 
 | ||||
| #define MAX_WORDS_IN_COMMAND 4 | ||||
| void parse(Game *g, char *typed_command) { | ||||
|   printf("Typed command: %s\n", typed_command); | ||||
|   char *bluh = malloc(strlen(typed_command) + 1); | ||||
|   strcpy(bluh, typed_command); | ||||
| 
 | ||||
|   Word **command = malloc(MAX_WORDS_IN_COMMAND * sizeof(Word)); | ||||
|   int word_count = 0; | ||||
|   char *token = strtok(bluh, " "); | ||||
| 
 | ||||
|   while (word_count < MAX_WORDS_IN_COMMAND && token != NULL) { | ||||
|     command[word_count] = find_word(g->words, token); | ||||
| 
 | ||||
|     word_count++; | ||||
|     token = strtok(NULL, " "); | ||||
|   } | ||||
| 
 | ||||
|   for(int i = 0; i < word_count; i++) { | ||||
|     printf("%s ", command[i]->word); | ||||
|   } | ||||
|   printf("\n"); | ||||
| 
 | ||||
|   free(bluh); | ||||
| } | ||||
							
								
								
									
										7
									
								
								01_text_adventure/parse.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								01_text_adventure/parse.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,7 @@ | ||||
| #ifndef _FD_PARSE_ | ||||
| #define _FD_PARSE_ | ||||
| #include "game.h" | ||||
| 
 | ||||
| void parse(Game *g, char *typed_command); | ||||
| 
 | ||||
| #endif | ||||
| @ -25,3 +25,13 @@ void load_word(Game *g, char *line) { | ||||
| void game_load_words(Game *g) { | ||||
|   parse_multiline_string(g, data_words_txt, &load_word); | ||||
| } | ||||
| 
 | ||||
| Word *find_word(Words *words, char *word_or_syn) { | ||||
|   for (int i = 0; i < words->count; i++) { | ||||
|     for (int j = 0; j < words->words[i].synonyms_count; j++) { | ||||
|       if (strcmp(words->words[i].synonyms[j], word_or_syn) == 0) return &words->words[i]; | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   return NULL; | ||||
| } | ||||
|  | ||||
| @ -17,5 +17,6 @@ struct Words { | ||||
| }; | ||||
| 
 | ||||
| void game_load_words(Game *g); | ||||
| Word *find_word(Words *words, char *word); | ||||
| 
 | ||||
| #endif | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user