Skip to content

Commit

Permalink
Support printout to custom streams
Browse files Browse the repository at this point in the history
  • Loading branch information
dark committed Jan 29, 2024
1 parent 34584a9 commit 3d05daf
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
6 changes: 3 additions & 3 deletions board.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
6 changes: 4 additions & 2 deletions board.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
#ifndef BOARD_H
#define BOARD_H

#include <iostream>
#include <vector>

#include "board_iterators.h"

// Defines various ways to initialize a new board.
Expand Down Expand Up @@ -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.
Expand Down
20 changes: 17 additions & 3 deletions create.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,31 @@
#include "create.h"

#include <cstdlib>
#include <fstream>

#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;
}
20 changes: 10 additions & 10 deletions puzzle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 4 additions & 2 deletions puzzle.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#ifndef PUZZLE_H
#define PUZZLE_H

#include <iostream>

#include "board.h"

class Puzzle {
Expand All @@ -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_;
Expand Down

0 comments on commit 3d05daf

Please sign in to comment.