Loading Makefile.am +2 −1 Original line number Original line Diff line number Diff line Loading @@ -2,7 +2,8 @@ bin_PROGRAMS=sudoku sudoku_SOURCES= src/sudoku.hh \ sudoku_SOURCES= src/sudoku.hh \ src/sudoku.cc \ src/sudoku.cc \ src/grid.cc src/grid.cc \ src/block.hxx CLEANFILES= *~ '\#*' CLEANFILES= *~ '\#*' Loading src/block.hxx 0 → 100644 +45 −0 Original line number Original line Diff line number Diff line #ifndef BLOCK_HXX_ # define BLOCK_HXX_ inline Block::Block() { Block(0); } inline Block::Block(int value) { value_ = value; for (int i = 0; i < GRID_SIDE; ++i) forbidden_[i] = false; } inline bool Block::is_forbidden(int value) const { assert(value > 0 && value <= GRID_SIDE); return forbidden_[value]; } inline int Block::value_get() const { return value_; } inline void Block::set(int val) { assert(val > 0 && val <= GRID_SIDE); value_ = val; } inline void Block::forbid(int val) { assert(val > 0 && val <= GRID_SIDE); if (val != value_) forbidden_[val - 1] = true; } inline std::ostream &operator<<(std::ostream &stream, const Block &blk) { if (blk.value_ == 0) stream << " "; else stream << blk.value_; return stream; } #endif src/grid.cc +4 −4 Original line number Original line Diff line number Diff line Loading @@ -14,15 +14,15 @@ int Grid::load(const char *filename) fs.open(filename, std::fstream::in); fs.open(filename, std::fstream::in); if (!fs.is_open()) if (!fs.is_open()) return 0; return 0; for (int i = 0; i < GRID_SIDE; ++i) for (int y = 0; y < GRID_SIDE; ++y) for (int j = 0; j < GRID_SIDE; ++j) { for (int x = 0; x < GRID_SIDE; ++x) { fs >> std::dec >> val; fs >> std::dec >> val; if (val < 0 || val > 9) { if (val < 0 || val > 9) { std::cerr << "Invalid value in file: " << val << std::endl; std::cerr << "Invalid value in file: " << val << std::endl; exit(EXIT_LOADFAIL); exit(EXIT_LOADFAIL); } } if (val > 0) if (val > 0) pose(i, j, val); pose(x, y, val); //block_[i][j].set(val); //block_[i][j].set(val); } } fs.close(); fs.close(); Loading Loading @@ -71,7 +71,7 @@ void Grid::print() const for (k = l * 3 + 1; k <= l * 3 + 3; ++k) for (k = l * 3 + 1; k <= l * 3 + 3; ++k) if (block_[i][j].value_get() == k) if (block_[i][j].value_get() == k) std::cout << "\033[0;32m" << k << "\033[0m"; std::cout << "\033[0;32m" << k << "\033[0m"; else if (block_[i][j].is_forbid(k)) else if (block_[i][j].is_forbidden(k)) std::cout << "\033[0;31m" << k << "\033[0m"; std::cout << "\033[0;31m" << k << "\033[0m"; else else std::cout << k; std::cout << k; Loading src/sudoku.cc +0 −7 Original line number Original line Diff line number Diff line Loading @@ -30,10 +30,3 @@ int main(int argc, char *argv[]) return found; return found; } } std::ostream &operator<<(std::ostream &stream, const Block &blk) { if (blk.value_ == 0) stream << " "; else stream << blk.value_; return stream; } src/sudoku.hh +9 −23 Original line number Original line Diff line number Diff line Loading @@ -21,29 +21,13 @@ enum { class Block class Block { { public: public: Block() { Block(0); } inline Block(); Block(int value) { inline Block(int val); value_ = value; inline bool is_forbidden(int val) const; for (int i = 0; i < GRID_SIDE; ++i) inline int value_get() const; forbidden_[i] = false; inline void set(int val); } inline void forbid(int val); bool is_forbidden(int value) const { inline friend std::ostream &operator<<(std::ostream &stream, const Block &blk); assert(value > 0 && value <= GRID_SIDE); return forbidden_[value]; } int value_get() const { return value_; } bool is_forbid(int v) const { assert(v > 0 && v <= GRID_SIDE); return forbidden_[v - 1]; } void init(int val) { assert(val >= 0 && val <= GRID_SIDE); value_ = val; } void set(int val) { assert(val > 0 && val <= GRID_SIDE); value_ = val; } void forbid(int val) { assert(val > 0 && val <= GRID_SIDE); if (val != value_) forbidden_[val] = true; } friend std::ostream &operator<<(std::ostream &stream, const Block &blk); private: private: int value_; int value_; bool forbidden_[GRID_SIDE]; bool forbidden_[GRID_SIDE]; Loading Loading @@ -76,4 +60,6 @@ void Grid::print_line() const } } } } #include "block.hxx" #endif #endif Loading
Makefile.am +2 −1 Original line number Original line Diff line number Diff line Loading @@ -2,7 +2,8 @@ bin_PROGRAMS=sudoku sudoku_SOURCES= src/sudoku.hh \ sudoku_SOURCES= src/sudoku.hh \ src/sudoku.cc \ src/sudoku.cc \ src/grid.cc src/grid.cc \ src/block.hxx CLEANFILES= *~ '\#*' CLEANFILES= *~ '\#*' Loading
src/block.hxx 0 → 100644 +45 −0 Original line number Original line Diff line number Diff line #ifndef BLOCK_HXX_ # define BLOCK_HXX_ inline Block::Block() { Block(0); } inline Block::Block(int value) { value_ = value; for (int i = 0; i < GRID_SIDE; ++i) forbidden_[i] = false; } inline bool Block::is_forbidden(int value) const { assert(value > 0 && value <= GRID_SIDE); return forbidden_[value]; } inline int Block::value_get() const { return value_; } inline void Block::set(int val) { assert(val > 0 && val <= GRID_SIDE); value_ = val; } inline void Block::forbid(int val) { assert(val > 0 && val <= GRID_SIDE); if (val != value_) forbidden_[val - 1] = true; } inline std::ostream &operator<<(std::ostream &stream, const Block &blk) { if (blk.value_ == 0) stream << " "; else stream << blk.value_; return stream; } #endif
src/grid.cc +4 −4 Original line number Original line Diff line number Diff line Loading @@ -14,15 +14,15 @@ int Grid::load(const char *filename) fs.open(filename, std::fstream::in); fs.open(filename, std::fstream::in); if (!fs.is_open()) if (!fs.is_open()) return 0; return 0; for (int i = 0; i < GRID_SIDE; ++i) for (int y = 0; y < GRID_SIDE; ++y) for (int j = 0; j < GRID_SIDE; ++j) { for (int x = 0; x < GRID_SIDE; ++x) { fs >> std::dec >> val; fs >> std::dec >> val; if (val < 0 || val > 9) { if (val < 0 || val > 9) { std::cerr << "Invalid value in file: " << val << std::endl; std::cerr << "Invalid value in file: " << val << std::endl; exit(EXIT_LOADFAIL); exit(EXIT_LOADFAIL); } } if (val > 0) if (val > 0) pose(i, j, val); pose(x, y, val); //block_[i][j].set(val); //block_[i][j].set(val); } } fs.close(); fs.close(); Loading Loading @@ -71,7 +71,7 @@ void Grid::print() const for (k = l * 3 + 1; k <= l * 3 + 3; ++k) for (k = l * 3 + 1; k <= l * 3 + 3; ++k) if (block_[i][j].value_get() == k) if (block_[i][j].value_get() == k) std::cout << "\033[0;32m" << k << "\033[0m"; std::cout << "\033[0;32m" << k << "\033[0m"; else if (block_[i][j].is_forbid(k)) else if (block_[i][j].is_forbidden(k)) std::cout << "\033[0;31m" << k << "\033[0m"; std::cout << "\033[0;31m" << k << "\033[0m"; else else std::cout << k; std::cout << k; Loading
src/sudoku.cc +0 −7 Original line number Original line Diff line number Diff line Loading @@ -30,10 +30,3 @@ int main(int argc, char *argv[]) return found; return found; } } std::ostream &operator<<(std::ostream &stream, const Block &blk) { if (blk.value_ == 0) stream << " "; else stream << blk.value_; return stream; }
src/sudoku.hh +9 −23 Original line number Original line Diff line number Diff line Loading @@ -21,29 +21,13 @@ enum { class Block class Block { { public: public: Block() { Block(0); } inline Block(); Block(int value) { inline Block(int val); value_ = value; inline bool is_forbidden(int val) const; for (int i = 0; i < GRID_SIDE; ++i) inline int value_get() const; forbidden_[i] = false; inline void set(int val); } inline void forbid(int val); bool is_forbidden(int value) const { inline friend std::ostream &operator<<(std::ostream &stream, const Block &blk); assert(value > 0 && value <= GRID_SIDE); return forbidden_[value]; } int value_get() const { return value_; } bool is_forbid(int v) const { assert(v > 0 && v <= GRID_SIDE); return forbidden_[v - 1]; } void init(int val) { assert(val >= 0 && val <= GRID_SIDE); value_ = val; } void set(int val) { assert(val > 0 && val <= GRID_SIDE); value_ = val; } void forbid(int val) { assert(val > 0 && val <= GRID_SIDE); if (val != value_) forbidden_[val] = true; } friend std::ostream &operator<<(std::ostream &stream, const Block &blk); private: private: int value_; int value_; bool forbidden_[GRID_SIDE]; bool forbidden_[GRID_SIDE]; Loading Loading @@ -76,4 +60,6 @@ void Grid::print_line() const } } } } #include "block.hxx" #endif #endif