doasku

Sudoku solver
git clone git://git.dimitrijedobrota.com/doasku.git
Log | Files | Refs

cord.hpp (1080B)


      1 #ifndef DOASKO_CORD_HPP
      2 #define DOASKO_CORD_HPP
      3 
      4 #include <cassert>
      5 #include <cinttypes>
      6 #include <utility>
      7 
      8 struct cord_t {
      9     cord_t(uint8_t value) : value(value) { assert(value < 9); }
     10     cord_t(uint8_t row, uint8_t col) : value(row * 3 + col) {
     11         assert(row < 3 && col < 3);
     12     }
     13 
     14     operator uint8_t() const { return value; }
     15 
     16     uint8_t row() const { return value / 3; }
     17     uint8_t col() const { return value % 3; }
     18 
     19     uint8_t value;
     20 };
     21 
     22 class acord_t {
     23   public:
     24     acord_t(cord_t subgrid, cord_t field)
     25         : subgrid_i(subgrid), field_i(field) {}
     26 
     27     acord_t(uint8_t row, uint8_t col)
     28         : subgrid_i(row / 3, col / 3), field_i(row % 3, col % 3) {}
     29 
     30     cord_t subgrid() const { return subgrid_i; }
     31     cord_t field() const { return field_i; }
     32 
     33     uint8_t row() const { return subgrid_i.row() * 3 + field_i.row(); }
     34     uint8_t col() const { return subgrid_i.col() * 3 + field_i.col(); }
     35 
     36     std::tuple<cord_t, cord_t> relative() const {
     37         return {subgrid_i, field_i};
     38     }
     39 
     40   private:
     41     cord_t subgrid_i;
     42     cord_t field_i;
     43 };
     44 
     45 #endif