Structural question

So, I'm creating a small text-adventure game to sharpen my skills and get me out of my comfort zone, but I'm stuck on how to accomplish something. The way I have it setup so far is there will be multiple 10x10 maps represents areas such as a forest, house, etc. with each index being a map_tile with exits, descriptions, items, etc. How could I get the player to move from for example, exit one map object at map[1][3] and enter the castle map[3][9]? I don't need code ( it might be helpful though ) just ideas!

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
#ifndef MAP_TILE_H
#define MAP_TILE_H
#include <iostream>
using std::string;


class Map_Tile
{
    public:
        Map_Tile();
        bool can_north();
        bool can_east();
        bool can_south();
        bool can_west();

        string get_north_description();
        string get_east_description();
        string get_south_description();
        string get_west_description();

        void set_exits(bool& N, bool& E, bool& S, bool& W);
        void set_exit_descriptions(string& N, string& E, string& S, string& W);
        void set_area_description(string& setDescription);

    private:
        bool northExit, eastExit, southExit, westExit;
        string entryDescription, northDescription, eastDescription, southDescription, westDescription;
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef MAP_SEGMENT_H
#define MAP_SEGMENT_H
#include "Map_Tile.h"
#include <iostream>
using std::string;


class Map_Segment
{
    public:
        const static int MAP_SIZE = 10;

        Map_Segment(const string areaName);

    private:
        Map_Tile game_board[MAP_SIZE][MAP_SIZE];

        void get_valid_exits(const string& areaName);
        void get_exit_descriptions(const string& areaName);
        void get_area_description(const string& areaName);
};

#endif 
Would this be a bad way to do it? These are two different classes, but the actor class is dependent on the information of the map. Would this be considered bad programming practice?

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
#ifndef ACTOR_H
#define ACTOR_H
#include "Map_Segment.h" // So we can hold the current map we are on
#include <iostream>

class Actor
{
    public:
        Actor();

        void set_coordinates(const int x, const int y);

        int get_x();
        int get_y();
        
        void move_north();
        void move_east();
        void move_south();
        void move_west();

    private:
        int xCoord, yCoord;
        Map_Segment *currentMap; // pointer to the current map we are dealing with
        
};

#endif 
Bump
Would this be considered bad programming practice?
Not necessarily, it depends always on how you use it.

Instead of a pointer you may consider an index. With this index you might the also access the descriptions/exits/etc.

1
2
3
4
5
6
7
8
9
10
11
struct gate
{
  int map_idx;
  std::string name;
...
  int to_map_idx;
  int to_gate_idx;
};

const map intial_maps[] = { ... };
const gate gates[] = { ... };
Do you mean when the character is in the map [3][9] the map will change into inside-castle?
If yes,

If the character is already in the position of castle that's it. You may clear the screen and start executing the code of castle
That was just an example, at certain places in the map, you will go from one area to another. For instance if there was a castle door at forest[0][0], and the user goes inside castle, the map will change castle[5][4], so of course I will change the coordinates. I do like your suggestion coder777. I might try to wrap my head around that and implement it.
Where would I hold the struct gate? In the Map_Tile class I presume?
Or main?
Last edited on
I suggest that you put all information regarding the gate in this struct that does not change throughout the whole program processing. Then make a global accessible const array [put it in a header]. There is no problem accessing constants from everywhere.

By the way: You certainly need more startup info for your game. Make them all const and global accessible. Accessing a constant object is always ok.
Thank you i will come up with something and post it up here for criticism! You gave me some great ideas to use.
Topic archived. No new replies allowed.