Hey everybody,
Once again, I find myself humbled and confused. You see, I have a text adventure engine (although at this point its just a map traversal... doesn't end yet :P) and I'm having a few issues with the commands. I can move around the map just fine and I can pick up items inserted into the map as well as look at my inventory. There are a few problems I'm having.
My drop (name) function isn't working. This command should drop (name) from the function.
My look (name) function is also giving me issues. When its just look, it prints out the description of the room.
First, I'll give you a walkthrough of my map file formatting...
0 (room number) Halsell 228 (room name)
You are standing in a room full of computers and comfy chairs with a giant air conditioning unit hanging from the ceiling. While the surroundings are serine enough, you can't help feeling a certain amount of dread. This isn't just a fear that the air conditioning unit is going to fall either. Something in you tells you that this room is regularly used for strange rituals involving torture. You can only wonder what happens here and why there isn't blood all over the place. Your uneasyness makes you want to leave quickly. (description)
1 (number of exits)
east 1 (direction and number of room linked to)
Items are much like the rooms.
0 (item number) Paper (name)
There is something about the piece of paper that tells you to keep it because it will be very important to you in a few moments. It may even keep you from dying... (description)
3 (location/room number)
So here's the actual code that I have.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
struct Room {
char name[30];
char description[1000];
int numLinks;
char linkName[6][10];
int linkTo[6];
};
struct Item {
char name[30];
char description[1000];
int roomNumber;
};
void parseDescription(char *desc) {
char *lastSpace=NULL;
int cnt=0,cnt2=0;
while(*desc!=0) {
if(cnt>=80) {
cnt=cnt2;
if(lastSpace!=NULL) *lastSpace='\n';
lastSpace=NULL;
cnt2=0;
}
if(*desc==' ') {
lastSpace=desc;
cnt2=0;
}
cnt++;
cnt2++;
desc++;
}
}
void readMap(FILE *file,struct Room *map) {
int i=0,j;
char buf[100];
while(fscanf(file,"%d%c",&i,buf)>0) {
fgets(map[i].name,30,file);
fgets(map[i].description,1000,file);
parseDescription(map[i].description);
fscanf(file,"%d",&map[i].numLinks);
for(j=0; j<map[i].numLinks; ++j) {
fscanf(file,"%s %d",map[i].linkName[j],&map[i].linkTo[j]);
}
}
fclose(file);
}
void readItems(FILE *file, struct Item *items) {
int i=0;
char buf[100];
while(fscanf(file, "%d%c", &i, buf)>0){
fgets(items[i].name,30,file);
fgets(items[i].description,1000,file);
parseDescription(items[i].description);
fscanf(file,"%d",&items[i].roomNumber);
}
fclose(file);
}
int parseCommand(char *command,char **ops) {
int ret=0;
while(*command!=0) {
ops[ret]=strsep(&command," \n");
ret++;
}
return ret;
}
void printRoom(struct Room room[],int cur) {
int i;
printf("%s%s",room[cur].name,room[cur].description);
printf("You can go:\n");
for(i=0; i<room[cur].numLinks; ++i) {
printf("%s : %s",room[cur].linkName[i],room[room[cur].linkTo[i]].name);
}
}
int findDirDest(struct Room *room,char *dir) {
int i;
for(i=0; i<room->numLinks; ++i) {
if(strncasecmp(dir,room->linkName[i],strlen(dir))==0) {
return room->linkTo[i];
}
}
return -1;
}
int main(void) {
struct Room map[100];
struct Item items[10];
struct Item inventory[10];
char command[100];
int going=1;
char *ops[20];
int opCnt;
int currentRoom=0;
int i,k=0,l=0, m=0, n=0, p=0;
readItems(fopen("item.t","rt"),items);
readMap(fopen("map.txt","rt"),map);
printRoom(map,currentRoom);
printf("You see the following item(s) in the room:\n");
for(i=0;i<10;++i){
if(items[i].roomNumber==currentRoom){
printf("%s", items[i].name);
}
}
do {
printf("> ");
fgets(command,100,stdin);
opCnt=parseCommand(command,ops);
if(strcasecmp(ops[0],"quit")==0 || strcasecmp(ops[0],"exit")==0) {
going=0;
}
else if(strcasecmp(ops[0],"get")==0){
for( i=0; i<10; i++){
if(items[i].roomNumber==currentRoom || strcasecmp(ops[1],items[i].name)==0){
strncpy(inventory[l].name,items[i].name,30);
strncpy(inventory[l].description,items[i].description,1000);
printf("This item has been added to your inventory:\n%s", inventory[l].name);
items[i].roomNumber=89; /*Just a random room value...*/
++l;
++n;
}
}
if(n==0){
printf("There is no item of that name here...\n");
}
ops[1]=0;
}
else if(strcasecmp(ops[0],"drop")==0){
for(i=0;i<10;i++){
if(strcasecmp(ops[1],inventory[i].name)==0){
printf("You have dropped %s",inventory[i].name);
strcpy(inventory[i].name,inventory[i+1].name);
strcpy(inventory[i].description,inventory[i+1].description);
++p;
}
}
ops[1]=0;
if(p==0){
printf("This item isn't in your inventory...\n");
}
}
else if (strcasecmp(ops[0], "inv")==0){
printf("This is your inventory: \n");
for(i=0; i<10;++i){
if(inventory[i].name!=0){
printf("%s", inventory[i].name);
}
}
}
else if(strcasecmp(ops[0], "look")==0){
if(ops[1]==0){
printRoom(map,currentRoom);
++m;
}
else{
for(i=0;i<10;++i){
if(strcasecmp(ops[1],inventory[i].name)==0){
printf("%s",inventory[i].description);
++m;
}
if(strcasecmp(ops[1],items[i].name)==0){
if(currentRoom==items[i].roomNumber){
printf("%s",items[i].description);
++m;
}
}
}
}
if(m==0){
printf("There must be something odd in the air. There is nothing there...\n");
}
ops[1]=0;
}
else {
int dest=findDirDest(map+currentRoom,ops[0]);
if(dest>=0) {
currentRoom=dest;
printRoom(map,currentRoom);
}
}
printf("You see the following item(s) in the room:\n");
for(i=0;i<10;++i){
if(items[i].roomNumber==currentRoom){
printf("%s", items[i].name);
}
}
} while(going);
return 0;
}
Any and all help would be appreciated, as would any tips on cleaning up the code just a bit... It feels almost as if strcasecmp stops working halfway through my coding...