Of course, the answers that offer a loop at the output location are correct. If you have to display the tic tac toe field in many different places in your application, you may prefer to encapsulate it. One possible solution is to have the tic tac toe class:
struct TicTacToe : public std::array<std::array<int, 3>, 3> { TicTacToe() :
and then define an output operator for it:
auto operator << (std::ostream& out, const TicTacToe& field) -> std::ostream& { return out << std::accumulate(std::begin(field), std::end(field), std::string{}, [](const std::string& a, const std::array<int, 3> b) -> std::string { return std::accumulate(std::begin(b), std::end(b), std::string{}, [](const std::string& a, int b) -> std::string { return std::string{b < 0 ? "O" : (b > 0 ? "X" : " ")} + (a.empty() ? "" : "|") }) + (a.empty() ? "" : "\n-+-+-\n"); }); }
Please note that I have not tested this code. It is intended to give you an idea, not a source for copy-paste.
source share