1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <iostream>
#include <string>
#include <array>
#include <map>
#include <numeric>
#include <iomanip>
struct board {
enum row_type { A, B, I, N, G, O }; // A = 0, B = 1 etc
static constexpr std::size_t NUMBERS_PER_ROW = 15 ;
// key: row mapped value: array of numbers
// https://en.cppreference.com/w/cpp/container/map
// https://en.cppreference.com/w/cpp/container/array
std::map< row_type, std::array<int,NUMBERS_PER_ROW> > plays ;
board()
{
int n = 1 ; // numbers for the first row start at 1
for( row_type row : { A, B, I, N, G, O } )
{
std::array<int,NUMBERS_PER_ROW> numbers ;
// fill the array with numbers n, n+1 ...
// https://en.cppreference.com/w/cpp/algorithm/iota
std::iota( numbers.begin(), numbers.end(), n ) ;
plays[row] = numbers ; // insert key, array pair into the map
n += NUMBERS_PER_ROW ; // numbers for the next row start at this value
}
}
// helper function to print the board
friend std::ostream& operator<< ( std::ostream& stm, const board& brd )
{
static const std::string chars = "ABINGO" ;
for( const auto& [row,numbers] : brd.plays )
{
stm << chars[row] << " [ " ;
for( int n : numbers ) stm << std::setw(3) << n ;
stm << " ]\n" ;
}
return stm ;
}
};
int main()
{
board brd ;
std::cout << brd << '\n' ;
}
|