Standard way to create and hold lots of similar objects?

I'm working on creating a maze using SFML, and I was wondering what the best way to go about creating the walls would be? If I have a Board class and Wall class, and the Board holds a vector of Wall objects, what's the simplest way to create each individual wall? It would be pointless really to try and hardcode the size and shape and position of every wall upon creation, but how else would I go about this? Would having arrays of stored data for the walls be a good idea? (Probably not because then I would have no idea which data corresponded to which wall?)

Let's say this is one of my walls:
1
2
3
4
void Board::SetWalls(){
//creates a wall 10*50, no rotation, position is (20, 20)
	walls_.push_back(Wall(10, 50, 0, 20, 20));
}
But I want to have 200 total wall pieces in this maze? Do I have to type walls_.push_back(//insert new wall values here); 200 times or is there an easier way?
Last edited on
Do I have to type walls_.push_back(//insert new wall values here); 200 times or is there an easier way?
1) Use emplace_back() to construct elements in-place.
walls_.emplace_back(10, 50, 0, 20, 20);

2) Use resource files. It can be a JSON file secribing each wall, or txt file with mase drawn in pseudographics — positions of individual walls would be calculated by your program.
http://en.cppreference.com/w/cpp/container/vector/vector
 
walls_(200, Wall(10, 50, 0, 20, 20));
@MiiNiPaa How would I integrate a JSON file into a c++ program? Also, if I'm simply describing each wall in a JSON file, wouldn't it be just as easy for me to type out each individual wall declaration in my c++ file?

@kbw that was just an example of one wall. Odds are, each wall will have a different size and position, and possibly a different rotation
How would I integrate a JSON file into a c++ program?
Save a file, open it, parse.
https://www.google.com/search?q=json+library+c%2B%2B

Also, if I'm simply describing each wall in a JSON file, wouldn't it be just as easy for me to type out each individual wall declaration in my c++ file?
You won't need to recompile your code just to shift walls around. And you do not have to describe each individual wall. You can describe primitives like "line of walls from there to there".

Also if you write WYSIWYG editor, generation of JSON would be automated.
And I strongly reccoment to stick with pseudographic mazes for start:
# - wall
@- start
* - finish
###@#
#   #
# ###
#   #
###*#
How do I make the program read a pseudographic maze?
How do I make the program read a pseudographic maze?

I don't understand the question.
http://www.cplusplus.com/doc/tutorial/files/
Thank you, so using that how do I actually draw an object on screen based on a character location in a text file?

Like in your map example above:
# - wall
@- start
* - finish
###@#
# #
# ###
# #
###*#


On the third row for instance, how would I set a tile's position to each symbol, and make a space between the walls where there is a space?
Last edited on
Imagine you load pseudoimage in vector of strings before that:
1
2
3
4
5
6
7
const int wall_height = 20;
const int wall_width = 20;
for(int i = 0; i < v.size(); ++i)
    for(int j = 0; j < v[i].size(); ++j) {
        if(v[i][j] == '#')
            walls_.emplace_back(wall_height, wall_width, 0, i * wall_height , j * wall_width);
    }
Ah ok, so the vector of strings is a 2d vector?
It is a vector which contains strings std::vector<std::string>
Amd this was just an example.
You can use static 2D buffer, 1D simulation of it, just read info char by char and make note where you are now....
Topic archived. No new replies allowed.