how to make dungeon crawl with classes?

Hello,
I recently the dungeon crawl exercice (http://www.cplusplus.com/forum/articles/12974/)

Make a program that outputs a simple grid based gameboard to the screen using either numbers or characters.
i.e.

. . . . . . . . . .
. G . . . . . . . .
. . . . . . T . . .
. . . . . . . . . .
. . . . T . . . . .
. . . . . . T . . .
. . . . . . . . . X

Allow the user (marked by G in the example) to move either up, down, left, or right each turn. If the player steps on a trap then they lose. If the make it to the treasure 'X' then they win.

★★ Add enemies that move randomly in any direction once per turn. (enemies just like traps cause the player to lose if touched)


And I want to solve this using classes.
I am trying to find a way to have the player and board distinct classes with private data.
The problem is that I don't know how to make the move function (specifically how to check for a valid move). I tryed something, but I always end up to cyclic dependency ( a pointer to the board object in the player object and a pointer to the player object in the board object ). This is a major problem, because things get so complicated that I can't manage to get it working.

Do you have any hints or ideeas about how can this be done?
First idea off the top of my head, not necessarily the best one. Obviously some implementation details missing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
typedef enum {md_up, md_left, md_down, md_right} MoveDir;

class Player
{
  int xPos_;
  int yPos_;
public:
  const int xPos() const { return xPos_; }
  const int yPos() const { return yPos_; }
};

class Board
{
public:
  void generateMoves(const Player &p, std::vector<MoveDir> &moves) {
    if(p.xPos() > 0) moves.push_back(md_left);
    if(p.xPos() < width-1) moves.push_back(md_right);
    ...
    ...
  }
};
Before the move is actually made check the position you are moving to in the array (which I assume you are using for the board) and output to the player whether or not the move is valid.

If you are not using an array for the board I highly recommend you do.


As for classes, kev82 has given you a good base to work with.
Hi,

I implemented something that works using classes and a vector, as you suggested.
It's not quite dungeon crawl, more like quake deathmatch board game. But I will implement oponents also.

Thank you for your help!
Topic archived. No new replies allowed.