Create predicates

This commit is contained in:
Bill Rossi 2025-01-13 19:56:15 -05:00
parent e8a9caf304
commit 0ff24ad65b
4 changed files with 74 additions and 1 deletions

View File

@ -1,2 +1,2 @@
LEVER_PULLED|0
STEPS_TAKEN|0
STEPS_TAKEN|10

View File

@ -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) {

View 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;
}
}

View 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