Thanks for the tip. I wanted to free the memory (when the program ends, just before ), but it just segfaults - any idea how to resolve this? This is on a Linux-based operating system.
Code from adventure.c:
Code:
// Text Adventure v1 by Joshua O'Leary
// Preprocessor Directives
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "definitions.h"
// Debug Mode when set to 1
#define DEBUG 1
#define INPUTLEN 20
#define PROMPT "\nWhat now?"
//extern struct gameplay status;
//void initial_state()
//{
// status.gameover = 0; status.room = 2;
//}
int main()
{
//initial_state();
char *input = malloc(INPUTLEN * sizeof(char));
printf("\nAdventure v1");
printf("\nYou are outside the house");
while ( status.gameover == 0 )
{
prompt(&input);
}
return 0;
}
void control(char value)
{
switch (value)
{
case 'q':
printf("\nExiting...");
status.gameover = 1;
break;
}
}
void prompt(char *input)
{
printf("%s", PROMPT);
gets(input);
//printf("%c", *input);
//input++;
//printf("%c", *input);
analyse(&input);
}
void analyse(char **input)
{
#if DEBUG == 1
printf("\nInput: %s", *input);
printf("\nRoom: %d", status.room);
#endif
switch ( **input )
{
case 'Q':
case 'q':
printf("\nquit called");
control('q');
break;
case 'N':
case 'n':
gotoroom(0);
break;
case 'S':
case 's':
gotoroom(1);
break;
case 'E':
case 'e':
gotoroom(2);
break;
case 'W':
case 'w':
gotoroom(3);
break;
default :
printf("\n?!");
break;
}
}
rooms.c (kept separate because there may be long descriptions:
Code:
#include <stdio.h>
#include "definitions.h"
void gotoroom(int direction)
{
// Room Descriptions
const char *descript[] = { "You are outside the house",
"You are in the front room",
"You are on the door-mat" };
// Room Maps
const int map[][4] = { { 1, -1, -1, -1 }, /* { north, south, east, west } */
{ 2, 0, 3, 2 }, /* a negative value means you can't move there */
{ -1, 1, 1, 3 } };
//printf("%d", map[status.room][direction]);
//printf("%d", status.room);
if ( map[status.room][direction] < 0 )
{
printf("\nYou can\'t go there");
}
else
{
status.room = map[status.room][direction];
printf("\n%s", descript[status.room]);
}
}
And of course definitions.h:
Code:
void control(char value);
void prompt();
void analyse(char **input);
void gotoroom(int direction);
struct gameplay
{
int room;
int gameover;
int hasitems[5];
} status;