Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char Name[70];
char Description1[70]; //gives the first line of descriptions, up to 70 characters
char Description2[70];
char Description3[70];
int Exit[4]; //makes a possible of 4 different exits (N/S/E/W)
}Room;
Room House[9]; //there are 9 rooms in the house
void DescribeRoom (int RoomNumber)
{
// Show Current Room Name
printf("ROOM:\n %s\n", House[RoomNumber].Name);
// Show Current Room Description
printf("A BRIEF DESCRIPTION:\n %s\n %s\n %s\n", House[RoomNumber].Description1, House[RoomNumber].Description2, House[RoomNumber].Description3);
// Show available exists for current room
printf("AVAILABLE DOOORS:\n");
if (House[RoomNumber].Exit[0] != -1)
{
//using printf to print the room number onto the screen
printf(" North to %s\n", House[House[RoomNumber].Exit[0]].Name);
}
if (House[RoomNumber].Exit[1] != -1)
{
printf(" South to %s\n", House[House[RoomNumber].Exit[1]].Name);
}
if (House[RoomNumber].Exit[2] != -1)
{
printf(" East to %s\n", House[House[RoomNumber].Exit[2]].Name);
}
if (House[RoomNumber].Exit[3] != -1)
{
printf(" West to %s\n", House[House[RoomNumber].Exit[3]].Name);
}
}
int MoveRoom (int RoomNumber)
{
char Direction[2];
char NotUsed;
int ExitNumber;
// Give the user a choice of direction to move
printf("\nChoose a direction (N, S, E or W) or press esc to leave:\n");
scanf ("%s", &Direction); //scanf to see what direction user has inputted
// Check to see if exit number has been pressed (0)
if(strcmp(Direction, "0") == 0)
{
printf("Come back whenever. We'll put the kettle on.");
exit(0);
}
// Converting direction choice in the exit number
if (strcmp(Direction, "n") == 0 || strcmp(Direction, "N") == 0)
{
ExitNumber = 0;
}
else if (strcmp(Direction, "s") == 0 || strcmp(Direction, "S") == 0)
{
ExitNumber = 1;
}
else if (strcmp(Direction, "e") == 0 || strcmp(Direction, "E") == 0)
{
ExitNumber = 2;
}
else if (strcmp(Direction, "w") == 0 || strcmp(Direction, "W") == 0)
{
ExitNumber = 3;
}
// If valid change Room direction has been input Describe new Room
if (House[RoomNumber].Exit[ExitNumber] == -1)
{
//error code,if no exit avalible
printf("\You just walked into a wall. Try the door.\n");
system("hold for just one moment please");
return RoomNumber;
}
else
{
return House[RoomNumber].Exit[ExitNumber];
}
}
//room names and discriptions
void SetupRooms ()
{
int Location = 1;
// Setup Room; West Hallway
strcpy(House[1].Name, "West Hallway");
strcpy(House[1].Description1, "Welcome to the house");
strcpy(House[1].Description2, "You're stood in the Hallway.");
strcpy(House[1].Description3, "You can leave again if you press 0");
House[1].Exit[0] = 0;
House[1].Exit[1] = 3;
House[1].Exit[2] = 2;
House[1].Exit[3] = 8;
// Setup Room; Office
strcpy(House[2].Name, "Office");
strcpy(House[2].Description1, "This is where the work is done");
strcpy(House[2].Description2, "It's also the technical hub of the house, with the printer and wireless router");
House[2].Exit[0] = 0;
House[2].Exit[1] = 0;
House[2].Exit[2] = 0;
House[2].Exit[3] = 1;
// Setup Room; Downstairs Toilet
strcpy(House[3].Name, "Downstairs Toilet");
strcpy(House[3].Description1, "Not an overly exciting room; there's a toilet and a sink");
strcpy(House[3].Description2, "There's also an 'Encyclopedia of Everything' on the windowsill");
House[3].Exit[0] = 1;
House[3].Exit[1] = 0;
House[3].Exit[2] = 0;
House[3].Exit[3] = 0;
// Setup Room; Kitchen & Dining Room
strcpy(House[4].Name, "Kitchen & Dining Room");
strcpy(House[4].Description1, "Welcome to the heart of the home");
strcpy(House[4].Description2, "Large open-plan room seperated by a kitchen counter");
strcpy(House[4].Description3, "My baby brother's playpen is in the corner, by the dining table");
House[4].Exit[0] = 0;
House[4].Exit[1] = 8;
House[4].Exit[2] = 9;
House[4].Exit[3] = 0;
// Setup Room; Bathroom
strcpy(House[5].Name, "Bathroom");
strcpy(House[5].Description1, "You are now in the Bathroom");
strcpy(House[5].Description2, "A really nice powerful shower is in the corner, with the bath itself directly in front of you");
House[5].Exit[0] = 8;
House[5].Exit[1] = 0;
House[5].Exit[2] = 0;
House[5].Exit[3] = 0;
// Setup Room; Small Living Room
strcpy(House[6].Name, "Small Living room");
strcpy(House[6].Description1, "Now you're in the original living room");
strcpy(House[6].Description2, "This was the main living room before we had the extension");
strcpy(House[6].Description3, "Now the fireplace has been knocked through on both sides, and you can see into the Large living room through the fire =- it's awesome");
House[6].Exit[0] = 8;
House[6].Exit[1] = 7;
House[6].Exit[2] = 0;
House[6].Exit[3] = 0;
// Setup Room; Big Living Room
strcpy(House[7].Name, "Big Living Room");
strcpy(House[7].Description1, "You've left the Small Living room, and now you're in the Big Living room");
strcpy(House[7].Description2, "This is where the TV lives, as well as the comfiest sofa in the known Universe");
strcpy(House[7].Description3, "A huge floor-to-ceiling bookcase covers one wall, rammed full of books, CDs and DVDs");
House[7].Exit[0] = 6;
House[7].Exit[1] = 0;
House[7].Exit[2] = 0;
House[7].Exit[3] = 0;
// Setup Room; East Hallway
strcpy(House[8].Name, "East Hallway");
strcpy(House[8].Description1, "Now you're in the East Hallway");
strcpy(House[8].Description2, "It forms the second part of an internal ring around the house");
strcpy(House[8].Description3, "The 8-year old cousins love to do laps of it");
House[8].Exit[0] = 5;
House[8].Exit[1] = 6;
House[8].Exit[2] = 0;
House[8].Exit[3] = 1;
// Setup Room; Utility Room
strcpy(House[8].Name, "Utility Room");
strcpy(House[8].Description1, "Welcome to the Utility room");
strcpy(House[8].Description2, "Originally a cow shed, this is where the booze fridge and the washing machine live");
strcpy(House[8].Description3, "It is entirely sepearte from the main house, but an important part nonetheless");
House[8].Exit[0] = 0;
House[8].Exit[1] = 0;
House[8].Exit[2] = 0;
House[8].Exit[3] = 5;
while(1)
{
// Clear Screen
system("cls");
// Discription of current room
DescribeRoom(Location);
// Go to a new room
Location = MoveRoom(Location);
}
}
void main ()
{
system("cls");
SetupRooms();
}
--------------------------------------------------