summaryrefslogtreecommitdiff
path: root/texad.c
blob: 91f75ba6ae7a092eff702a8ce66ac664a5925315 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "texad.h"

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];

    unsigned int i;

    w = malloc(sizeof *w);
    p = malloc(sizeof *p);

    for (i = 0; i < sizeof r / sizeof *r; i++)
        r[i] = malloc(sizeof *r);
    for (i = 0; i < sizeof d / sizeof *d; i++)
        d[i] = malloc(sizeof *d);

    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]->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 *ds[][3] = {
        {d[0], NULL, NULL},
        {d[1], d[2], NULL},
        {d[3], NULL, NULL}
    };

    for (i = 0; i < sizeof r / sizeof *r; i++) {
        r[i]->doors = malloc(sizeof ds[i]);
        memcpy(r[i]->doors, ds[i], sizeof ds[i]);
    }

    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)
            continue;
        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 *commands; 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;
}