Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define North 0
#define East 1
#define South 2
#define West 3
#define SIZE 30
int GetDirection (int RoomNumber);
void DescribeRoom (int Fig);
void DescribeExit (int Fig);
void SetupRooms();
typedef struct{
char RoomName[50];
char Description[400];
char Description2[400];
unsigned int Exit[4];
}Location;
Location Room[15];
//Username Function
void get_username( char *user, int size )
{
printf("What is your name?");
fgets(user, size, stdin);
printf("Your username is %s",user);
}
//Main function
void main(){
printf("A few few years ago you had found a book. Nothing special, but it kept his attention for a week or so. These days it was hard to find time to read, but you considered this the only way to cling onto civilisation. The book, in hindsight, was dull. You had no real interest interest in religion and waded through the pages with a sense of irony.");
printf("\n\nYour memory of the past few days was somewhat confused. After so long it became hard to keep track of the passing of time. Events became blurred and actions became instinct. You stood at the top of a road, hands clutching the straps of your backpack, feet echoing that recognisable ache.\n\n\n\n");
char user[SIZE];
get_username( user, SIZE );
unsigned int RoomNumber = 0;
unsigned int Direction;
SetupRooms();
while(1){
system("cls");
DescribeRoom(RoomNumber);
DescribeExit(RoomNumber);
Direction = GetDirection(RoomNumber);
if(Direction==99)exit(0);
if(Direction>0)RoomNumber= Direction;
else
{
// Tony's code
}
}
}
//DescribeRoom Function
void DescribeRoom (int Fig){
printf("Currently possition: ");
printf(Room[Fig].RoomName);
printf(Room[Fig].Description);
printf(Room[Fig].Description2);
}
//DescribeExit Function
void DescribeExit (int Fig){
printf("\n\nHe could go to any of the following places...\n");
printf("\n--------------------------------------------------------------------\n");
if(Room[Fig].Exit[North]!=0) printf ("\n(N) To the North he sees the %s",Room[Room[Fig].Exit[North]].RoomName);
if(Room[Fig].Exit[East]!=0) printf ("\n\n(E) East is the %s",Room[Room[Fig].Exit[East]].RoomName);
if(Room[Fig].Exit[South]!=0) printf ("\n\n(S) Looking South is the %s",Room[Room[Fig].Exit[South]].RoomName);
if(Room[Fig].Exit[West]!=0) printf ("\n\n(W) Going West will take him to the %s",Room[Room[Fig].Exit[West]].RoomName);
printf("\n\n--------------------------------------------------------------------");
printf("\n\n\nJust type the first letter of the direction or press 'Q' to quit.");
}
//GetDirection Function
int GetDirection(int Fig)
{
char test;
unsigned int NewDirection=0;
while(1)
{
test=getch();
if(test=='n') NewDirection=Room[Fig].Exit[North];
if(test=='e') NewDirection=Room[Fig].Exit[East];
if(test=='s') NewDirection=Room[Fig].Exit[South];
if(test=='w') NewDirection=Room[Fig].Exit[West];
if(test=='q') NewDirection=99;
if(NewDirection!=20) return(NewDirection);
printf("\n\nYou cannot go that way, there must be another path to take.");
}
}
void SetupRooms(char *user){
//ROOM 1
strcpy(Room[14].RoomName, "Outside");
strcpy(Room[14].Description, "\n\nHis eyes slowly rested back upon the house. Its unkept walls wore a thin green\ncoat of algae.");
strcpy(Room[14].Description2, "\nThe trees produced shadows, dancing along the empty flower beds as a slow wind\nkissed the top of the branches. It looked sorry, lonely, friendly.\n\n");
Room[14].Exit[North]=1;
Room[14].Exit[East]=0;
Room[14].Exit[West]=0;
Room[14].Exit[South]=0;
//ROOM 2
strcpy(Room[1].RoomName, "Entrance");
strcpy(Room[1].Description, "\n\nHe was greeted by a wide staircase, covered in what would have been a plush,\nwhite carpet.");
strcpy(Room[1].Description2, "\nHis eyes were drawn up the stairs but his better judgment told him to look\naround the ground floor first.\n\n");
Room[1].Exit[North]=6;
Room[1].Exit[East]=3;
Room[1].Exit[West]=2;
Room[1].Exit[South]=14;
//ROOM 3
strcpy(Room[2].RoomName, "Office");
strcpy(Room[2].Description, "\n\nHe slowly edged the door open, his left hand resting on the top of the blade\nhanging naturally off his hip.");
strcpy(Room[2].Description2, "\nThe chair lay helplessly on the floor, paper strewn around its base. The room\nbore nothing of interest.\n\n");
Room[2].Exit[North]=0;
Room[2].Exit[East]=1;
Room[2].Exit[West]=0;
Room[2].Exit[South]=0;
//ROOM 4
strcpy(Room[3].RoomName, "Small Hallway 1");
strcpy(Room[3].Description, "\n\nOn the other side of the hallway he came across two doors. The one to his left\nwas ajar, the other closed tightly.");
strcpy(Room[3].Description2, "\nThe small hallway brought him a feeling of both comfort and clostrophobia.\n\n");
Room[3].Exit[North]=4;
Room[3].Exit[East]=5;
Room[3].Exit[West]=1;
Room[3].Exit[South]=0;
//ROOM 5
strcpy(Room[4].RoomName, "Downstairs Bathroom");
strcpy(Room[4].Description, "\n\nThe room was the same as the day it was left. It was not big, or luxurious, but instead a reminder of what had been.");
strcpy(Room[4].Description2, "\nHe spent a minute searching the shelves, but a man in his position found little use for aftershave.\n\n");
Room[4].Exit[North]=0;
Room[4].Exit[East]=0;
Room[4].Exit[West]=0;
Room[4].Exit[South]=3;
//ROOM 6
strcpy(Room[5].RoomName, "Downstairs Bedroom");
strcpy(Room[5].Description, "\n\nThe cream walls adorned with posters, pictures and memories all suggested that\nof a youth");
strcpy(Room[5].Description2, "\nA double bed lay half dressed with belongings hurriedly strewn across it.\nMusical instruments, a guitar, a drum set.\n\n");
Room[5].Exit[North]=0;
Room[5].Exit[East]=0;
Room[5].Exit[West]=3;
Room[5].Exit[South]=0;
//ROOM 7
strcpy(Room[6].RoomName, "Landing");
strcpy(Room[6].Description, "\n");
strcpy(Room[6].Description2, "\n");
Room[6].Exit[North]=10;
Room[6].Exit[East]=8;
Room[6].Exit[West]=7;
Room[6].Exit[South]=1;
//ROOM 8
strcpy(Room[7].RoomName, "Living Room");
strcpy(Room[7].Description, "\nBLAH\n\n");
Room[7].Exit[North]=0;
Room[7].Exit[East]=6;
Room[7].Exit[West]=0;
Room[7].Exit[South]=0;
//ROOM 9
strcpy(Room[8].RoomName, "Kitchen");
strcpy(Room[8].Description, "\nBLAH\n\n");
Room[8].Exit[North]=0;
Room[8].Exit[East]=9;
Room[8].Exit[West]=6;
Room[8].Exit[South]=0;
//ROOM 10
strcpy(Room[9].RoomName, "Utility Room");
strcpy(Room[9].Description, "\nBLAH\n\n");
Room[9].Exit[North]=0;
Room[9].Exit[East]=0;
Room[9].Exit[West]=8;
Room[9].Exit[South]=0;
//ROOM 11
strcpy(Room[10].RoomName, "Upstairs Hallway");
strcpy(Room[10].Description, "\nBLAH\n\n");
Room[10].Exit[North]=12;
Room[10].Exit[East]=13;
Room[10].Exit[West]=11;
Room[10].Exit[South]=6;
//ROOM 12
strcpy(Room[11].RoomName, "Bedroom 1");
strcpy(Room[11].Description, "\nBLAH\n\n");
Room[11].Exit[North]=0;
Room[11].Exit[East]=10;
Room[11].Exit[West]=0;
Room[11].Exit[South]=0;
//ROOM 13
strcpy(Room[12].RoomName, "Bedroom 2");
strcpy(Room[12].Description, "\nBLAH\n\n");
Room[12].Exit[North]=0;
Room[12].Exit[East]=0;
Room[12].Exit[West]=0;
Room[12].Exit[South]=10;
//ROOM 14
strcpy(Room[13].RoomName, "Upstairs Bathroom");
strcpy(Room[13].Description, "\nBLAH\n\n");
Room[13].Exit[North]=0;
Room[13].Exit[East]=0;
Room[13].Exit[West]=10;
Room[13].Exit[South]=0;
//ROOM 15
strcpy(Room[0].RoomName, "Introduction");
strcpy(Room[0].Description, user);
strcat(Room[0].Description, "stood in the centre of the road, fully aware of this shadowed eyes and \nslumped shoulders.");
strcpy(Room[0].Description2,"\nThe chill of night pinched his skin and woke him from his thoughts.\n\n");
Room[0].Exit[North]=14;
Room[0].Exit[East]=0;
Room[0].Exit[West]=0;
Room[0].Exit[South]=0;
}
Here's the code with the problem.