?? puzzlemodel.h
字號:
#ifndef SUDOKU_PUZZLEMODEL_H
#define SUDOKU_PUZZLEMODEL_H
#include <fstream>
#include <iterator>
namespace Sudoku
{
#ifndef __GRID_SIZE
#define __GRID_SIZE 9
#endif //__GRID_SIZE
/**
* The type defination for const reference to a N * N array of <b>bool</b> type.
* This type is used for method <i>getPuzzleMask</i>.
*/
typedef const bool (&PuzzleMask)[__GRID_SIZE][__GRID_SIZE];
class PuzzleModel
{
public:
static const int N = __GRID_SIZE;
typedef unsigned char value_type;
PuzzleModel()
{
memset(m_grids, 0, sizeof(value_type) * N * N);
}
PuzzleModel(const value_type *data)
{
for (int x = 0; x < 9; ++x)
{
for (int y = 0; y < 9; ++y)
{
m_grids[x][y] = *data++;
}
}
}
value_type getValue(int x, int y) const
{
return m_grids[x][y];
}
void setValue(int x, int y, value_type value)
{
m_grids[x][y] = value;
}
void reset(PuzzleMask mask)
{
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
{
if (!mask[i][j])
m_grids[i][j] = 0;
}
}
}
bool operator==(const PuzzleModel &rhs)
{
return memcmp(m_grids, rhs.m_grids, sizeof(value_type) * N * N) == 0;
}
bool operator!=(const PuzzleModel &rhs)
{
return !(this->operator==(rhs));
}
friend std::istream& operator>>(std::istream &in, Sudoku::PuzzleModel &puzzle);
friend std::ostream& operator<<(std::ostream &out, Sudoku::PuzzleModel &puzzle);
private:
value_type m_grids[N][N];
};
inline std::istream& operator>>(std::istream &in, PuzzleModel &puzzle)
{
std::istream_iterator<PuzzleModel::value_type> it(in);
std::istream_iterator<PuzzleModel::value_type> end;
PuzzleModel::value_type *data = (PuzzleModel::value_type *)puzzle.m_grids;
memset(data, 0, sizeof(PuzzleModel::value_type) * PuzzleModel::N * PuzzleModel::N);
for (int i = 0; it != end && i < PuzzleModel::N * PuzzleModel::N; ++it, ++i)
{
*data++ = *it - '0';
}
return in;
}
inline std::ostream& operator<<(std::ostream &out, PuzzleModel &puzzle)
{
for (int i = 0; i < PuzzleModel::N; ++i)
{
for (int j = 0; j < PuzzleModel::N; ++j)
{
out << static_cast<char>(puzzle.getValue(i, j) + '0') << " ";
}
out <<std::endl;
}
//std::ostream_iterator<PuzzleModel::value_type> it(out, " ");
//PuzzleModel::value_type *data = (PuzzleModel::value_type *)puzzle.m_grids;
//std::copy(data, data + PuzzleModel::N * PuzzleModel::N, it);
return out;
}
}
#endif // SUDOKU_PUZZLEMODEL_H
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -