Create predicates
This commit is contained in:
		
							parent
							
								
									e8a9caf304
								
							
						
					
					
						commit
						0ff24ad65b
					
				| @ -1,2 +1,2 @@ | ||||
| LEVER_PULLED|0 | ||||
| STEPS_TAKEN|0 | ||||
| STEPS_TAKEN|10 | ||||
|  | ||||
| @ -9,6 +9,7 @@ | ||||
| #include "util.h" | ||||
| #include "word.h" | ||||
| #include "flag.h" | ||||
| #include "predicate.h" | ||||
| 
 | ||||
| Game *game_create(void) { | ||||
|   Game *g = malloc(sizeof(Game)); | ||||
| @ -125,6 +126,37 @@ void game_handle_command(Game *g, const char *command) { | ||||
|     free(response); | ||||
|     break; | ||||
|   } | ||||
| 
 | ||||
|   Predicate fr; | ||||
|   fr.type = PREDICATE_IN_ROOM; | ||||
|   fr.argument = "FIRST_ROOM"; | ||||
|   printf("in room FIRST_ROOM: %d\n", predicate_fulfilled(g, &fr)); | ||||
| 
 | ||||
|   Predicate sr; | ||||
|   sr.type = PREDICATE_IN_ROOM; | ||||
|   sr.argument = "SECOND_ROOM"; | ||||
|   printf("in room SECOND_ROOM: %d\n", predicate_fulfilled(g, &sr)); | ||||
| 
 | ||||
|   Predicate lr; | ||||
|   lr.type = PREDICATE_IN_ROOM; | ||||
|   lr.argument = "LAST_ROOM"; | ||||
|   printf("in room LAST_ROOM: %d\n", predicate_fulfilled(g, &lr)); | ||||
| 
 | ||||
|   Predicate tr; | ||||
|   tr.type = PREDICATE_TRUE; | ||||
|   printf("true: %d\n", predicate_fulfilled(g, &tr)); | ||||
| 
 | ||||
|   Predicate lf; | ||||
|   lf.type = PREDICATE_FLAG_ENABLED; | ||||
|   lf.argument = "LEVER_PULLED"; | ||||
|   printf("lever pulled: %d\n", predicate_fulfilled(g, &lf)); | ||||
| 
 | ||||
|   Predicate sf; | ||||
|   sf.type = PREDICATE_FLAG_ENABLED; | ||||
|   sf.argument = "STEPS_TAKEN"; | ||||
|   printf("steps_taken: %d\n", predicate_fulfilled(g, &sf)); | ||||
| 
 | ||||
|   printf("\n"); | ||||
| } | ||||
| 
 | ||||
| void game_handle_input(Game *g) { | ||||
|  | ||||
							
								
								
									
										19
									
								
								01_text_adventure/predicate.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								01_text_adventure/predicate.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,19 @@ | ||||
| #include <stdio.h> | ||||
| #include <string.h> | ||||
| #include "game.h" | ||||
| #include "flag.h" | ||||
| #include "predicate.h" | ||||
| 
 | ||||
| bool predicate_fulfilled(Game *g, Predicate *p) { | ||||
|   switch (p->type) { | ||||
|   case PREDICATE_TRUE: | ||||
|     return true; | ||||
|   case PREDICATE_IN_ROOM: | ||||
|     return strcmp(g->current_room->name, p->argument) == 0; | ||||
|   case PREDICATE_FLAG_ENABLED: | ||||
|     return flag_value(g->flags, p->argument) > 0; | ||||
|   default: | ||||
|     printf("Invalid predicate type\n"); | ||||
|     return false; | ||||
|   } | ||||
| } | ||||
							
								
								
									
										22
									
								
								01_text_adventure/predicate.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								01_text_adventure/predicate.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,22 @@ | ||||
| #ifndef _FD_PREDICATE_ | ||||
| #define _FD_PREDICATE_ | ||||
| 
 | ||||
| typedef struct Predicate Predicate; | ||||
| 
 | ||||
| typedef enum PredicateType { | ||||
|   PREDICATE_TRUE, | ||||
|   PREDICATE_IN_ROOM, | ||||
|   // PREDICATE_HAS_ITEM,
 | ||||
|   PREDICATE_FLAG_ENABLED, | ||||
| } PredicateType; | ||||
| 
 | ||||
| #include "game.h" | ||||
| 
 | ||||
| struct Predicate { | ||||
|   PredicateType type; | ||||
|   char *argument; | ||||
| }; | ||||
| 
 | ||||
| bool predicate_fulfilled(Game *g, Predicate *p); | ||||
| 
 | ||||
| #endif | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user