Of course the answers, that propose a loop at the place of output are correct. If you happen to have to output your tic tac toe field at many different places in your application, you might prefer to encapsulate that. One possible solution is to have a tic tac toe class:
struct TicTacToe : public std::array<std::array<int, 3>, 3> {
TicTacToe() :
// not sure, if the initialization will work like that
std::array<std::array<int, 3>, 3>{{0,0,0},{0,0,0},{0,0,0}}
{};
};
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 meant to give you an idea, not a source for copy-paste.
tab[i]. Now ask yourself how do I print that type.string tab[5][5]tostring tab[25]and print thestd::endlafter each 5th value.