Need Help with C program!
Thread Starter
Senior Member
Joined: May 2010
Posts: 126
Likes: 0
From: Boston, Massachusetts
Vehicle: 2000 Hyundai Tiburon
Hey guys im having difficulty with a c program and its deadline is tomorrow. I need help so bad in this! Basically I need to create a Text Adventure Game, no objects just moving from room to room!
Here are the instrutions:
When you start the game, you have a “current location” and you see the name of that location and a description printed to the screen:
Dead end
You are at a dead end of a dirt road. The road goes to the east.
In the distance you can see that it will eventually fork off. The
trees here are very tall royal palms, and they are spaced equidistant
from each other.
There is an exit to the east.
>
The description ends in a prompt. At this prompt, you can type north, south, east, or west, or you can of course quit. Any direction brings you to a new location, its name, and description:
>east
E/W Dirt road
You are on the continuation of a dirt road. There are more trees on
both sides of you. The road continues in each direction.
There is an exit to the east.
There is an exit to the west.
In your game, you will only be able to wander about from location to location and quit when you’re done. It’s a lot more work to implement objects you can manipulate, and game objectives you can reach, although you can feel free to add anything you like once the interface is working.
Before you get started, draw a map of locations. This makes the job of setting up neighbors much easier. Plan on implementing only a part of your map to start.
Requirements
To implement a location in the game, you must create a struct that represents the information about a location. The fields of this struct are:
the location’s name (a string)
the location’s description (a longer string)
a pointer to each of the location’s neighbors: to the north, to the south, to the east, and to the west
In main(), represent your “game world” as an array of these structs. Note: the size of this array should be exactly how many locations you intend to have. While testing, make this as small as 4. For submission, it must be at least 10.
Write a function that takes this array and initializes the name, description, and neighbor pointers of each of the locations in your game.
Note: This is not really a pretty function. It will be long. There is no quick way of doing all of them at once. Not without adding complexity, anyway.
Note also: Since the neighbor reference is a pointer, you need the address operator to get the address of another location in order to point to it:
/* the neighbor to the north of location 2 is location 3 */
locations[2].north = &locations[3];
Note also also: If there is nothing to the north of a location, the pointer must be set to NULL:
/* there is nobody to the south */
locations[2].south = NULL;
Note also also also: If you go north to get from A to B, going back south had better bring you back to A.
In main(), declare a pointer to one of the locations that will be your reference to where you “are” at any time during the game. Initialize it to point to an element of your array of places.
Note: Again, you will want to use the address operator here:
current_location = &locations[5];
Implement the following functionality in main():
Report the name, location, and existence of exits of the current location. This should be in its own function.
Give the user a prompt.
Accept the commands: north, south, east, west, quit.
If the user has entered a direction command, and there is an exit in that direction, change the user’s current location pointer. If there is no exit that way (the exit pointer is NULL), give an error message.
Give an error message for invalid commands.
If the user hasn’t quit, repeat from the top.
Here are the instrutions:
When you start the game, you have a “current location” and you see the name of that location and a description printed to the screen:
Dead end
You are at a dead end of a dirt road. The road goes to the east.
In the distance you can see that it will eventually fork off. The
trees here are very tall royal palms, and they are spaced equidistant
from each other.
There is an exit to the east.
>
The description ends in a prompt. At this prompt, you can type north, south, east, or west, or you can of course quit. Any direction brings you to a new location, its name, and description:
>east
E/W Dirt road
You are on the continuation of a dirt road. There are more trees on
both sides of you. The road continues in each direction.
There is an exit to the east.
There is an exit to the west.
In your game, you will only be able to wander about from location to location and quit when you’re done. It’s a lot more work to implement objects you can manipulate, and game objectives you can reach, although you can feel free to add anything you like once the interface is working.
Before you get started, draw a map of locations. This makes the job of setting up neighbors much easier. Plan on implementing only a part of your map to start.
Requirements
To implement a location in the game, you must create a struct that represents the information about a location. The fields of this struct are:
the location’s name (a string)
the location’s description (a longer string)
a pointer to each of the location’s neighbors: to the north, to the south, to the east, and to the west
In main(), represent your “game world” as an array of these structs. Note: the size of this array should be exactly how many locations you intend to have. While testing, make this as small as 4. For submission, it must be at least 10.
Write a function that takes this array and initializes the name, description, and neighbor pointers of each of the locations in your game.
Note: This is not really a pretty function. It will be long. There is no quick way of doing all of them at once. Not without adding complexity, anyway.
Note also: Since the neighbor reference is a pointer, you need the address operator to get the address of another location in order to point to it:
/* the neighbor to the north of location 2 is location 3 */
locations[2].north = &locations[3];
Note also also: If there is nothing to the north of a location, the pointer must be set to NULL:
/* there is nobody to the south */
locations[2].south = NULL;
Note also also also: If you go north to get from A to B, going back south had better bring you back to A.
In main(), declare a pointer to one of the locations that will be your reference to where you “are” at any time during the game. Initialize it to point to an element of your array of places.
Note: Again, you will want to use the address operator here:
current_location = &locations[5];
Implement the following functionality in main():
Report the name, location, and existence of exits of the current location. This should be in its own function.
Give the user a prompt.
Accept the commands: north, south, east, west, quit.
If the user has entered a direction command, and there is an exit in that direction, change the user’s current location pointer. If there is no exit that way (the exit pointer is NULL), give an error message.
Give an error message for invalid commands.
If the user hasn’t quit, repeat from the top.
Sounds pretty straight forward to me. Your instructor laid out pretty detailed instructions.
If you have a specific question I'd be happy to help.
I have a couple points though:
As stated, the game world should be setup in an array of structs. Each struct having record of the title, description, and 4 directions.
This method isn't the best, but it will work. For the record, I'd use a 2 dimensional array as a map with each room having a location next to another.
Anyway, You are going to be using a lot of pointers. To build your structs, your instructor said to manually input static values. (This is time consuming, so pulling the values from a '.map' file would be better.) So create a current_room pointer and change the value of the pointer every time a direction is chosen based on the selection of 'north', 'south','east','west'.
If you have a specific question I'd be happy to help.
I have a couple points though:
As stated, the game world should be setup in an array of structs. Each struct having record of the title, description, and 4 directions.
This method isn't the best, but it will work. For the record, I'd use a 2 dimensional array as a map with each room having a location next to another.
Anyway, You are going to be using a lot of pointers. To build your structs, your instructor said to manually input static values. (This is time consuming, so pulling the values from a '.map' file would be better.) So create a current_room pointer and change the value of the pointer every time a direction is chosen based on the selection of 'north', 'south','east','west'.
Code:
//defines a type 'room' as a defined struct
typedef struct {
char title[100];
char description[250];
room *north;
room *east;
room *west;
room *south;
} room;
room allrooms[50];
room *current_room;
void main(){
//declare all the elements in allrooms
//draw this out and make everything connect
strcpy(allrooms[0].title,'First Room');
strcpy(allrooms[0].description,'First Room Description');
//Allrooms[1] does not yet have to be defined, as long as it is defined later, before processing
allrooms[0].east = allrooms[1];
//Continue this for all the rooms
//Set first room to current_room
current_room = allrooms[0];
//while loop to ask for input
while(true){
//display title and description, and prompt for input
char input*;
if(strcmp(tolower(input),'east') == 0){
current_room = current_room.east;
} else if(){ // Use if states to find out what to do
} else {
//Fit error message for user to make correct input
}
}
}
Moderator


Joined: Feb 2009
Posts: 11,732
Likes: 5
From: Leesville, Louisiana
Vehicle: 2001 Hyundai Tiburon
I made one of these in Fortran or Pascal (it's been so long) on my TI-85 calculator about 15 years ago.
I think the way I did it was to make a map, number each room, and give options to the user per room. I don't know C very well, so I'll pseudocode some java for you
However, like lazyshot said, a better way of doing it would be to create a 2d array of control values and another of words. One 2d array would be the words you see, and the other 2d array would be the options to go east west north or south. A series of testing would allow you to construct the basics in your language, and then use a spreadsheet application to finish the words and what not. That method would allow you to easily create a string in the tables like +E+ROCK+DOOR which would give you the ability to go east, Pick up the rock, or open the door.
I think the way I did it was to make a map, number each room, and give options to the user per room. I don't know C very well, so I'll pseudocode some java for you
Code:
package javaapplication3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author adam
*
*/
public class Main {
public static void main(String[] args) {
room1();
}
private static String getInput( boolean east, boolean west, boolean south, boolean north ){
String inputData = "";
while ( ((! inputData.equals("east"))) && ( ! inputData.equals("west")) && ( ! inputData.equals("north")) && ( ! inputData.equals("south")) && ( ! inputData.equals("command") )) {
BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
try {
inputData = inputReader.readLine();
}
catch(IOException e) {
System.out.println("Error reading keyboard input") ;
if ( inputData.equals("quit")) {
System.exit(0);
}
if ( inputData.equals("mycommand")) {
//do stuff
}
if (( inputData="east" ) && ( ! east)){
inputData="";
System.out.print("try again");
}
}
}
return inputData;
}
static void room1(){
System.out.println("Dead end");
System.out.println("You are at a dead end of a dirt road. The road goes to the east.");
System.out.println("In the distance you can see that it will eventually fork off. The");
System.out.println("trees here are very tall royal palms, and they are spaced equidistant");
System.out.println("There is an exit to the east.");
String input=getInput( true, false, false, false);
if ( input.equals("east") ) {
room2();
}
}
static void room2() {
System.out.println("E/W Dirt road");
System.out.println("You are on the continuation of a dirt road. There are more trees on");
System.out.println("both sides of you. The road continues in each direction.");
System.out.println("There is an exit to the east.");
System.out.println("There is an exit to the west.");
}
}
However, like lazyshot said, a better way of doing it would be to create a 2d array of control values and another of words. One 2d array would be the words you see, and the other 2d array would be the options to go east west north or south. A series of testing would allow you to construct the basics in your language, and then use a spreadsheet application to finish the words and what not. That method would allow you to easily create a string in the tables like +E+ROCK+DOOR which would give you the ability to go east, Pick up the rock, or open the door.


