I think i am almost there i have a few glitches when i run it. it will not accept the commands i input im not too sure what it is tho, any help would be fantastic. thanks so much for all your help. i feel like i have learnt a lot this is all my code:
Code:
// Required System Files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Global Definitions
#define NORTH 0
#define EAST 1
#define SOUTH 2
#define WEST 3
// Global Structure Type Definition
typedef struct { // Define new Structure Type
char description[200]; // Use upto 200 characters for description string
int Exit[4]; // Use 4 possible different values for Exit array
} Location; // Call this structure type: Location
// Global Location Type Variable Definition
Location Room[14]; // Create 13 Location type Global structures called Room’s
// function prototypes
void setupRooms(void);
void describeRoom(int roomNumber);
void describeExits(int roomNumber);
int getDirection(int currentRoom);
//SETUP ROOM FUNCTION
void setupRooms() {
// MY ROOM 1 DEFINITION
strcpy(Room[1].description, "Entrance");
Room[1].Exit[NORTH] = 2; // Going north takes you to Room 2
Room[1].Exit[EAST] = 0; // Use zero to indicate no exit East
Room[1].Exit[SOUTH] = 0;
Room[1].Exit[WEST] = 0;
// MY ROOM 2 DEFINITION
strcpy(Room[2].description, "Lower South Corridor");
Room[2].Exit[NORTH] = 4;
Room[2].Exit[EAST] = 3;
Room[2].Exit[SOUTH] = 1;
Room[2].Exit[WEST] = 0;
// MY ROOM 3 DEFINITION
strcpy(Room[3].description, "Living Room");
Room[3].Exit[NORTH] = 0;
Room[3].Exit[EAST] = 0;
Room[3].Exit[SOUTH] = 0;
Room[3].Exit[WEST] = 2;
// MY ROOM 4 DEFINITION
strcpy(Room[4].description, "Lower North Corridor");
Room[4].Exit[NORTH] = 6;
Room[4].Exit[EAST] = 5;
Room[4].Exit[SOUTH] = 2;
Room[4].Exit[WEST] = 0;
// MY ROOM 5 DEFINITION
strcpy(Room[5].description, "Stairs");
Room[5].Exit[NORTH] = 0;
Room[5].Exit[EAST] = 8;
Room[5].Exit[SOUTH] = 0;
Room[5].Exit[WEST] = 4;
// MY ROOM 6 DEFINITION
strcpy(Room[6].description, "Kitchen");
Room[6].Exit[NORTH] = 7;
Room[6].Exit[EAST] = 0;
Room[6].Exit[SOUTH] = 4;
Room[6].Exit[WEST] = 0;
// MY ROOM 7 DEFINITION
strcpy(Room[7].description, "Garden");
Room[7].Exit[NORTH] = 0;
Room[7].Exit[EAST] = 0;
Room[7].Exit[SOUTH] = 6;
Room[7].Exit[WEST] = 0;
// MY ROOM 8 DEFINITION
strcpy(Room[8].description, "Upper North corridor");
Room[8].Exit[NORTH] = 9;
Room[8].Exit[EAST] = 0;
Room[8].Exit[SOUTH] = 10;
Room[8].Exit[WEST] = 5;
// MY ROOM 9 DEFINITION
strcpy(Room[9].description, "Main Bedroom");
Room[9].Exit[NORTH] = 0;
Room[9].Exit[EAST] = 0;
Room[9].Exit[SOUTH] = 8;
Room[9].Exit[WEST] = 0;
// MY ROOM 7 DEFINITION
strcpy(Room[10].description, "Upper South Corridor");
Room[10].Exit[NORTH] = 8;
Room[10].Exit[EAST] = 11;
Room[10].Exit[SOUTH] = 12;
Room[10].Exit[WEST] = 13;
// MY ROOM 11 DEFINITION
strcpy(Room[11].description, "Bathroom");
Room[11].Exit[NORTH] = 0;
Room[11].Exit[EAST] = 0;
Room[11].Exit[SOUTH] = 0;
Room[11].Exit[WEST] = 10;
// MY ROOM 12 DEFINITION
strcpy(Room[12].description, "Guest Room");
Room[12].Exit[NORTH] = 10;
Room[12].Exit[EAST] = 0;
Room[12].Exit[SOUTH] = 0;
Room[12].Exit[WEST] = 0;
// MY ROOM 13 DEFINITION
strcpy(Room[13].description, "Kids Bedroom");
Room[13].Exit[NORTH] = 0;
Room[13].Exit[EAST] = 10;
Room[13].Exit[SOUTH] = 0;
Room[13].Exit[WEST] = 0;
}
//DESCRIBE ROOM FUNCTION
void describeRoom (int roomNumber) {
printf("You are in: %s\n", Room[roomNumber].description);
}
//DESCRIBE EXITS FUNCTION
void describeExits(int roomNumber) {
printf("\nExits available are to the");
if ( Room[roomNumber].Exit[NORTH] > 0 )
{
printf("\nNorth");
}
if ( Room[roomNumber].Exit[EAST] > 0 )
{
printf("\nEast");
}
if ( Room[roomNumber].Exit[SOUTH] > 0 )
{
printf("\nSouth");
}
if ( Room[roomNumber].Exit[WEST] > 0 )
{
printf("\nWest");
}
}
////GET DIRECTION FUNCTION
int getDirection(int currentRoom){
printf("\nType the letter of the direction you wish to go in \nOr type q to quit");
char direction='q';
scanf("%c", &direction);
switch(direction){
case 'n':
case 'N':
if(Room[currentRoom].Exit[NORTH] > 0)
{
getchar();
return Room[currentRoom].Exit[NORTH];
}
break;
case 's':
case 'S':
if(Room[currentRoom].Exit[SOUTH] > 0)
{
getchar();
return Room[currentRoom].Exit[SOUTH];
}
break;
case 'e':
case 'E':
if(Room[currentRoom].Exit[EAST] > 0)
{
getchar();
return Room[currentRoom].Exit[EAST];
}
break;
case 'w':
case 'W':
if(Room[currentRoom].Exit[WEST] > 0)
{
getchar();
return Room[currentRoom].Exit[WEST];
}
break;
case 'q':
case 'Q':
getchar();
return -1;
break;
default:
printf("\nInvalid key, please only use n,e,s,w or q");
break;
}
getchar();
return -2;
}
// MAIN FUNCTION
int main(void) {
int roomNumber = 1; // Setup starting room number as 1
int direction = 0;
setupRooms(); // Setup Room structures
while (1) {
system("cls"); // Clear Screen
if (direction == -2)
{
printf("\nYou cannot go that way");
}
describeRoom(roomNumber); // Describe current Room
describeExits(roomNumber); // Describe current Room Exits
direction = getDirection(roomNumber); // Get direction to go in
if (direction == -1 ) {
break; // Exit prog if error is returned
}
if (direction >= 0) {
roomNumber = direction; // Set current Room number
}
}
return 0;
}
thanks again for all the help you have given