Sign in to follow this  
CzarKirk

Dynamic Map Size

Recommended Posts

Hello. I am planning to make a Minesweeper game which as you know can have a board of any size. I'd like to know the best way to create a dynamic 2d map board, which will need to be passed as a parameter to something else (BlitzMax for drawing). I was thinking of a long 1D dynamic array, the coordinates worked out by some maths, and passing the pointer. Is this the best way? Thanks.

Share this post


Link to post
Share on other sites
how dynamic does it need to be? For minesweeper, I'm guessing you'd only need to resize it infrequently - so you could just use a standard 2D array and create a new one each time you wanted to resize it...

map = new array[newMapWidth, newMapHeight]; // in C# anyway

Alternatives would be one long dynamic array like you suggest (but that could get confusing), or a dynamic array of dynamic arrays?? Not sure if there is a List<> type object that is inherently 2D, but someone else might know...

Share this post


Link to post
Share on other sites
Whoops! Sorry.C++.

I'm unfamilular with lists, though I can use vectors. I was thinking along the lines of type* board; board = new type[size]; where type is whatever type I decide my board to be. Then define a macro like #define getindex(x, y) y * size + x so board[getindex(x, y)]; or I might just do #define board(x, y) newboardname[y * size + x]



BlitzMax will need to call a C++ function to get the board information to draw it. This is the tricky bit actually. In my Tic Tac Toe game I took the easy way out and simply made this C++ function boardtype getcell(int x, int y) {return gameboard[x][y];} Then for looped it to get each cell. But it doesn't seem the most efficent way. I'm trying to investigate ways of simply returning a pointer to BlitzMax. In this case the C++ will be easier to figure out than how BM can get the pointer. C++ something like boardtype* getboard() {return &gameboard;} ???

Thanks.

Share this post


Link to post
Share on other sites
I'd go with your previous approach rather than a pointer/macro solution. The latter may be more optimal, but it's (much) harder to maintain in the long run. It can too easily become a mess after just a few bugfixes.

So unless your game is running slow, I wouldn't go for such optimizations. What 'seems' to be unoptimal may not be a real bottleneck after all. First profile your code, then optimize. Of course, some things will obviously not be optimal, so don't start coding ahead blindly, but don't go just by what feels right.

Bytheway, I'd use std::vector over an array or dynamically allocated memory any day - much easier to work with and less chance to introduce bugs along the way. Memory leaks or off-by-one accesses are common mistakes. Besides, you'll hardly ever notice any difference in speed.

Share this post


Link to post
Share on other sites
Quote:
Original post by CzarKirk
Whoops! Sorry.C++.


Then use boost::multi_array.

Quote:

BlitzMax will need to call a C++ function to get the board information to draw it. This is the tricky bit actually. In my Tic Tac Toe game I took the easy way out and simply made this C++ function boardtype getcell(int x, int y) {return gameboard[x][y];} Then for looped it to get each cell. But it doesn't seem the most efficent way. I'm trying to investigate ways of simply returning a pointer to BlitzMax. In this case the C++ will be easier to figure out than how BM can get the pointer. C++ something like boardtype* getboard() {return &gameboard;} ???

Thanks.


Something like that. With boost::multi_array, you would call the .data() member function of the multi_array and return the result (it would be a pointer to a 'tile' - whatever you're storing to represent each space on the board - rather than to a 'board' per se).

But consider whether BlitzMax will actually let you work like that.

Also consider that it might be faster to go the other direction: create some kind of pointer to a *BlitzMax drawing function*, and pass it to a C++ function, which would loop over each cell and call the drawing function upon it. BlitzMax might not support that, though. I don't know, never worked with it - just citing common techniques when creating "bindings" with other languages.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this