vendredi 27 janvier 2017

Can't figure why incorrect values are being assigned to vector?

I'm trying to learn C++, so I apologize if my code is messy or inefficient.

I've been trying to learn vectors, and I'm attempting to generate a "map" stored in one. So far, I can generate the vector, but I'm having troubles with assigning values to it. This is the function I have set up to assign values to my "map" vector:

void generateMap(mapSize size, vector<vector<vector<int>>>& map) {
int xSize, ySize;
//Resize Vector Based on Map Size Choice
if (size == tiny) { xSize = 10; ySize = 10; }
if (size == medium) { xSize = 30; ySize = 30; }
if (size == large) { xSize = 50; ySize = 50; }
map.resize(xSize, vector<vector<int>>(ySize, vector<int>(2)));
//Generate Map
for (int x = 0; x < xSize; x++) {
    for (int y = 0; y < ySize; y++) {
        if (x == 0 || x == xSize) map[x][y][0] = wallTile;
        else if (y == 0 || y == ySize) map[x][y][0] = wallTile;
        else map[x][y][0] = tileType::floorTile;
    }
} }

With the vector "map" defined in main as:

vector<vector<vector<int>>> map;

And my "mapSize" and tile enums defined as such:

enum tileType { emptyTile, floorTile, wallTile };
enum mapSize { tiny, medium, large }; //10x10, 30x30, 50x50 tiles, respectively

(In my head, the vector holds x and y coordinates to a "cell", and each "cell" (not sure what they're actually called) has two entries, tileType, and presence of a monster/trap/something else) To test, I attempted to display the "map" in main as such:

for (int y = 0; y < ySize; y++) {
    for (int x = 0; x < xSize; x++) {
        cout << map[x][y][0];
    }
    cout << endl;
}

Now, what I was expecting to be produced was a box, with the outside 4 "walls" being comprised of wallTiles (or in this case, 2s), and the inside filled with floorTiles (1s). What was actually displayed, however, is the top and left walls being comprised of 2s, and the rest being 1s. I'm not entirely sure what's going wrong since, to me, the if statements should have taken care of that. I went through with debug mode as well, watching the values change, and I still can't figure where it's setting the wrong "tiles" to 1s. It could also be that I'm going about assigning values to the vector wrong; I was just going off research from the c++ reference, as well as here.

At this point, I just need another set of (more experienced) eyes. Thanks!

(This is the full code, if it helps: http://ift.tt/2jehhrP)

Aucun commentaire:

Enregistrer un commentaire