diff --git a/board.cc b/board.cc index 5427263..c0fa6d0 100644 --- a/board.cc +++ b/board.cc @@ -18,7 +18,9 @@ #include "board.h" +#include #include +#include #include Board::Board(const int size) : Board(size, BoardInitializer::EMPTY) {} @@ -60,9 +62,12 @@ int Board::at(const int row, const int column) const { } void Board::print() const { + // Define how many digits are required, at max, to print each value in this board. + const int value_width = std::floor(std::log10(size_)) + 1; + for (int row = 0; row < size_; ++row) { for (int column = 0; column < size_; ++column) { - std::cout << board_[row][column] << " "; + std::cout << std::setw(value_width) << board_[row][column] << " "; } std::cout << std::endl; } diff --git a/puzzle.cc b/puzzle.cc index a572326..302cd91 100644 --- a/puzzle.cc +++ b/puzzle.cc @@ -18,10 +18,13 @@ #include "puzzle.h" -#include "board.h" +#include #include +#include #include +#include "board.h" + template int compute_visibility(T begin, T end) { int visible_cells = 0; @@ -68,20 +71,24 @@ Puzzle::Puzzle(const Board& board) : Puzzle(board.size()) { } void Puzzle::print() const { - std::cout << " "; + // Define how many digits are required, at max, to print each value in this puzzle. + const int value_width = std::floor(std::log10(size_)) + 1; + + std::cout << std::setw(value_width) << " "; for (const int v: top_) { - std::cout << v << " "; + std::cout << std::setw(value_width) << v << " "; } std::cout << std::endl; for (int row = 0; row < size_; ++row) { - std::cout << left_[row] << " " - << right_[row] << std::endl; + std::cout << std::setw(value_width) << left_[row] + << std::setw((value_width + 1) * size_ - 1) << "" + << std::setw(value_width) << right_[row] << std::endl; } - std::cout << " "; + std::cout << std::setw(value_width) << " "; for (const int v: bottom_) { - std::cout << v << " "; + std::cout << std::setw(value_width) << v << " "; } std::cout << std::endl; }