/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <stdio.h>
#include <string.h>
#include "location.h"
// http://home.hccnet.nl/r.helderman/adventures
static char input[100];
static int getInput()
{
printf("\n--> ");
return fgets(input, sizeof(input), stdin) != NULL;
}
static int parseAndExecute()
{
char *verb = strtok(input, " \n");
char *noun = strtok(NULL, " \n");
if (verb != NULL)
{
if (strcmp(verb, "quit") == 0)
{
return 0;
}
else if (strcmp(verb, "look") == 0)
{
executeLook(noun);
}
else if (strcmp(verb, "go") == 0)
{
executeGo(noun);
}
else if (strcmp(verb, "get") == 0)
{
executeGet(noun);
}
else if (strcmp(verb, "drop") == 0)
{
executeDrop(noun);
}
else if (strcmp(verb, "give") == 0)
{
executeGive(noun);
}
else if (strcmp(verb, "ask") == 0)
{
executeAsk(noun);
}
else if (strcmp(verb, "inventory") == 0)
{
executeInventory();
}
else
{
printf("I don't know how to '%s'.\n", verb);
}
}
return 1;
}
int main()
{
printf("Welcome to Little Cave Adventure.\n");
executeLook("around");
printf("It's very dark in here.\n");
while (getInput() && parseAndExecute());
printf("\nBye!\n");
return 0;
}
extern void executeLook(const char *noun);
extern void executeGo(const char *noun);
#include <stdio.h>
#include <string.h>
#include "object.h"
#include "misc.h"
void executeLook(const char *noun)
{
if (noun != NULL && strcmp(noun, "around") == 0)
{
printf("You are in %s.\n", player->location->description);
listObjectsAtLocation(player->location);
}
else
{
printf("I don't understand what you want to see.\n");
}
}
void executeGo(const char *noun)
{
OBJECT *obj = parseObject(noun);
DISTANCE distance = distanceTo(obj);
if (distance >= distUnknownObject)
{
printf("I don't understand where you want to go.\n");
}
else if (distance == distLocation)
{
printf("You are already there.\n");
}
else if (distance == distOverthere)
{
printf("OK.\n");
player->location = obj;
executeLook("around");
}
else if (distance == distHere && obj->destination != NULL)
{
printf("OK.\n");
player->location = obj->destination;
executeLook("around");
}
else if (distance < distNotHere)
{
printf("You can't get any closer than this.\n");
}
else
{
printf("You don't see any %s here.\n", noun);
}
}
typedef struct object {
const char *description;
const char **tags;
struct object *location;
struct object *destination;
} OBJECT;
extern OBJECT objs[];
#define field (objs + 0)
#define cave (objs + 1)
#define silver (objs + 2)
#define gold (objs + 3)
#define guard (objs + 4)
#define player (objs + 5)
#define intoCave (objs + 6)
#define exitCave (objs + 7)
#define wallField (objs + 8)
#define wallCave (objs + 9)
#define endOfObjs (objs + 10)
#include <stdio.h>
#include "object.h"
static const char *tags0[] = {"field", NULL};
static const char *tags1[] = {"cave", NULL};
static const char *tags2[] = {"silver", "coin", "silver coin", NULL};
static const char *tags3[] = {"gold", "coin", "gold coin", NULL};
static const char *tags4[] = {"guard", "burly guard", NULL};
static const char *tags5[] = {"yourself", NULL};
static const char *tags6[] = {"east", "entrance", NULL};
static const char *tags7[] = {"west", "out", NULL};
static const char *tags8[] = {"west", "north", "south", "forest", NULL};
static const char *tags9[] = {"east", "north", "south", "rock", NULL};
OBJECT objs[] = {
{"an open field" , tags0, NULL , NULL },
{"a little cave" , tags1, NULL , NULL },
{"a silver coin" , tags2, field, NULL },
{"a gold coin" , tags3, cave , NULL },
{"a burly guard" , tags4, field, NULL },
{"yourself" , tags5, field, NULL },
{"a cave entrance to the east", tags6, field, cave },
{"a way out to the west" , tags7, cave , field },
{"dense forest all around" , tags8, field, NULL },
{"solid rock all around" , tags9, cave , NULL }
};
typedef enum {
distPlayer,
distHeld,
distHeldContained,
distLocation,
distHere,
distHereContained,
distOverthere,
distNotHere,
distUnknownObject,
distNoObjectSpecified
} DISTANCE;
extern OBJECT *getPassageTo(OBJECT *targetLocation);
extern DISTANCE distanceTo(OBJECT *obj);
extern OBJECT *parseObject(const char *noun);
extern OBJECT *personHere(void);
extern int listObjectsAtLocation(OBJECT *location);
#include <stdio.h>
#include <string.h>
#include "object.h"
#include "misc.h"
OBJECT *getPassageTo(OBJECT *targetLocation)
{
OBJECT *obj;
for (obj = objs; obj < endOfObjs; obj++)
{
if (obj->location == player->location &&
obj->destination == targetLocation)
{
return obj;
}
}
return NULL;
}
DISTANCE distanceTo(OBJECT *obj)
{
return
obj == NULL ? distUnknownObject :
obj == player ? distPlayer :
obj == player->location ? distLocation :
obj->location == player ? distHeld :
obj->location == player->location ? distHere :
getPassageTo(obj) != NULL ? distOverthere :
obj->location == NULL ? distNotHere :
obj->location->location == player ? distHeldContained :
obj->location->location == player->location ? distHereContained :
distNotHere;
}
static int nounIsInTags(const char *noun, const char **tags)
{
while (*tags != NULL)
{
if (strcmp(noun, *tags++) == 0) return 1;
}
return 0;
}
OBJECT *parseObject(const char *noun)
{
OBJECT *obj, *found = NULL;
for (obj = objs; obj < endOfObjs; obj++)
{
if (noun != NULL && nounIsInTags(noun, obj->tags) &&
distanceTo(obj) < distanceTo(found))
{
found = obj;
}
}
return found;
}
OBJECT *personHere(void)
{
OBJECT *obj;
for (obj = objs; obj < endOfObjs; obj++)
{
if (distanceTo(obj) == distHere && obj == guard)
{
return obj;
}
}
return NULL;
}
int listObjectsAtLocation(OBJECT *location)
{
int count = 0;
OBJECT *obj;
for (obj = objs; obj < endOfObjs; obj++)
{
if (obj != player && obj->location == location)
{
if (count++ == 0)
{
printf("You see:\n");
}
printf("%s\n", obj->description);
}
}
return count;
}
extern void executeGet(const char *noun);
extern void executeDrop(const char *noun);
extern void executeGive(const char *noun);
extern void executeAsk(const char *noun);
extern void executeInventory(void);
#include <stdio.h>
#include "object.h"
#include "misc.h"
static void moveObject(const char *noun, OBJECT *from, OBJECT *to)
{
OBJECT *obj = parseObject(noun);
if (obj == NULL)
{
printf("I don't understand what item you mean.\n");
}
else if (from != obj->location)
{
switch (distanceTo(obj))
{
case distPlayer:
printf("You should not be doing that to yourself.\n");
break;
case distHeld:
printf("You already have %s.\n", obj->description);
break;
case distLocation:
case distOverthere:
printf("That's not an item.\n");
break;
case distHere:
if (from == player)
{
printf("You have no %s.\n", noun);
}
else
{
printf("Sorry, %s has no %s.\n", from->description, noun);
}
break;
case distHeldContained:
case distHereContained:
printf("Sorry, %s is holding it.\n", obj->location->description);
break;
default:
printf("You don't see any %s here.\n", noun);
}
}
else if (to == NULL)
{
printf("There is nobody here to give that to.\n");
}
else
{
obj->location = to;
printf("OK.\n");
}
}
void executeGet(const char *noun)
{
moveObject(noun, player->location, player);
}
void executeDrop(const char *noun)
{
moveObject(noun, player, player->location);
}
void executeGive(const char *noun)
{
moveObject(noun, player, personHere());
}
void executeAsk(const char *noun)
{
moveObject(noun, personHere(), player);
}
void executeInventory(void)
{
if (listObjectsAtLocation(player) == 0)
{
printf("You are empty-handed.\n");
}
}