diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-12-02 14:42:45 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-12-02 14:42:45 +1300 |
commit | 97b662b680e7724040e7b93f14b3535a7bf8b862 (patch) | |
tree | 73a2fd93ae9debca4ae9056138232510b9fd076b | |
parent | Name the command struct inline (diff) | |
download | texad-97b662b680e7724040e7b93f14b3535a7bf8b862.tar.gz texad-97b662b680e7724040e7b93f14b3535a7bf8b862.zip |
Remove typedefs, be explicit with enum/struct
Helps me keep this clearer in my own head
-rw-r--r-- | texad.c | 98 |
1 files changed, 49 insertions, 49 deletions
@@ -5,7 +5,7 @@ #define INPUT_LIMIT 256 #define PROMPT "> " -typedef enum { +enum action { UNKNOWN, LOOK, GO_NORTH, @@ -13,49 +13,49 @@ typedef enum { GO_EAST, GO_WEST, QUIT -} action; +}; -typedef enum { +enum direction { NORTH, SOUTH, EAST, WEST -} direction; +}; -typedef struct room { +struct room { char *title; char *description; struct door **doors; -} room; +}; -typedef struct door { - direction direction; +struct door { + enum direction direction; struct room *src; struct room *dst; -} door; +}; -typedef struct world { +struct world { struct player *player; -} world; +}; -typedef struct player { +struct player { char *name; struct room *room; -} player; +}; -typedef struct command { - action action; +struct command { + enum action action; char *string; -} command; +}; -world *genesis(void); -void apocalypse(world*); -action parse(char*); -void move(player*, direction); -void look(room*); -int loop(world*); +struct world *genesis(void); +void apocalypse(struct world*); +enum action parse(char*); +void move(struct player*, enum direction); +void look(struct room*); +int loop(struct world*); -static command commands[] = { +static struct command commands[] = { {LOOK , "l" }, {GO_NORTH , "n" }, {GO_SOUTH , "s" }, @@ -65,18 +65,18 @@ static command commands[] = { {UNKNOWN , NULL} }; -world *genesis(void) +struct world *genesis(void) { - world *w; - player *p; - room *r1, *r2, *r3; - door *d12, *d21, *d23, *d32; + struct world *w; + struct player *p; + struct room *r1, *r2, *r3; + struct door *d12, *d21, *d23, *d32; - w = (world*) malloc(sizeof(world)); - p = (player*) malloc(sizeof(player)); - r1 = (room*) malloc(sizeof(room)); - r2 = (room*) malloc(sizeof(room)); - r3 = (room*) malloc(sizeof(room)); + w = (struct world*) malloc(sizeof(struct world)); + p = (struct player*) malloc(sizeof(struct player)); + r1 = (struct room*) malloc(sizeof(struct room)); + r2 = (struct room*) malloc(sizeof(struct room)); + r3 = (struct room*) malloc(sizeof(struct room)); r1->title = "The foo room"; r1->description = "You are in the foo room. Sunlight streams from the windows."; @@ -87,10 +87,10 @@ world *genesis(void) r3->title = "The baz room"; r3->description = "You are in the baz room. It is richly furnished."; - d12 = (door*) malloc(sizeof(door)); - d21 = (door*) malloc(sizeof(door)); - d23 = (door*) malloc(sizeof(door)); - d32 = (door*) malloc(sizeof(door)); + d12 = (struct door*) malloc(sizeof(struct door)); + d21 = (struct door*) malloc(sizeof(struct door)); + d23 = (struct door*) malloc(sizeof(struct door)); + d32 = (struct door*) malloc(sizeof(struct door)); d12->direction = EAST; d12->src = r1; @@ -108,9 +108,9 @@ world *genesis(void) d32->src = r3; d32->dst = r2; - door *ds1[] = {d12, NULL}; - door *ds2[] = {d21, d23, NULL}; - door *ds3[] = {d32, NULL}; + struct door *ds1[] = {d12, NULL}; + struct door *ds2[] = {d21, d23, NULL}; + struct door *ds3[] = {d32, NULL}; r1->doors = ds1; r2->doors = ds2; @@ -123,7 +123,7 @@ world *genesis(void) return w; } -void apocalypse(world *w) +void apocalypse(struct world *w) { free(w->player->room); free(w->player); @@ -132,7 +132,7 @@ void apocalypse(world *w) int main(void) { - world *w; + struct world *w; w = genesis(); while (loop(w)); @@ -141,15 +141,15 @@ int main(void) exit(EXIT_SUCCESS); } -void look(room *r) +void look(struct room *r) { printf("%s: %s\n", r->title, r->description); return; } -void move(player *p, direction dir) +void move(struct player *p, enum direction dir) { - door **ds; + struct door **ds; for (ds = p->room->doors; *ds; ds++) if ((*ds)->direction == dir) { p->room = (*ds)->dst; @@ -160,20 +160,20 @@ void move(player *p, direction dir) return; } -action parse(char *s) +enum action parse(char *s) { unsigned long i; s[strcspn(s, "\n")] = 0; - for (i = 0; i < sizeof(commands) / sizeof(command); i++) + for (i = 0; i < sizeof(commands) / sizeof(struct command); i++) if (commands[i].string && !strcmp(commands[i].string, s)) return commands[i].action; return UNKNOWN; } -int loop(world *w) +int loop(struct world *w) { char input[INPUT_LIMIT]; - action a; + enum action a; printf("%s", PROMPT); if (fgets(input, INPUT_LIMIT, stdin) != NULL) { |