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.
- When you want to visualize the contents of 2 dimensional data, (usually for verification purposes).
- When trying to print/verify Board games, Puzzles & 2D Video games that are based on some kind of grid
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 << ") "; });
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)
Notice how Grid::print()
removes the need for looping over x
& y
.
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, ".");
which produces:
....
....
which you can then build on by gradually implementing your lambda...