All pastes #275134 Raw Edit

Miscellany

public text v1 · immutable
·published 2006-12-11 07:13 UTC
rendered paste body
@@main.c
#include <stdio.h>
#include <stdlib.h>
#include "Room.h"
#include "Exit.h"
#include "Actor.h"

int main(int argc, char *argv[])
{
    Room *rooms[2];
    Actor player;
    
    rooms[0] = Room_new(
        "Outside a small cabin",
        "You are in the woods, outside of a small log cabin, which stands in front of you to the north."
    );
    rooms[1] = Room_new(
        "Inside a small cabin",
        "You are inside a small cabin. It is minimally furnished, and doesn't look the least bit comfortable."
    );
    rooms[0]->exits[NORTH] = Exit_new(rooms[1], "You see a small log cabin.");
    rooms[1]->exits[SOUTH] = Exit_new(rooms[0], "You see the door leading back into the woods.");
    
    Actor_set_location(player, *rooms[0]);
    
    Player_attach(player);
    World_run();
}

@@Room.h
#ifndef ROOM
#define ROOM

#include "Exit.h"

typedef struct {
    char* name;
    char* description;
    Exit* exits[6];
} Room;

Room* Room_new(char* name, char* description);
void Room_add_exit(Room* room, int direction, Exit* exit);

#endif

@@Room.c
#include "Room.h"

Room* Room_new(char* name, char* description)
{
     Room* result = malloc(sizeof(Room));
     result->name = name;
     result->description = description;
     return result;
}

void Room_add_exit(Room* room, int direction, Exit* exit)
{
    room->exits[direction] = exit;
}

@@Exit.h
#ifndef EXIT
#define EXIT

#include "Room.h"

#define NORTH 0
#define SOUTH 1
#define EAST  2
#define WEST  3
#define UP    4
#define DOWN  5

typedef struct {
    char* description;
    Room* destination;
} Exit;

Exit* Exit_new(Room* destination, char* description);

#endif

@@Exit.c
#include "Exit.h"

Exit* Exit_new(Room* destination, char* description)
{
    Exit* result = malloc(sizeof(Exit));
    result->destination = destination;
    result->description = description;
    return result;
}

@@Actor.h
#ifndef ACTOR
#define ACTOR

typedef struct {
    Room *location;
} Actor;

#endif

@@Actor.c
#include "Actor.h"