Skip to content

Commit

Permalink
Skeleton and phase 1 of puzzle generation from a board
Browse files Browse the repository at this point in the history
Only left and right vectors are computed for now.
  • Loading branch information
dark committed Jan 28, 2024
1 parent 5a8d343 commit 15f12d1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion board.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Board {
Board(const int size, const BoardInitializer initializer);

// Retrieves the board size.
int size() const;
int size() const { return size_; }

// Returns the entry at the specified row and column. This could be
// zero if the cell was never written to.
Expand Down
2 changes: 1 addition & 1 deletion main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int main(void) {
Board b{5, BoardInitializer::DIAGONAL_INCREASING};
b.print();

Puzzle p{5};
Puzzle p{b};
p.print();

return 0;
Expand Down
35 changes: 35 additions & 0 deletions puzzle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,25 @@

#include "puzzle.h"

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

template <typename T>
int compute_visibility(T begin, T end) {
int visible_cells = 0;
int highest_value = 0;

for (auto i = begin; i != end; ++i) {
if (*i > highest_value) {
highest_value = *i;
++visible_cells;
}
}

return visible_cells;
}

Puzzle::Puzzle(const int size)
: size_(size) {
if (size_ <= 0) {
Expand All @@ -34,6 +50,25 @@ Puzzle::Puzzle(const int size)
right_.resize(size_);
}

Puzzle::Puzzle(const Board& board) : Puzzle(board.size()) {
#if 0
// Fill the top and bottom vectors
for (int column = 0; column < size_; ++column) {
top_[column] = compute_visibility(board.column_cbegin(column),
board.column_cend(column));
bottom_[column] = compute_visibility(board.column_crbegin(column),
board.column_crend(column));
}
#endif
// Fill the left and right vectors
for (int row = 0; row < size_; ++row) {
left_[row] = compute_visibility(board.row_cbegin(row),
board.row_cend(row));
right_[row] = compute_visibility(board.row_crbegin(row),
board.row_crend(row));
}
}

void Puzzle::print() const {
std::cout << " ";
for (const int v: top_) {
Expand Down
2 changes: 1 addition & 1 deletion puzzle.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Puzzle {
explicit Puzzle(const int size);

// Creates a puzzle based on an existing, solved board.
explicit Puzzle(const Board &board);
explicit Puzzle(const Board& board);

// Prints the puzzle to standard output.
void print() const;
Expand Down

0 comments on commit 15f12d1

Please sign in to comment.