How can I model a grid(minesweeper problem) as a data type in the best possible way? Is it better to use a vector as 2 dimensional entity. The reason for a vector is because of its bounds checking capabilities. Is my assumption right?
Thanks :)
Since the grid-size is (I presume!) fixed, a vector gives you little advantage over an array. So I recommend going with a 2D array.
While not necessary, you might find it useful to add sentinel values around the boundary of the grid to remove the need for implicit bounds checking. An example is placing 0's all around the grid for when you're summing the number of neighbouring mines. Read this article for some simpler examples to begin with.
I personally think that the vest option is to use a specialized class that acts like a multidimensional array while giving you the benefits of the STL vector (size-aware, no implicit conversions to pointers, etc.). Boost's multi_array class might be a good candidate here, and more generally whenever you need a multidimensional array. It handles all of the weirdness normally associated with raw C++ arrays without sacrificing performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With