Game design question - please help

I am creating a text adventure. There is a player class, and a map class. I will create many objects of class map, each one contains a 5 by 5 two-dimensional array for navigating a map. My question is, with what I have thus far, at the point where I want my player to say... move through a door, that takes him to the next map object, how can I accomplish that, and what would the code look like, again using what I have thus far?

If you can answer this I will worship you for life. Thank you so much for your time.

My files.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef GAME_AREA_H
#define GAME_AREA_H
#include <iostream>

class game_area
{
	public:
		
		game_area(const std::string file);
		static const int boardLimit = 5;
		
		void display();//DEBUGGING FUNCTION
		
		
	private:
		
		std::string area[boardLimit][boardLimit];
		
		
};

#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "game_area.h"
#include <iostream>
#include <fstream>

game_area::game_area(const std::string file)
{
	std::ifstream load_map( file.c_str() );
	if ( load_map.fail() )
	{
		std::cout << "An error occured while trying to open file '" << file << "'." << std::endl;
	}
	else
	{
		for ( int x = 0; x < boardLimit; x++ )
		{
			for ( int y = 0; y < boardLimit; y++ )
			{
				load_map >> area[x][y];
			}
		}		
	}
}

void game_area::display()//DEBUGGING FUNCTION
{
			for ( int x = 0; x < boardLimit; x++ )
		{
			for ( int y = 0; y < boardLimit; y++ )
			{
				std::cout <<  area[x][y] << " ";
			}
			std::cout << std::endl;
		}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef PLAYER_H
#define PLAYER_H
#include "game_area.h"
#include <iostream>

class player
{
	public:
		player();
		void updateLocation();
		
	private:
		int xCoord, yCoord;
		std::string currentLocation, lastLocation;
		static const int north = -1, west = -1, east = 1, south = 1;
};

#endif 

1
2
3
4
5
6
7
8
#include "player.h"
#include "game_area.h"
#include <iostream>

player::player()
{
	
}
Last edited on
Not sure if it's a duplicate, but it's close:
http://www.cplusplus.com/forum/beginner/131156/
I suggest you define a class Globe that will have Nodes like in a binary tree, except instead of left_child, right_child, _parent, you have the Nodes linked according to how you want your cities linked. Each Node will have game_area* data member, so your game_area class is still being used. Instead of std::string currentLocation, lastLocation in player, use data member Node* current_position (or Globe::Node* if you want Node a nested class of Globe) for the player's current location (pointers must really be used here due to the constant changes in the player's location during the game). When the player decides to move to a new city, the Globe class will call up one of its member functions to list out all the neighbouring Nodes to Player::current_position. Of course, all the Nodes must be defined and Globe itself during compile time. This will take up many lines of code depending on how complex you want your Globe and each game_area to be. Just the first thought that came out of my head.

Here is a snippet of a CheckerBoard math puzzle program that I wrote similar to the above ideas to give you some sense of what I mean. You can think of the checkerboard as your globe, and the AbstractNodes as your cities.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class AbstractNode {
	protected:
		int value, x, y;
		AbstractNode (int X, int Y): x (X), y (Y) {}
		void applyChanges() {establishParents();  updateValue();  createChildren();}  // Facade Pattern Method
	public:
		int Value() const {return value;}
	private:
		virtual void establishParents() = 0;
		virtual void updateValue() = 0;
		virtual void createChildren() = 0;
};

class AbstractCheckerBoard {
	protected:
		const int dimension_x, dimension_y, start_x, start_y;
		AbstractCheckerBoard (int X, int Y, int startX, int startY): dimension_x (X), dimension_y (Y), start_x (startX), start_y (startY) {}
	public:
		int Dimension_x() const {return dimension_x;}
		int Dimension_y() const {return dimension_y;}
		virtual AbstractNode* NodesFound (int i, int j) const = 0;
		virtual void display (int space);
};

class CheckerBoard: public AbstractCheckerBoard {
	private:
		class Node: public AbstractNode {
			private:
				CheckerBoard& checkerBoard;  // reference data member because a CheckerBoard::Node always belongs to a CheckerBoard
				Node *leftChild, *rightChild, *leftParent, *rightParent;
				friend class CheckerBoard;
				Node (CheckerBoard& cb, int X, int Y);  // Many private functions, including the constructor.
				~Node () {}				virtual inline void establishParents();
				virtual inline void updateValue();
				virtual inline void checkLeftChild();
				virtual inline void checkRightChild();
				virtual inline void createChildren();
		};
	public:
		CheckerBoard (int X, int Y, int startX, int startY);
//		~CheckerBoard(); 
		Node*** NodesFound() const {return nodesFound;}
		virtual AbstractNode* NodesFound (int i, int j) const {return nodesFound[i][j];}
	private:
		Node *startingPoint;
		Node ***nodesFound;  /* This 2-dimensional array of Node*'s is to search all Nodes found so that the latest constructed Node with the same coordinates as some Node already found 
		would be made equal to that Node. */
};

class CheckerBoardUpAllowed: public AbstractCheckerBoard {
	private:
		class Node: public AbstractNode {
			private:
				CheckerBoardUpAllowed& checkerBoard;  // reference data member because a CheckerBoardUpAllowed::Node always belongs to a CheckerBoardUpAllowed
				Node *leftChild, *rightChild, *topChild, *leftParent, *rightParent, *bottomParent;
				friend class CheckerBoardUpAllowed;
				Node (CheckerBoardUpAllowed& cb, int X, int Y);  // Many private functions, including the constructor.
				~Node () {}
				virtual inline void establishParents();
				virtual inline void updateValue();
				virtual inline void checkLeftChild();
				virtual inline void checkRightChild();
				virtual inline void checkTopChild();
				virtual inline void createChildren();
		};
	public:
		CheckerBoardUpAllowed (int X, int Y, int startX, int startY);
//		~CheckerBoardUpAllowed();
		Node*** NodesFound() const {return nodesFound;}
		virtual AbstractNode* NodesFound (int i, int j) const {return nodesFound[i][j];}
	private:
		Node *startingPoint;
		Node ***nodesFound;
};
Last edited on
whats a node o.o
A node is typically an element in a linked list or a tree that contains a piece of data and a link to the next element or node in the tree. Understanding what a node is is key to understanding linked lists.
http://www.cprogramming.com/tutorial/lesson15.html
http://en.wikipedia.org/wiki/Node_(computer_science)

Not sure why you've defined game_area::area to be a std::string. I may be reading too much into your game, but an adventure map such as yours typically contains a collection of rooms. Hint: Room should be a class. Each room then has attributes that define the name of the room, which way you can move in that room, what the next room is when you move in a particular direction, and usually a list of objects and/or enemies in the room. A room class usually has functions that define what actions you can take in a room.
Topic archived. No new replies allowed.