Moving between areas

This is a discussion on Moving between areas within the C++ Programming forums, part of the General Programming Boards category; Hello all, I wish to move from place to place, like in the attatchment of this post. The user comes ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128

    Moving between areas

    Hello all,

    I wish to move from place to place, like in the attatchment of this post. The user comes to an area, and can chose to go different ways, at this point the user does not have to have an option to go back, just to move on in the path selected. When moving to the next path, a text will display saying stuff about the surrounds and then he is given another choice to move onto 3 more areas.

    I was just wondering how I would go about this, I have the room class, and was thinking an array but i dont know if i cud move around properply using arrays as i have 50 odd rooms...

    Not sure how to go about it

    Thanks in advance
    Attached Images Attached Images  

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    *(You have a list of rooms that are linked.)

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Sorry but I am new to cpp and I am not sure what a linked list in cpp is?

  4. #4
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Hello Aliaks

    You could make your place map with an array. Here is an example:
    Code:
    struct PlaceMapElement
    {
    	int northIndex;
    	int southIndex;
    	int eastIndex;
    	int westIndex;
    };
    
    PlaceMapElement placeMap[] = {
    	/* n   s   e   w */
    	{ -1,  1, -1, -1},
    	{  0, -1, -1, -1},
    
    };
    Each PlaceMapElement has an index back into the placeMap array for each direction you might want to go in. In the example, the Place at element 0 can lead to south to the Place at element 1 but it cannot lead north because that has an invalid index which you would check for.

    You could use the placeMap to initialize your Place objects. Note, that you cannot have an array of classes initialized in the way I have shown above if your classes have constructors or private members.

    Even if you go the way of linked rooms, you might find a placeMap array useful. It lets you see your map, and you'd need some way to initialize your links.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    I see, where can I read further into maps? I have tried google but can't seem to find anything, and my CPP book doesnt so much as mention a map .

    Well, My room class only really has a cout function with info, I just did it that way to allow expasion later on.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,591
    But it's not a map; it's a regular array. That's what the ideas is based on.
    Each room contain a new set of indexes in your array where the room it leads to is placed inside the array.
    Nothing more to it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    A map in C++ has a different meaning from a map use for navigation. I suppose I should have used a different name. Here is a more fleshed out idea.
    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
    struct PlaceMapElement
    {
    	char* roomName;
    	char* description;
    	int northIndex;
    	int southIndex;
    	int eastIndex;
    	int westIndex;
    };
    
    PlaceMapElement placeMap[] = {
    	/* n   s   e   w */
    	{ "Grue room", "You are in a dark room.",
    	-1,  1, -1, -1},
    	{ "Kitchen", "You are in a bright room.",
    	0, -1, -1, -1},
    
    };
    
    class Place
    {
    public:
    	Place()
    	{
    
    	}
    	
    	void Initialize(const PlaceMapElement& pme)
    	{
    		roomName = pme.roomName;
    		description = pme.description;
    		northIndex = pme.northIndex;
    		southIndex = pme.southIndex;
    		eastIndex = pme.eastIndex;
    		westIndex = pme.westIndex;
    	}
    
    	void Describe()
    	{
    		cout << "You are in " << roomName << endl;
    		cout << description << endl;
    		if (northIndex >= 0) cout << "You can go North";
    		if (southIndex >= 0) cout << "You can go South";
    		if (eastIndex >= 0) cout << "You can go East";
    		if (westIndex >= 0) cout << "You can go West";
    	}
    
    private:
    	std::string roomName;
    	std::string description;
    	int northIndex;
    	int southIndex;
    	int eastIndex;
    	int westIndex;
    };
    
    Place* places;
    
    void initializePlaces()
    {
    	places = new Place[ARRAY_SIZE(placeMap)];
    	for(int ix = 0; ix < ARRAY_SIZE(placeMap); ++ix)
    	{
    		places[ix].Initialize(placeMap[ix]);
    	}
    }
    
    int main(void)
    {
    	initializePlaces();
    	return 0;
    }

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,591
    Use std::string for strings, or alternatively, const char*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    Here is a simple room class and a navigating application:
    Code:
    #import <string>
    #import <iostream>
    
    // A very simple room class
    class Room {
    public:
    	string description;
    	
    	Room *north;
    	Room *south;
    	Room *east;
    	Room *west;
    };
    
    // A very simple player class
    class Player {
    public:
    	Room *location;
    };
    
    int main (int argc, char **argv) {
    
    	// Create an example map with four rooms:
    	// For an actual game you might load this data from a file.
    
    	// Create four rooms.
    	Room *rooms = new Room [4];
    	rooms[0].description = "Room 1";
    	rooms[1].description = "Room 2";
    	rooms[2].description = "Room 3";
    	rooms[3].description = "Room 4";
    
    	// Connect room 1 and room 2.
    	rooms[0].east = &rooms[1];
    	rooms[1].west = &rooms[0];
    
    	// Connect room 2 and room 3.
    	rooms[1].north = &rooms[2];
    	rooms[2].south = &rooms[1];
    
    	// Connect room 2 and room 4.
    	rooms[1].south = &rooms[3];
    	rooms[3].north = &rooms[1];
    
    	// Now we have a map that looks like this:
    	//         [ 3 ]
    	//           |
    	// [ 1 ] - [ 2 ]
    	//           |
    	//         [ 4 ]
    
    	// Setup the player.
    	Player player;
    	player.location = &rooms[0];
    
    	// Game loop:
    	while (1) {
    		// Display the players location and possible movements.
    		cout << "------------------" << endl;
    		cout << "Location:   " << player.location->description << endl;
    		if (player.location->north)
    			cout << "(N)orth to: " << player.location->north->description << endl;
    		if (player.location->south)
    			cout << "(S)outh to: " << player.location->south->description << endl;
    		if (player.location->east)
    			cout << "(E)ast to:  " << player.location->east->description << endl;
    		if (player.location->west)
    			cout << "(W)est to:  " << player.location->west->description << endl;
    		cout << "(Q)uit" << endl;
    
    		// Get input and decide where to go next.
    		char input;
    		cin >> input;
    		if (input == 'n' && player.location->north)
    			player.location = player.location->north;
    		if (input == 's' && player.location->south)
    			player.location = player.location->south;
    		if (input == 'e' && player.location->east)
    			player.location = player.location->east;
    		if (input == 'w' && player.location->west)
    			player.location = player.location->west;
    		if (input == 'q')
    			break;
    	}
    
    	delete [] rooms;
    }

  10. #10
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Aliaks here is another idea. Having a Place class with a fixed number of connected rooms is a bit restrictive. How about this. You have a Place class with an integer placeID and a std::vector<int> of other places that you can get to from a particular place instance. Keep all your Place instances on a STL map like this std::map<int, Place*> allPlaces;

    Code:
    #include <vector>
    #include <map>
    
    class Place
    {
    public:
    	int placeId;
    
    	std::vector<int> connectedPlaces;
    };
    
    std::map<int, Place*> allPlaces;
    Now you are not restricted to the number of places you can reach from a room. You can even have a teleportation device in your game that can take you to a growing list of new places.

    When it's time to prompt the user to move, you can go through the vector of connectedPlaces, find the place instance on the map of allPlaces, and print the description of the room.

    the stl map gives you flexibility without having to worry about linking up rooms with pointers.

    Furthermore, you could now (optionally) have your world represented in a file, so your program becomes more flexible. You don't even have to recompile to change your world. Here is what a world file might look like.

    Place
    ID 1000
    Description: The kitchen
    ConnectedPlace 2000
    ConnectedPlace 2001
    ConnectedPlace 2002
    Place
    ID 2000
    Description: The dungeon
    ConnectedPlace 1000

    As you read the file, you instantiate each Place and put it into the map. I suggest instantiating a Place in the heap and placing the pointer into the std::map instead of copying the entire Place instance into the map.
    Last edited by Zlatko; 07-12-2009 at 02:08 PM.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,591
    Except with this, you don't know how to get to each place. Do you go north, south? Where is the teleporter?
    Furthermore, a map is slower than a vector at random access. There is still no need for a map; a vector will do fine. Each room just needs a unique ID, and they don't need to be contiguous, so a room where you go west from the current can have ID 500, and one where you go north from the current can have 2051.

    rossipoo has the best idea if you have ask me (it's the solution I would probably use). Basically it's just the same as previous suggestions, only adding having to assign valid pointers instead of valid IDs.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Quote Originally Posted by Elysia View Post
    Except with this, you don't know how to get to each place. Do you go north, south?
    I was thinking the game would dynamically print out the list of choices corresponding to the elements in the connectedPlaces vector and the user would choose. But I see your point, if the player wants to draw a diagram of connected rooms it becomes difficult if they cannot associate a direction with the travel.

  13. #13
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Code:
    #import <string>
    #import <iostream>
    
    // A very simple room class
    class Room {
    public:
    	string description;
    	
    	Room *north;
    	Room *south;
    	Room *east;
    	Room *west;
    };
    
    // A very simple player class
    class Player {
    public:
    	Room *location;
    };
    
    int main (int argc, char **argv) {
    
    	// Create an example map with four rooms:
    	// For an actual game you might load this data from a file.
    
    	// Create four rooms.
    	Room *rooms = new Room [4];
    	rooms[0].description = "Room 1";
    	rooms[1].description = "Room 2";
    	rooms[2].description = "Room 3";
    	rooms[3].description = "Room 4";
            more rooms here
            etc
    
    
    	// Connect room 1 and room 2.
    	rooms[0].east = &rooms[1];
    	rooms[1].west = &rooms[0];
    
    	// Connect room 2 and room 3.
    	rooms[1].north = &rooms[2];
    	rooms[2].south = &rooms[1];
    
    	// Connect room 2 and room 4.
    	rooms[1].south = &rooms[3];
    	rooms[3].north = &rooms[1];
    
    	// Connect more rooms
    	rooms[etc].south = &rooms[etc];
    	rooms[etc].north = &rooms[etc];
    
    	// Now we have a map that looks like this:
    	//         [ 3 ]
    	//           |
    	// [ 1 ] - [ 2 ]
    	//           |
    	//         [ 4 ]
    
    	// Setup the player.
    	Player player;
    	player.location = &rooms[0];
    
    	// Game loop:
    	while (1) {
    		// Display the players location and possible movements.
    		cout << "------------------" << endl;
    		cout << "Location:   " << player.location->description << endl;
    		if (player.location->north)
    			cout << "(N)orth to: " << player.location->north->description << endl;
    		if (player.location->south)
    			cout << "(S)outh to: " << player.location->south->description << endl;
    		if (player.location->east)
    			cout << "(E)ast to:  " << player.location->east->description << endl;
    		if (player.location->west)
    			cout << "(W)est to:  " << player.location->west->description << endl;
    		cout << "(Q)uit" << endl;
    
    		// Get input and decide where to go next.
    		char input;
    		cin >> input;
    		if (input == 'n' && player.location->north)
    			player.location = player.location->north;
    		if (input == 's' && player.location->south)
    			player.location = player.location->south;
    		if (input == 'e' && player.location->east)
    			player.location = player.location->east;
    		if (input == 'w' && player.location->west)
    			player.location = player.location->west;
    		if (input == 'q')
    			break;
    	}
    
    	delete [] rooms;
    }
    So if I wanted to expand rooms and make it more large scale, It would be a matter of adding alot of rooms and connecting them all? giving them values?

    player.location->description what does ' -> ' mean? I have only used it a few times.
    Last edited by Aliaks; 07-13-2009 at 12:16 AM.

  14. #14
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    The bit that gets me is that if Its larger scale like the picture, the loop cud get masssive, and wouldn't you have to have a tone of if statements or switches taht if he chose this then give him the option of choosing the next 3 places? Or am I missing a major point of the above example?
    Attached Images Attached Images  

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    You could expand a room to be whatever you like. You want to be able to move around inside the room? Have monsters? Treasure? It's all up to you. You cam see the o ly thing I implemented was a name for the room.

    Making the map bigger is indeed only a matter of adding rooms and connecting them all. For a large game, probably you would not want to do that by hand. You might make another application that is a level editor and could create a file which the game could then load in.

    The game loop won't get bigger as you add rooms. If you study the game loop, you'll notice that it is generic to any room. It will get bigger if you decide to add things to do in the rooms.

    The -> operator is the same as a dot . only it is for pointers. player.location is a variable that holds a pointer to whatever room you are currently in.

Page 1 of 7 1234567 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack overflow errors in 3 areas
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 03:20 PM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  3. rescaling areas
    By hannibar in forum C Programming
    Replies: 2
    Last Post: 02-28-2006, 02:22 PM
  4. 3D moving
    By bluehead in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2005, 04:46 AM
  5. Simple program i cant get (dot moving)
    By Lupusk9 in forum C++ Programming
    Replies: 4
    Last Post: 09-14-2004, 08:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21