Skip to content

Commit

Permalink
Better spacing on printout
Browse files Browse the repository at this point in the history
  • Loading branch information
dark committed Jan 29, 2024
1 parent 793b422 commit 34584a9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
7 changes: 6 additions & 1 deletion board.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

#include "board.h"

#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>

Board::Board(const int size) : Board(size, BoardInitializer::EMPTY) {}
Expand Down Expand Up @@ -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;
}
Expand Down
21 changes: 14 additions & 7 deletions puzzle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

#include "puzzle.h"

#include "board.h"
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>

#include "board.h"

template <typename T>
int compute_visibility(T begin, T end) {
int visible_cells = 0;
Expand Down Expand Up @@ -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;
}

0 comments on commit 34584a9

Please sign in to comment.