Skip to content
block.hxx 1.55 KiB
Newer Older
Seblu's avatar
Seblu committed
#ifndef BLOCK_HXX_
# define BLOCK_HXX_

Seblu's avatar
Seblu committed
inline Block::Block(int value = 0) {
Seblu's avatar
Seblu committed
  value_ = value;
  for (int i = 0; i < GRID_SIDE; ++i)
    forbidden_[i] = false;
}

Seblu's avatar
Seblu committed
inline bool Block::is_forbidden(int value) const throw (Error) {
Seblu's avatar
Seblu committed
  if (!(value > 0 && value <= GRID_SIDE))
Seblu's avatar
Seblu committed
    throw Error("Invalid \"is_forbidden\" value.");
Seblu's avatar
Seblu committed
  return forbidden_[value - 1];
}

inline bool Block::is_set() const {
  return value_ != 0;
Seblu's avatar
Seblu committed
}

Seblu's avatar
Seblu committed
inline int Block::get() const
Seblu's avatar
Seblu committed
{
  return value_;
}

Seblu's avatar
Seblu committed
inline void Block::set(int val) throw (Error)
Seblu's avatar
Seblu committed
{
Seblu's avatar
Seblu committed
  if (!(val > 0 && val <= GRID_SIDE))
Seblu's avatar
Seblu committed
    throw Error("Try to set an invalid value.", EXIT_INV_VAL);
Seblu's avatar
Seblu committed
  if (value_ != 0)
Seblu's avatar
Seblu committed
    throw Error("Try to set an already set value.");
Seblu's avatar
Seblu committed
  if (forbidden_[val - 1])
Seblu's avatar
Seblu committed
    throw Error("Try to set a forbidden value.");
Seblu's avatar
Seblu committed
  value_ = val;
Seblu's avatar
Seblu committed
  for (register int i = 0; i < GRID_SIDE; ++i)
Seblu's avatar
Seblu committed
    forbidden_[i] = true;
Seblu's avatar
Seblu committed
}

Seblu's avatar
Seblu committed
inline void Block::forbid(int val) throw (Error) {
Seblu's avatar
Seblu committed
  if (!(val > 0 && val <= GRID_SIDE))
Seblu's avatar
Seblu committed
    throw Error("Invalid forbid value.", EXIT_INV_VAL);
  if (value_ == val)
    throw Error("Try to forbid whereas case is set.", EXIT_INV_GRID);
Seblu's avatar
Seblu committed
  if (value_ != 0)
Seblu's avatar
Seblu committed
    return;
Seblu's avatar
Seblu committed
  forbidden_[val - 1] = true;
Seblu's avatar
Seblu committed
  if (forbidden_count() == GRID_SIDE)
Seblu's avatar
Seblu committed
    throw Error("All possibilities are forbidden.", EXIT_INV_GRID);
Seblu's avatar
Seblu committed
}

Seblu's avatar
Seblu committed
inline ostream &operator<<(ostream &stream, const Block &blk) {
Seblu's avatar
Seblu committed
  if (blk.value_ == 0)
    stream << " ";
  else
    stream << blk.value_;
  return stream;
}

Seblu's avatar
Seblu committed
inline int Block::forbidden_count() const
{
  int count = 0;

  for (int i = 0; i < GRID_SIDE; ++i)
    if (forbidden_[i])
      ++count;
  return count;
}

Seblu's avatar
Seblu committed
#endif