⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 puzzlemodel.h

📁 要谈论的不是数独游戏的算法
💻 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 + -