Code:
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
#define North 0
#define East 1
#define South 2
#define West 3
typedef struct
{
char Description[200];
unsigned int Exit[4];
enum {
NO_EXIT=-1,
R_ENTRANCE=0,
R_LIVING=1,
R_GARAGE=2,
R_DINING=3,
R_SPARE=4,
R_KITCHEN=5,
R_PANTRY=6,
R_PATIO=7,
};
} Location;
Location Room[0];
void RoomSetup(void)
{
strcpy(Room[R_ENTRANCE].Description, "Entrance Corridor");
Room[R_ENTRANCE].Exit[North]=1;
Room[R_ENTRANCE].Exit[East]=2;
Room[R_ENTRANCE].Exit[South]=-1;
Room[R_ENTRANCE].Exit[West]=-1;
strcpy(Room[R_LIVING].Description, "Living Room");
Room[R_LIVING].Exit[North]=3;
Room[R_LIVING].Exit[East]=-1;
Room[R_LIVING].Exit[South]=-1;
Room[R_LIVING].Exit[West]=0;
strcpy(Room[R_GARAGE].Description, "Garage");
Room[R_GARAGE].Exit[North]=4;
Room[R_GARAGE].Exit[East]=0;
Room[R_GARAGE].Exit[South]=-1;
Room[R_GARAGE].Exit[West]=-1;
strcpy(Room[R_DINING].Description, "Dining Room");
Room[R_DINING].Exit[North]=6;
Room[R_DINING].Exit[East]=-1;
Room[R_DINING].Exit[South]=1;
Room[R_DINING].Exit[West]=-1;
strcpy(Room[R_SPARE].Description, "Spare Room");
Room[R_SPARE].Exit[North]=-1;
Room[R_SPARE].Exit[East]=-1;
Room[R_SPARE].Exit[South]=2;
Room[R_SPARE].Exit[West]=-1;
strcpy(Room[R_KITCHEN].Description, "Kitchen");
Room[R_KITCHEN].Exit[North]=7;
Room[R_KITCHEN].Exit[East]=6;
Room[R_KITCHEN].Exit[South]=0;
Room[R_KITCHEN].Exit[West]=-1;
strcpy(Room[R_PANTRY].Description, "PANTRY");
Room[R_PANTRY].Exit[North]=-1;
Room[R_PANTRY].Exit[East]=-1;
Room[R_PANTRY].Exit[South]=3;
Room[R_PANTRY].Exit[West]=5;
strcpy(Room[R_PATIO].Description, "PATIO");
Room[R_PATIO].Exit[North]=-1;
Room[R_PATIO].Exit[East]=-1;
Room[R_PATIO].Exit[South]=5;
Room[R_PATIO].Exit[West]=-1;
}
void DescribeRoom(unsigned int RoomNumber)
{
printf("Welcome to the home of James Ward's Family.\n\n");
printf("You are currently in the %s. ", Room[RoomNumber].Description);
}
void DescribeExits(int RoomNumber)
{
unsigned int exitdir;
unsigned int exitcount = 0;
for (exitdir = 0; exitdir < 4; exitdir++)
{
if (Room[RoomNumber].Exit[exitdir] > 0) exitcount++;
}
if (exitcount == 1) printf("This room has %u exit: \n", exitcount);
else printf("This room has %u exits: \n", exitcount);
if (Room[RoomNumber].Exit[0] > 0) printf(" North: %s\n", Room[Room[RoomNumber].Exit[0]].Description);
if (Room[RoomNumber].Exit[1] > 0) printf(" East: %s\n", Room[Room[RoomNumber].Exit[1]].Description);
if (Room[RoomNumber].Exit[2] > 0) printf(" South: %s\n", Room[Room[RoomNumber].Exit[2]].Description);
if (Room[RoomNumber].Exit[3] > 0) printf(" West: %s\n", Room[Room[RoomNumber].Exit[3]].Description);
printf("\n");
}
void GetDirection(char Direction[])
{
printf("Type the first letter of the direction you want to go in, or type q to quit\n");
scanf("%s", Direction);
while(strlen(Direction) > 1)
{
printf("You may only type ONE letter - please try again\n");
scanf("%s", Direction);
}
}
int main(void)
{
char Direction[1];
char DummyInput[1];
char ExitName[5];
unsigned int RoomNumber = 0;
unsigned int NewRoomNumber, i, CheckExit, ValidExit;
RoomSetup();
while(1){
system("cls"); //Clear Screen
DescribeRoom(RoomNumber); //Describe current Room
DescribeExits(RoomNumber); //Describe current Room Exits
GetDirection(Direction); //Get direction to go in
if (strcmp(Direction, "Q") == 0 || strcmp(Direction, "q") == 0) exit(0);
ValidExit = 1;
if (strcmp(Direction, "N") == 0 || strcmp(Direction, "n") == 0)
{
CheckExit = 0;
strcpy(ExitName, "North");
}
else if (strcmp(Direction, "E") == 0 || strcmp(Direction, "e") == 0)
{
CheckExit = 1;
strcpy(ExitName, "East");
}
else if (strcmp(Direction, "S") == 0 || strcmp(Direction, "s") == 0)
{
CheckExit = 2;
strcpy(ExitName, "South");
}
else if (strcmp(Direction, "W") == 0 || strcmp(Direction, "w") == 0)
{
CheckExit = 3;
strcpy(ExitName, "West");
}
else
{
ValidExit = 0;
printf("'%s' is not a valid direction - please press any key and 'Enter' to continue\n", Direction);
scanf("%s", DummyInput);
}
if (ValidExit == 1)
{
if (Room[RoomNumber].Exit[CheckExit] > 0) RoomNumber = Room[RoomNumber].Exit[CheckExit];
else
{
printf("There is no exit in the %s - please press any key and 'Enter' to continue\n", ExitName);
scanf("%s", DummyInput);
}
}
}
return 0;
}