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
|
#include <iostream>
struct FBoard
{
// It should have an 8x8 array of char for tracking the positions of the pieces.
char board[8][8] ;
static constexpr char empty = '.' ;
static constexpr char o_piece = 'O' ;
static constexpr char x_piece = 'X' ;
// A default constructor that initializes the array to empty
// you can use whatever character you want to represent empty
// member initializer list: https://en.cppreference.com/w/cpp/language/initializer_list
FBoard() : board { { empty, empty, empty, empty, empty, empty, empty, empty },
{ empty, empty, empty, empty, empty, empty, empty, empty },
{ empty, empty, empty, empty, empty, empty, empty, empty },
{ empty, empty, empty, empty, empty, empty, empty, empty },
{ empty, empty, empty, empty, empty, empty, empty, empty },
{ empty, empty, empty, empty, empty, empty, empty, empty },
{ empty, empty, empty, empty, empty, empty, empty, empty },
{ empty, empty, empty, empty, empty, empty, empty, empty } }
{
// It should then put four o pieces on row 7, in columns 0, 2, 4, and 6.
board[7][0] = board[7][2] = board[7][4] = board[7][6] = o_piece ;
// It should put an x piece on row 0, column 3.
board[0][3] = x_piece ;
}
std::ostream& print( std::ostream& stm ) const
{
for( const auto& row : board )
{
for( char c : row ) std::cout << c << ' ' ;
std::cout << '\n' ;
}
return stm ;
}
friend std::ostream& operator<< ( std::ostream& stm, const FBoard& brd )
{ return brd.print(stm) ; }
};
int main()
{
const FBoard my_board ;
std::cout << my_board << '\n' ;
}
|