Creating the Grid

Vector vs 2D Array in Minesweeper

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

Abstract art representing computer programming

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.

This Question is from the Lesson:

Creating the Grid

Building a two-dimensional grid of interactive minesweeper cells

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Creating the Grid

Building a two-dimensional grid of interactive minesweeper cells

3D art representing computer programming
Part of the course:

Building Minesweeper with C++ and SDL2

Apply what we learned to build an interactive, portfolio-ready capstone project using C++ and the SDL2 library

Free, unlimited access

This course includes:

  • 37 Lessons
  • 100+ Code Samples
  • 92% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved