# dynamic structures of 2d dynamic arrays I need help obviously

This topic is 3589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello I'm coding this game where I have x amount of blocks on the screen and the number of blocks in the level increase by some variable as you progress through the level, and since I knew that the compiler wouldn't know how many blocks were on the screen before compile time I didn't know if I need to use pointer-to-pointer or a dynamic array, but see this is where it baffles the hell out of me, and I'd like to resolve this problem by tonight, for it's been FUN working on it. However, before completing throwing out there what little code I have for attempting this project let me give you a little history and reason and game objectives I have for this project. There are some number of blocks in the level that you must collide into with before exiting to the next level. Each block must be collided into before exiting or the game is over. You have a specific amount of time to do so before the game is over. You can only move in one direction at any given time. So if I pressed the up/down/right/left button (like orbox) I move in that direction until I have collided with the box. However, if I collide with a specific box on the level another box may move, delete itself, or a random box created, which would make you have to rethink your solution to the given puzzle. Example: Let's say we have 10 blocks on the screen. And I begin by moving to block 1 then to block 2 and 3 as so on. Well as I hit block 3 moving toward block 4 that specific block has moved to a place on the board that if I were to try and move there I would go off the screen, for I can't go in that direction, ergo the game is over and the level is reset, or if upon arrival of block 3 a new block has been placed in the level, which makes me have to retrace my steps until it's deleted, or i have touched base with it, and back on track. etc... So here is what little code I have, for I'm confused on dynamic arrays, which I thought would be what I needed to use, but i'm having trouble creating x number of structures that is really unknown of how many there will be on the screen to have a given x, y pos, width, height (represented by size) and a boolean value to either be created, deleted, or moved. I have a structure of the attributes of the wall
struct wall_attributes
{
int x;
int y;
static const int size = 25;
bool move;
bool create;
bool delete;
}lvl_walls;

//there's the structure


i thought about using vector<int> x, y; However, then I thought x&y could just be a 2d array of numbers representing the blocks position on the screen, but I don't know how to represent a structure using vector, and if I could represent vector<struct> wall_attributes; would it be of any benefit to have a 2d array at all? Please help me on this. And if I've confused anyone let me just simply reiterate what I'd like to accomplish with this code. Have a dynamic array structure that is unkown at compiled time and may create or delete itself during runtime during game play of any level, or the blocks move to another position on the screen at any given time. Thanks,

##### Share on other sites

The header file, a.k.a problem I'm having right now.

Soon as I start declaring the class as in soon as I make the source file of this header
#ifndef LEVEL_H#define LEVEL_H#include <vector>class Level{	public:			Level();	~Level();	void Move(int pos_x, int pos_y);	void Begin(int num, double time);	void Create(int num);	void Delete(int num);	int x_pos();	int lvl_size();	int y_pos();	bool collided();private:	typedef struct lvl_att	{		int pos_x;		int pos_y;		static const int wall_size = 25;		bool create;		bool delete_this;		bool move;	};public: vector<lvl_att> my_lvl;	};#endif

I get the following error in the debugger

c:\documents and settings\carl\my documents\visual studio 2008\projects\first project\first project\Level.h(36) : error C2143: syntax error : missing ';' before '<'c:\documents and settings\carl\my documents\visual studio 2008\projects\first project\first project\Level.h(36) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-intc:\documents and settings\carl\my documents\visual studio 2008\projects\first project\first project\Level.h(36) : error C2238: unexpected token(s) preceding ';'Build log was saved at "file://c:\Documents and Settings\carl\My Documents\Visual Studio 2008\Projects\First Project\First Project\Release\BuildLog.htm"First Project - 3 error(s), 0 warning(s)========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

WHY?

Am I not declaring a dynamic structure correctly?

I wanted to use the Begin function to create x amount of structures depending on how many I needed at the time, and then have create, move, and delete functions do what's needed with that dynamic number of strcutures I'm having trouble creating.

##### Share on other sites
What is that typedef doing there? Also, std::vector.

##### Share on other sites
As mentioned above it's std::vector, that should fix your problems.

As for the original question. With vector or list you'll have to iterate throught it to find the block you might collide with, I'd assume this won't really be a problem since you probably won't have that many blocks. Still it would be better to use something like.
typedef std::map<std::pair<int, int>, boost::shared_ptr<wall_attributes> > Blocks;Blocks mBlocks;void testBlock(int x, int y) {  Blocks::iterator it = mBlocks.find(std::make_pair(x, y));  if (it != mBlocks.end()) {    // there's a block, do something and remove it    mBlocks.erase(it);  }}void addBlock(boost::shared_ptr<wall_attributes> block) {  if (mBlocks.find(std::make_pair(block->x, block->y)) != mBlocks.end()) throw std::runtime_error("Block already exists in current position);  mBlocks[std::make_pair(block->x, block->y)] = block;}
This way you can easily test if there's a block in the current position.

##### Share on other sites
I shall try this, and thanks for the replies.