qcross/libqnono/nonogramproblem.cpp

90 lines
3.3 KiB
C++
Raw Normal View History

/***************************************************************************
* Copyright (C) 2012 Stefan Bühler <stbuehler@web.de> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "nonogramproblem.h"
#include "nonogramsolver.h"
namespace libqnono {
static const quint64 NonogramProblem_MAGIC = Q_UINT64_C(0xfdbbd8d0936ea9a9);
NonogramProblem::NonogramProblem() {
}
NonogramProblem::NonogramProblem(const NonogramImage & solution)
: m_solution(solution), m_numbers(solution), m_name(), m_timeout(0) {
}
NonogramProblem::NonogramProblem(QString name, quint16 timeout, const NonogramImage & solution)
: m_solution(solution), m_numbers(solution), m_name(name), m_timeout(timeout) {
}
bool NonogramProblem::hasUniqueSolution() const {
return 1 == solve(m_numbers).count();
}
void NonogramProblem::setPixel(int x, int y, bool value) {
m_solution.setPixel(x, y, value);
m_numbers.updateFromImage(m_solution, x, y);
}
void NonogramProblem::fill(bool value) {
m_solution.fill(value);
m_numbers.calcFromImage(m_solution);
}
void NonogramProblem::loadFromImage(const NonogramImage & solution) {
m_solution = solution;
m_numbers.calcFromImage(m_solution);
}
bool NonogramProblem::readFromStream(QDataStream & stream) {
quint64 magic;
stream >> magic;
if (NonogramProblem_MAGIC != magic || QDataStream::Ok != stream.status()) {
if (QDataStream::ReadPastEnd != stream.status()) stream.setStatus(QDataStream::ReadCorruptData);
return false;
}
QString name;
quint16 timeout;
NonogramImage solution;
stream >> name >> timeout >> solution;
if (QDataStream::Ok != stream.status()) return false;
m_solution = solution;
m_name = name;
m_timeout = timeout;
m_numbers.calcFromImage(m_solution);
return true;
}
void NonogramProblem::writeToStream(QDataStream & stream) const {
stream << NonogramProblem_MAGIC << m_name << m_timeout << m_solution;
}
QDataStream & operator<<(QDataStream & stream, const NonogramProblem & problem) {
problem.writeToStream(stream);
return stream;
}
QDataStream & operator>>(QDataStream & stream, NonogramProblem & problem) {
problem.readFromStream(stream);
return stream;
}
}