(Suggested book reading: Programming Abstractions in C++, Chapter 5.1)
Today we will learn about a collection object named Grid
.
A Grid
is a two-dimensional rectangular collection that stores data in rows and columns.
A grid is similar to a 2D array except with simpler and more convenient functionality.
When you declare a grid, you write the type of data it will contain in < >
brackets.
Here are two short code examples that use a grid:
Grid<char> tictactoe; tictactoe.resize(3, 3); // makes a 3x3 board tictactoe[1][1] = 'X'; // player X claims the center square of tic-tac-toe tictactoe[0][0] = 'O'; // player O claims the top left square tictactoe[1][0] = 'X'; // player X claims the left middle square tictactoe[2][2] = 'O'; // player O claims the bottom right tictactoe[1][2] = 'X'; // player X wins!
Grid<string> chessBoard; chessBoard.resize(8, 8); chessBoard[2][3] = "knight"; chessBoard[1][6] = "queen";