qcross/libqnono/cnonogram.cpp
Oliver Groß 8ebd6975d7 big ugly commit
- renamed library
- added support for saving loading current game
- added (still crappy) nonogram solver
- some code cleanups
2011-12-21 12:49:33 +01:00

176 lines
5.1 KiB
C++

/***************************************************************************
* Copyright (C) 2008 by Oliver Groß *
* z.o.gross@gmx.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 "cnonogram.h"
#include <QImage>
namespace libqnono {
CNonogram::CNonogram()
: m_Size(0, 0),
m_Data(NULL),
m_Timeout(0),
m_MaximumNumberCount(0) {
}
CNonogram::CNonogram(QSize size) : m_Size(size), m_MaximumNumberCount(0) {
init();
}
CNonogram::CNonogram(QImage & image) : m_Data(NULL), m_MaximumNumberCount(0) {
loadFromImage(image);
}
CNonogram::CNonogram(QDataStream & stream) : m_Data(NULL), m_MaximumNumberCount(0) {
loadFromStream(stream);
}
CNonogram::~CNonogram() {
cleanup();
}
void CNonogram::loadFromImage(QImage & image) {
if (image.isNull())
return;
resize(image.size());
for (int iy = 0; iy < m_Size.height(); iy++)
for (int ix = 0; ix < m_Size.width(); ix++)
m_Data[ix][iy] = !(image.pixel(ix, iy) & 0x00FFFFFF);
}
void CNonogram::loadFromStream(QDataStream & stream) {
qDebug("loading picture from stream...");
stream >> m_Name; qDebug("m_Name = %s", qPrintable(m_Name));
int imageWidth, imageHeight;
stream >> imageWidth; qDebug("imageWidth = %i", imageWidth);
stream >> imageHeight; qDebug("imageHeight = %i", imageHeight);
resize(QSize(imageWidth, imageHeight));
stream >> m_Timeout;
for (int iy = 0; iy < m_Size.height(); iy++)
for (int ix = 0; ix < m_Size.width(); ix++)
stream >> m_Data[ix][iy];
}
void CNonogram::resize(QSize size) {
if (m_Data)
cleanup();
m_Size = size;
init();
}
void CNonogram::setPixel(int x, int y, bool value) {
m_Data[x][y] = value;
}
void CNonogram::updateNumbers() {
m_BlackPixels = 0;
int pixelCount;
m_MaximumNumberCount = 0;
for (int i = 0; i < m_Size.height(); i++) {
m_RowNumbers[i].clear();
pixelCount = 0;
for (int j = 0; j < m_Size.width(); j++) {
if (m_Data[j][i]) {
pixelCount++;
m_BlackPixels++;
}
else if (pixelCount) {
m_RowNumbers[i] << pixelCount;
pixelCount = 0;
}
}
if (pixelCount || m_RowNumbers[i].empty())
m_RowNumbers[i] << pixelCount;
if (m_RowNumbers[i].count() > m_MaximumNumberCount)
m_MaximumNumberCount = m_RowNumbers[i].count();
}
for (int j = 0; j < m_Size.width(); j++) {
m_ColumnNumbers[j].clear();
pixelCount = 0;
for (int i = 0; i < m_Size.height(); i++) {
if (m_Data[j][i]) {
pixelCount++;
}
else if (pixelCount) {
m_ColumnNumbers[j] << pixelCount;
pixelCount = 0;
}
}
if (pixelCount || m_ColumnNumbers[j].empty())
m_ColumnNumbers[j] << pixelCount;
if (m_ColumnNumbers[j].count() > m_MaximumNumberCount)
m_MaximumNumberCount = m_ColumnNumbers[j].count();
}
}
void CNonogram::fill(bool value) {
for (int i = 0; i < m_Size.width(); i++)
for (int j = 0; j < m_Size.height(); j++)
m_Data[i][j] = value;
}
void CNonogram::cleanup() {
delete[] m_RowNumbers;
delete[] m_ColumnNumbers;
for (int i = 0; i < m_Size.width(); i++)
delete[] m_Data[i];
delete[] m_Data;
m_Data = NULL;
}
void CNonogram::init() {
m_Data = new bool *[m_Size.width()];
for (int i = 0; i < m_Size.width(); i++)
m_Data[i] = new bool[m_Size.height()];
m_RowNumbers = new NumbersVector[m_Size.height()];
m_ColumnNumbers = new NumbersVector[m_Size.width()];
}
QDataStream & operator<<(QDataStream & stream, CNonogram & nonogram) {
stream << nonogram.name();
stream << nonogram.width();
stream << nonogram.height();
stream << nonogram.timeout();
for (int iy = 0; iy < nonogram.height(); iy++)
for (int ix = 0; ix < nonogram.width(); ix++)
stream << nonogram.pixel(ix, iy);
return stream;
}
QDataStream & operator>>(QDataStream & stream, CNonogram & nonogram) {
nonogram.loadFromStream(stream);
return stream;
}
}