Vector vs 2D Array in Minesweeper

Why do we use a vector to store the cells instead of a 2D array?

In our Minesweeper implementation, we used a one-dimensional std::vector to store the cells, but a 2D array is also a viable option. Both approaches have their advantages, and the choice often depends on specific requirements and preferences. Let's explore both methods:

Using std::vector

We chose to use a std::vector for its flexibility and ease of use. Here's why:

  1. Dynamic sizing: std::vector can easily resize at runtime, which is useful if we want to change the grid size dynamically.
  2. Contiguous memory: std::vector stores elements in contiguous memory, which can lead to better cache performance.
  3. Standard library support: std::vector comes with many useful functions like push_back(), emplace_back(), and range-based for loop support.

Here's how we implemented it:

#include <vector>
#include "Minesweeper/Cell.h"

class MinesweeperGrid {
public:
  MinesweeperGrid(int x, int y) {
    using namespace Config;
    Children.reserve(GRID_COLUMNS * GRID_ROWS);
    for (int Col{1}; Col <= GRID_COLUMNS;
         ++Col) {
      for (int Row{1}; Row <= GRID_ROWS;
           ++Row) {
        constexpr int Spacing{CELL_SIZE +
                              PADDING};
        Children.emplace_back(
          x + (Spacing) * (Col - 1),
          y + (Spacing) * (Row - 1), CELL_SIZE,
          CELL_SIZE, Row, Col);
      }
    }
  }

  std::vector<MinesweeperCell> Children;
};

Using a 2D Array

A 2D array could be implemented using std::array or a raw C-style array. Here's how it might look:

#include <array>
#include "Minesweeper/Cell.h"

class MinesweeperGrid {
public:
  MinesweeperGrid(int x, int y) {
    using namespace Config;
    for (int Col{0}; Col < GRID_COLUMNS;
         ++Col) {
      for (int Row{0}; Row < GRID_ROWS; ++Row) {
        constexpr int Spacing{CELL_SIZE +
                              PADDING};
        Children[Row][Col] = MinesweeperCell(
          x + (Spacing)*Col, y + (Spacing)*Row,
          CELL_SIZE, CELL_SIZE, Row + 1,
          Col + 1);
      }
    }
  }

  std::array<std::array<MinesweeperCell,
                        Config::GRID_COLUMNS>,
             Config::GRID_ROWS>
    Children;
};

This approach has the advantage of more intuitive 2D indexing (Children[row][col]) and potentially better performance for fixed-size grids.

Both approaches are valid, and the choice often comes down to specific needs and personal preference. In our case, the vector approach provides more flexibility for potential future enhancements, such as dynamic grid sizing.

Creating the Grid

Building a two-dimensional grid of interactive minesweeper cells

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Custom Grid Shapes in Minesweeper
Can we implement a custom grid shape instead of just rectangular?
Cell Clearing Animations in Minesweeper
How can we add animations for cell clearing or mine explosions?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant