Text based adventure game in C
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
4.3 KiB

3 years ago
#include <stdio.h>
#include <string.h>
#define _DEBUGGING 0
#define _NORTH 8
#define _EAST 4
#define _SOUTH 2
#define _WEST 1
#define _FORWARD 8
#define _RIGHT 4
#define _BACKWARD 2
#define _LEFT 1
#define _NEWLINE 10
#define _SPACEBAR 32
/*
adv - text based adventure. See changelog for details and ideas for future work
3 years ago
*/
char *roomName[16] = {"hallen", "ett vardagsrum", "Rum 2", "Rum 3",
"Rum 4", "Rum 5", "Rum 6", "Rum 7",
"Rum 8", "Rum 9", "Rum 10", "Rum 11",
"Rum 12", "Rum 13", "Rum 14", "Rum 15"};
char *roomDesc[16] = { "Det ligger en ful matta på golvet. \nDet luktar ättika..",
"En TV står och brusar bredvid en bokhylla.\nTapeterna på väggen ramlar nästan av."};
short int roomExits[16] = {15,15,15,15, /* Binary information about available exits */
3 years ago
15,15,15,15, /* 1000 - north, 0100 - east, 0010 - south, 0001 - west */
15,15,15,15,
15,15,15,15};
short int commandParts;
char command[5][16];
short int currentRoom;
short int looking; /* Will hold direction in the same format as directions */
3 years ago
int main () {
startup();
while(1) {
enterRoom();
prompt();
parseCommand();
}
}
int startup () {
printf("***************************\n");
printf("* E T T Ä V E N T Y R *\n");
printf("***************************\n\n\n");
printf("Du står utanför ett hus och sparkar in dörren.\nDen går itu och du går in...\n\n");
looking = _NORTH; /* Utgå från rum 13 och titta norrut */
currentRoom = 13;
return 0;
}
int enterRoom () {
printf("- - Du är i %s\n\n", roomName[currentRoom]);
printf("%s \n\n", roomDesc[currentRoom]);
}
int prompt () {
char input[40]; /* Hold user input */
3 years ago
printf("Vad vill du göra?\n");
fgets(input,40,stdin);
printf("\n");
commandParts=0;
int i=0;
int ltr=0; /* Position marker for the word being processed */
3 years ago
while (i<sizeof(input)) {
switch (input[i]) {
case _NEWLINE: /* Remove \n, terminate word with null, stop collecting words */
3 years ago
command[commandParts][ltr] = 0;
i=sizeof(input);
break;
case _SPACEBAR: /* Find spaces, terminate word with null, move on to next word */
3 years ago
command[commandParts][ltr] = 0;
commandParts++;
ltr=0;
break;
default: /* Append letter to current word */
3 years ago
command[commandParts][ltr] = input[i];
ltr++;
break;
}
i++;
}
/* debug stuff */
if(_DEBUGGING){
printf("Hittade %d ord:\n", commandParts+1);
int x=0;
while (x<=commandParts) {
printf("%s\n",command[x]);
x++;
}
}
/* end of debugging stuff */
return 0;
}
/* parseCommand expects the command array to contain something */
/* Look at the first word and call appropriate function */
int parseCommand() {
if (strcmp(command[0], "hjälp") == 0) { /* HJÄLP */
help();
} else if (strcmp(command[0], "") == 0) { /* GÅ */
commandMove();
} else { /* unknown commands */
printf("- - ! HUH? (skriv hjälp för tillgängliga kommandon)\n\n");
}
return 0;
}
/* gå command - depends on the command array */
int commandMove () {
short int direction;
if (strcmp(command[1], "framåt") == 0) {
direction = _FORWARD;
} else if (strcmp(command[1], "bakåt") == 0) {
direction = _BACKWARD;
} else if (strcmp(command[1], "vänster") == 0) {
direction = _LEFT;
} else if (strcmp(command[1], "höger") == 0) {
direction = _RIGHT;
} else {
printf("-- ! HUH? gå framåt / bakåt / vänster / höger\n\n");
return 1;
}
if(canigo(direction)) {
printf("Du går %s...\n\n", command[1]);
switch(direction) {
case _FORWARD:
currentRoom=currentRoom-4;
break;
case _BACKWARD:
currentRoom=currentRoom+4;
break;
case _LEFT:
currentRoom--;
break;
case _RIGHT:
currentRoom++;
break;
}
} else {
printf("Du kan inte gå %s\n\n", command[1]);
return 1;
}
return 0;
}
/* Future use - use this function to check roomExits according to direction of looking */
3 years ago
int canigo(int direction) { /* 0 - forward, 1 - backward, 2 - left, 3 - right */
/* Do a bitwise comparison of rotaded movement and roomExits */
3 years ago
return 1;
}
/* hjälp command */
3 years ago
int help() {
printf("- - ! Tillgängliga kommandon: \ngå (framåt / bakåt / vänster / höger) \n\n- - ! SLUT PÅ HJÄLP\n");
return 0;
}
3 years ago
/* that's all folks */