From 3d05daf05fcf8787c02dd4a4f50b51838557e273 Mon Sep 17 00:00:00 2001 From: Marco Leogrande Date: Sun, 28 Jan 2024 17:08:02 -0800 Subject: [PATCH] Support printout to custom streams --- board.cc | 6 +++--- board.h | 6 ++++-- create.cc | 20 +++++++++++++++++--- puzzle.cc | 20 ++++++++++---------- puzzle.h | 6 ++++-- 5 files changed, 38 insertions(+), 20 deletions(-) diff --git a/board.cc b/board.cc index c0fa6d0..ba418ad 100644 --- a/board.cc +++ b/board.cc @@ -61,15 +61,15 @@ int Board::at(const int row, const int column) const { return board_[row][column]; } -void Board::print() const { +void Board::print(std::ostream &ostream) 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 << std::setw(value_width) << board_[row][column] << " "; + ostream << std::setw(value_width) << board_[row][column] << " "; } - std::cout << std::endl; + ostream << std::endl; } } diff --git a/board.h b/board.h index 1278117..1c079cc 100644 --- a/board.h +++ b/board.h @@ -19,7 +19,9 @@ #ifndef BOARD_H #define BOARD_H +#include #include + #include "board_iterators.h" // Defines various ways to initialize a new board. @@ -65,8 +67,8 @@ class Board { // is invalid, returns false. bool is_column_valid(const int column) const; - // Prints the board to standard output. - void print() const; + // Prints the board to the provided output stream. + void print(std::ostream &ostream) const; // Swaps two rows by index, if both indices are valid, and returns // true. Otherwise, does nothing and returns false. diff --git a/create.cc b/create.cc index 9cb9522..d3f2d85 100644 --- a/create.cc +++ b/create.cc @@ -19,17 +19,31 @@ #include "create.h" #include +#include #include "board.h" #include "options.h" #include "puzzle.h" int CreateBoard(const ProgramOptions& options) { - Board b{5, BoardInitializer::DIAGONAL_INCREASING}; - b.print(); + // Create a board. + Board b{options.board_size, BoardInitializer::DIAGONAL_INCREASING}; + // Shuffle randomly based on options + // FIXME + + // Print board to the desired location. + { + std::ofstream out{options.board_output_file, std::ios::out}; + b.print(out); + } + + // Genearte puzzle and print to the desired location. Puzzle p{b}; - p.print(); + { + std::ofstream out{options.puzzle_output_file, std::ios::out}; + p.print(out); + } return EXIT_SUCCESS; } diff --git a/puzzle.cc b/puzzle.cc index 302cd91..55e56fa 100644 --- a/puzzle.cc +++ b/puzzle.cc @@ -70,25 +70,25 @@ Puzzle::Puzzle(const Board& board) : Puzzle(board.size()) { } } -void Puzzle::print() const { +void Puzzle::print(std::ostream &ostream) const { // 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) << " "; + ostream << std::setw(value_width) << " "; for (const int v: top_) { - std::cout << std::setw(value_width) << v << " "; + ostream << std::setw(value_width) << v << " "; } - std::cout << std::endl; + ostream << std::endl; for (int row = 0; row < size_; ++row) { - std::cout << std::setw(value_width) << left_[row] - << std::setw((value_width + 1) * size_ - 1) << "" - << std::setw(value_width) << right_[row] << std::endl; + ostream << std::setw(value_width) << left_[row] + << std::setw((value_width + 1) * size_ - 1) << "" + << std::setw(value_width) << right_[row] << std::endl; } - std::cout << std::setw(value_width) << " "; + ostream << std::setw(value_width) << " "; for (const int v: bottom_) { - std::cout << std::setw(value_width) << v << " "; + ostream << std::setw(value_width) << v << " "; } - std::cout << std::endl; + ostream << std::endl; } diff --git a/puzzle.h b/puzzle.h index 1057a8f..54705d4 100644 --- a/puzzle.h +++ b/puzzle.h @@ -19,6 +19,8 @@ #ifndef PUZZLE_H #define PUZZLE_H +#include + #include "board.h" class Puzzle { @@ -29,8 +31,8 @@ class Puzzle { // Creates a puzzle based on an existing, solved board. explicit Puzzle(const Board& board); - // Prints the puzzle to standard output. - void print() const; + // Prints the puzzle to the provided output stream. + void print(std::ostream &ostream) const; private: const int size_;