Grid

What it is

A utility for creating 2D text in a grid format. Grid abstracts away the printing of the grid, so you can focus on how to represent the contents of each cell.

Where is this useful?

  1. When you want to visualize the contents of 2 dimensional data, (usually for verification purposes).

  2. When trying to print/verify Board games, Puzzles & 2D Video games that are based on some kind of grid

Usage

Conventional Usage

Grid is passed a width, height, and a function pointer or lambda to represent the contents of each cell.

For example:

auto grid = ApprovalTests::Grid::print(
    4, 3, [](int x, int y, auto& os) { os << "(" << x << "," << y << ") "; });

(See snippet source)

which produces:

(0,0) (1,0) (2,0) (3,0)
(0,1) (1,1) (2,1) (3,1)
(0,2) (1,2) (2,2) (3,2)

(See snippet source)

Notice how Grid::print() removes the need for looping over x & y.

Simple Starting Point

When developing with Fake it till you make it Grid can be used to start by printing a simple grid of repeating text, such as:

auto grid = ApprovalTests::Grid::print(4, 2, ".");

(See snippet source)

which produces:

....
....

(See snippet source)

which you can then build on by gradually implementing your lambda…