How can I convert from a class with a 2D vector of chars to a 2D vector of another obj with a char variable?

StackOverflow https://stackoverflow.com/questions/15888899

Question

I'm still new to this so my apologies in advance if I provide too much or not enough information for my problem.


The Run Down

This question isn't a rogue like game related question, but for a little background of what my program does.. is to be similar to a rogue like game.

Ok, so I had a class that had a member 2D char vector (the chars were manipulated to represent a "dungeon"). Everything worked seamlessly. I wanted to expand what my program could do so I decided to create a new class to replace the 2D vector of chars so that the char is just the visual representation of the new class and other variables can be stored along with that char.

For simplistic sake, I tried to remove what isn't necessary for this question. This new class is called Tile - which represents a space being used for the dungeon level. Tile has:

  • a char variable called displayChar.. defaults to ' ' changes to represent what it contains

  • (other variables..)


The Problems / My Poor guesses

I'm not the best at completely understanding some concepts of programming syntax/implementation, so please don't judge.

  • The way I filled the vector of chars, I resized it to the width of the game board, and set everything to ' ' since all the values were chars
  • I think I need to fill it with new, Tile objects and push them to the 2D Tile vector?

  • My other methods that manipulated the char values are giving errors.

  • I think I should change the methods to take a pointer to the Tile object and use the setDisplayChar(' ') method to change its value?

  • My at(int, int) method used to return the char at that location

  • I thought I could change it to "return m_vvTiles[y][x].getDisplayChar()" but I get an error

I'm bad at changing how something works and I usually end up making a mess out of my code. I'll post the code that is relevant for this. I'd greatly appreciate any help you can offer. And please let me know if I need to add more. (I'll try to keep the code minimized to only related methods). Thanks!


DungeonLevel.h

#include "Tile.h"
#include <vector>
#include <random>

class DungeonLevel {
public:
    DungeonLevel(int iWidth, int iHeight, std::mt19937 & mt);
    ~DungeonLevel(void);

    void dump();
    char at(int x, int y);

    int getWidth();
    int getHeight();

private:
    std::vector<std::vector<Tile>> m_vvTiles; //Tile was char

};

DungeonLevel.cpp

#include <iostream>
#include <random>
#include "DungeonLevel.h"

using namespace std;


DungeonLevel::DungeonLevel(int iWidth, int iHeight, std::mt19937 & mt){
    // Initialize the blank vector
    m_vvTiles.resize(iHeight);

    for(auto it = m_vvTiles.begin(); it != m_vvTiles.end(); it++ ){
    //      Tile tempTile = new;?
            (*it).resize(iWidth,' ');
    }

    // divide the level into 4x2 chunks
    int iChunkWidth = iWidth / 4;
    int iChunkHeight = iHeight / 2;

    // Taking the easy way out, and generating
    // a loop of tunnels first to drop rooms on to
    for( int x = (iChunkWidth/2); x <= ((3 * iChunkWidth) + (iChunkWidth/2)$
            m_vvTiles[iChunkHeight/2][x] = '#';
            m_vvTiles[iChunkHeight + (iChunkHeight/2)][x] = '#';
    }

    for( int y = (iChunkHeight/2); y <= (iChunkHeight + (iChunkHeight/2)); $
            m_vvTiles[y][iChunkWidth/2] = '#';
            m_vvTiles[y][(3 * iChunkWidth) + (iChunkWidth/2)] = '#';
    }

 void DungeonLevel::dump(){
    for( auto itOuter = m_vvTiles.begin(); itOuter != m_vvTiles.end(); itOu$
            for( auto itInner = (*itOuter).begin(); itInner != (*itOuter).e$
                    cout << *itInner.getDisplayChar(); ///Updated:: CAUSING ERROR//
            }

            cout << endl;
    }
}



//SEVERAL IRRELEVANT LINES FOR THIS PROBLEM..

}

char DungeonLevel::at(int x, int y){
    return m_vvTiles[y][x].getDisplayChar();
    //return m_vvTiles[y][x]; WORKED BEFORE
}

Tile.h

#include "Entity.h"
#include <vector>

class Tile : public Entity {
public:
    Tile(void);
    virtual ~Tile(void);
    //void setEntity(Entity * entityToSet); BOTH IRRELEVANT
    //Entity * getEntity();
    void setDisplayChar(char displayCharToSet);
    char getDisplayChar();
    //virtual void dumpObjectData(); IRRELEVANT AT THIS TIME


private:
    char displayChar;
    //Entity * theEntity; IRRELEVANT AT THIS TIME
};

Tile.cpp

#include "Tile.h"
#include "Entity.h"

using namespace std;

Tile::Tile(void){
    displayChar = '.';
    //theEntity = NULL;
}


Tile::~Tile(void){

}

void Tile::setDisplayChar(char displayCharToSet){
    displayChar = displayCharToSet;
}

char Tile::getDisplayChar(){
    return displayChar;
}

*Heres my existing error: *

For the method dump() in DungeonLevel.cpp,

Was it helpful?

Solution

Some of your questions with my answers

I think I need to fill it with new, Tile objects and push them to the 2D Tile vector?

No you need to resize it and fill it with Tile objects. Pretty much like you did when it was a char array. new doesn't come into it.

I think I should change the methods to take a pointer to the Tile object and use the setDisplayChar(' ') method to change its value?

I would use a reference.

I thought I could change it to return m_vvTiles[y][x].getDisplayChar() but I get an error.

That sounds right to me, what error did you get?

Everything about your redesign sounds well motivated to me. Good idea to make a back up before going on a major reorganization however.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top