summaryrefslogtreecommitdiff
path: root/texad.c
blob: eabd59eef5109757f588c067b35a55bc6a126529 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define INPUT_LIMIT 256
#define PROMPT "> "

enum action {
    UNKNOWN,
    LOOK,
    GO_NORTH,
    GO_SOUTH,
    GO_EAST,
    GO_WEST,
    QUIT
};

enum direction {
    NORTH,
    SOUTH,
    EAST,
    WEST
};

struct room {
    char *title;
    char *description;
    struct door **doors;
};

struct door {
    enum direction direction;
    struct room *src;
    struct room *dst;
};

struct world {
    struct player *player;
};

struct player {
    char *name;
    struct room *room;
};

struct command {
    enum action action;
    char *string;
};

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 struct command commands[] = {
    {LOOK     , "l" },
    {GO_NORTH , "n" },
    {GO_SOUTH , "s" },
    {GO_EAST  , "e" },
    {GO_WEST  , "w" },
    {QUIT     , "q" },
    {UNKNOWN  , NULL}
};

struct world *genesis(void)
{
    struct world *w;
    struct player *p;
    struct room *r[3];
    struct door *d[4];

    w = (struct world*) malloc(sizeof(struct world));
    p = (struct player*) malloc(sizeof(struct player));
    r[0] = (struct room*) malloc(sizeof(struct room));
    r[1] = (struct room*) malloc(sizeof(struct room));
    r[2] = (struct room*) malloc(sizeof(struct room));

    r[0]->title = "The foo room";
    r[0]->description = "You are in the foo room. Sunlight streams from the windows.";

    r[1]->title = "The bar room";
    r[1]->description = "You are in the bar room. A vague sense of despair washes over you.";

    r[2]->title = "The baz room";
    r[2]->description = "You are in the baz room. It is richly furnished.";

    d[0] = (struct door*) malloc(sizeof(struct door));
    d[1] = (struct door*) malloc(sizeof(struct door));
    d[2] = (struct door*) malloc(sizeof(struct door));
    d[3] = (struct door*) malloc(sizeof(struct door));

    d[0]->direction = EAST;
    d[0]->src = r[0];
    d[0]->dst = r[1];

    d[1]->direction = WEST;
    d[1]->src = r[1];
    d[1]->dst = r[0];

    d[2]->direction = NORTH;
    d[2]->src = r[1];
    d[2]->dst = r[2];

    d[3]->direction = SOUTH;
    d[3]->src = r[2];
    d[3]->dst = r[1];

    struct door *ds1[] = {d[0], NULL};
    struct door *ds2[] = {d[1], d[2], NULL};
    struct door *ds3[] = {d[3], NULL};

    r[0]->doors = ds1;
    r[1]->doors = ds2;
    r[2]->doors = ds3;

    p->room = r[0];

    w->player = p;

    return w;
}

void apocalypse(struct world *w)
{
    free(w->player->room);
    free(w->player);
    free(w);
}

int main(void)
{
    struct world *w;

    w = genesis();
    while (loop(w));
    apocalypse(w);

    exit(EXIT_SUCCESS);
}

void look(struct room *r)
{
    printf("%s: %s\n", r->title, r->description);
    return;
}

void move(struct player *p, enum direction dir)
{
    struct door **ds;
    for (ds = p->room->doors; *ds; ds++)
        if ((*ds)->direction == dir) {
            p->room = (*ds)->dst;
            look(p->room);
            return;
        }
    puts("You can't go that way.");
    return;
}

enum action parse(char *s)
{
    unsigned long i;
    s[strcspn(s, "\n")] = 0;
    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(struct world *w)
{
    char input[INPUT_LIMIT];
    enum action a;

    printf("%s", PROMPT);
    if (fgets(input, INPUT_LIMIT, stdin) != NULL) {
        a = parse(input);

        switch (a) {

            case LOOK:
                look(w->player->room);
                return 1;

            case QUIT:
                return 0;

            case GO_NORTH:
                move(w->player, NORTH);
                return 1;

            case GO_SOUTH:
                move(w->player, SOUTH);
                return 1;

            case GO_EAST:
                move(w->player, EAST);
                return 1;

            case GO_WEST:
                move(w->player, WEST);
                return 1;

            case UNKNOWN:
                printf("What is \"%s\"?\n", input);
                return 1;
        }

    } else {
        puts("");
        return 0;
    }
}