2 Image Puzzle - A set your pieces straight game
3 Copyright (C) 2009 Timo Härkönen
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "puzzleitem.h"
22 #include "introitem.h"
23 #include "imageimporter.h"
26 #include <QGraphicsScene>
29 #include <QPropertyAnimation>
30 #include <QParallelAnimationGroup>
32 #include <QMessageBox>
35 #include <QTextStream>
36 #include <QCloseEvent>
40 GameView *GameView::instance_ = 0;
42 GameView::GameView(QWidget *parent) :
45 scene_ = new QGraphicsScene;
49 introItem_ = new IntroItem;
50 introItem_->setText("Select new game from menu to play");
55 qsrand(QDateTime::currentDateTime().toTime_t());
57 if(QFile::exists(QString("%1/%2/%3")
58 .arg(QDir::homePath()).arg(HOME_DIRECTORY).arg(RESTORE_FILE))) {
60 setPieces(ImageImporter::instance()->newPieces(Settings::instance()->image(), Settings::instance()->pieceCount()));
64 scene_->addItem(introItem_);
68 GameView *GameView::instance()
71 instance_ = new GameView;
77 QList<PuzzleItem *> GameView::pieces() const
82 void GameView::setPieces(const QList<PuzzleItem *> pieces, bool shuffle)
84 if(pieces.isEmpty()) {
85 qDebug() << "Empty list @ GameView::setPieces";
89 QList<QGraphicsItem *> previousItems = scene_->items();
90 if(!previousItems.isEmpty()) {
91 foreach(QGraphicsItem *item, previousItems) {
92 scene_->removeItem(item);
98 int horizontalCount = 0;
100 // Find out board size
101 if(pieces_.count() == EASY_PIECE_COUNT) {
102 horizontalCount = EASY_HORIZONTAL_COUNT;
104 else if(pieces_.count() == HARD_PIECE_COUNT) {
105 horizontalCount = HARD_HORIZONTAL_COUNT;
108 qDebug() << "Invalid piece count @ GameView::setPieces";
109 qDebug() << QString("Count was %1").arg(pieces_.count());
113 int verticalCount = pieces_.count() / horizontalCount;
114 horizontalStep_ = IMAGE_WIDTH / horizontalCount + 5;
115 verticalStep_ = IMAGE_HEIGHT / verticalCount + 5;
119 // Set pieces to their correct positions
120 for(int i = 0; i < verticalCount; ++i) {
121 for(int j = 0; j < horizontalCount; ++j) {
122 scene_->addItem(pieces_.at(pieceNumber));
123 QPointF point(j * horizontalStep_, i * verticalStep_);
124 pieces_.at(pieceNumber)->setPos(point);
125 pieces_.at(pieceNumber)->setCorrectPlace(point);
126 pieces_.at(pieceNumber)->setCurrentPlace(point);
127 pieces_.at(pieceNumber)->setDrawNumber(true);
132 // Wait and shuffle if desired
134 QTimer::singleShot(750, this, SLOT(shufflePieces()));
138 void GameView::shufflePieces()
140 if(pieces_.isEmpty()) {
141 qDebug() << "Empty list @ GameView::shufflePieces";
145 // Give pieces ramdom locations
146 int rounds = 5; //TODO
147 for(int j = 0; j < rounds; ++j) {
148 for(int i = 0; i < pieces_.count(); ++i) {
151 while(changeIndex == i) {
152 changeIndex = qrand() % pieces_.count();
154 tmp = pieces_.at(changeIndex)->currentPlace();
155 pieces_.at(changeIndex)->setCurrentPlace(pieces_.at(i)->currentPlace());
156 pieces_.at(i)->setCurrentPlace(tmp);
160 QParallelAnimationGroup *animationGroup = new QParallelAnimationGroup(this);
161 for(int i = 0; i < pieces_.count(); ++i) {
162 QPropertyAnimation *animation = new QPropertyAnimation(pieces_.at(i), "pos");
163 animation->setStartValue(pieces_.at(i)->correctPlace());
164 animation->setEndValue(pieces_.at(i)->currentPlace());
165 animation->setDuration(750);
166 animation->setEasingCurve(QEasingCurve::InOutCirc);
167 animationGroup->addAnimation(animation);
169 animationGroup->start();
172 int hiddenPiece = qrand() % pieces_.count();
173 emptyPlace_ = pieces_.at(hiddenPiece)->currentPlace();
174 pieces_.at(hiddenPiece)->hide();
175 hiddenIndex_ = hiddenPiece;
180 QPointF GameView::emptyPlace()
185 void GameView::setEmptyPlace(const QPointF &place)
190 bool GameView::areAllPiecesOk() const
192 for(int i = 0; i < pieces_.count(); ++i) {
194 if(i == hiddenIndex_) {
197 // Id piece is not in it's place
198 else if(pieces_.at(i)->correctPlace() != pieces_.at(i)->currentPlace()) {
202 // Show hidden piece and move it to it's place
203 pieces_.at(hiddenIndex_)->show();
204 pieces_.at(hiddenIndex_)->moveMeTo(emptyPlace_);
206 // Set all pieces not movable and hide numbers
207 for(int i = 0; i < pieces_.count(); ++i) {
208 pieces_.at(i)->setMovable(false);
209 pieces_.at(i)->setDrawNumber(false);
212 // Show dialog with move count
213 QMessageBox::about(const_cast<GameView *>(this), tr("You won"), QString("Puzzle completed with %1 moves").arg(PuzzleItem::moveCount()));
218 void GameView::setMovingPieces()
220 if(pieces_.isEmpty()) {
221 qDebug() << "Empty list @ GameView::setMovingPieces";
225 QPointF point = QPointF();
226 for(int i = 0; i < pieces_.count(); ++i) {
227 point = pieces_.at(i)->currentPlace();
229 // Is piece on the left side of the empty space
230 if(emptyPlace_.y() == point.y() && point.x() + horizontalStep_ == emptyPlace_.x()) {
231 pieces_.at(i)->setMovable(true);
234 // Is piece on the right side of the empty space
235 else if(emptyPlace_.y() == point.y() && point.x() - horizontalStep_ == emptyPlace_.x()) {
236 pieces_.at(i)->setMovable(true);
239 // Is piece below the empty space
240 else if(emptyPlace_.x() == point.x() && point.y() - verticalStep_ == emptyPlace_.y()) {
241 pieces_.at(i)->setMovable(true);
244 // Is piece on top of the empty space
245 else if(emptyPlace_.x() == point.x() && point.y() + verticalStep_ == emptyPlace_.y()) {
246 pieces_.at(i)->setMovable(true);
249 // The piece is somewhere else
251 pieces_.at(i)->setMovable(false);
256 bool GameView::restoreGame()
258 // Read settings from file
259 QFile file(QString("%1/%2/%3")
260 .arg(QDir::homePath())
264 if(!file.open(QIODevice::ReadOnly)) {
265 qDebug() << "Failed to open restore file for reading";
269 QTextStream in(&file);
273 list = in.readLine().split(";;");
275 qDebug() << "restore list count: " << list.count();
277 if(!list.isEmpty()) {
278 Settings::instance()->setPieceCount(list.at(0).toInt());
280 QString im = list.at(1);
281 if(im == "default" || im.isEmpty()) {
282 Settings::instance()->setImage(0);
283 Settings::instance()->setImagePath("default");
286 Settings::instance()->setImagePath(im);
287 Settings::instance()->setImage(QPixmap(im));
289 PuzzleItem::setMoveCount(list.at(2).toInt());
291 setPieces(ImageImporter::instance()->newPieces(Settings::instance()->image(), Settings::instance()->pieceCount()), false);
293 qDebug() << "pieces_ count after restoring image: " << pieces_.count();
295 if(list.count() >= pieces_.count() + 3) {
296 for(int j = 0; j < pieces_.count(); ++j) {
297 if(!list.at(j + 3).isNull()) {
298 QStringList points = list.at(j + 3).split("#");
299 //if(points.count() == 2)
300 QPointF point(points.at(0).toInt(), points.at(1).toInt());
302 qDebug() << "Setting piece " << pieces_.at(j)->pieceNumber();
303 qDebug() << "x: " << point.x() << " y: " << point.y();
305 pieces_.at(j)->setCurrentPlace(point);
315 QStringList hidden = list.last().split("#");
317 if(hidden.count() == 3) {
318 for(int m = 0; m < pieces_.count(); ++m) {
319 pieces_.at(m)->setPos(pieces_.at(m)->currentPlace());
320 if(pieces_.at(m)->pieceNumber() == hidden.at(2).toInt()) {
321 qDebug() << "Hiding piece number " << hidden;
326 setEmptyPlace(QPointF(hidden.at(0).toInt(), hidden.at(1).toInt()));
328 pieces_.at(hiddenIndex_)->setVisible(false);
334 setPieces(ImageImporter::instance()->newPieces(Settings::instance()->image(), Settings::instance()->pieceCount()));
341 qDebug() << "Invalid restore file";
353 bool GameView::saveGame()
355 if(pieces_.isEmpty() || pieces_.count() < EASY_PIECE_COUNT) {
360 if(!dir.exists(QString("%1/%2")
361 .arg(QDir::homePath())
362 .arg(HOME_DIRECTORY))) {
363 dir.mkpath(QString("%1/%2")
364 .arg(QDir::homePath())
365 .arg(HOME_DIRECTORY));
368 QFile file(QString("%1/%2/%3")
369 .arg(QDir::homePath())
373 if(!file.open(QIODevice::WriteOnly)) {
374 qDebug() << "Failed to open restore file for writing";
378 QTextStream out(&file);
380 out << Settings::instance()->pieceCount();
382 if(Settings::instance()->imagePath().isEmpty()) {
386 out << Settings::instance()->imagePath();
389 out << PuzzleItem::moveCount();
396 while(number != pieces_.count()) {
397 for(int i = 0; i < pieces_.count(); ++i) {
398 if(pieces_.at(i)->pieceNumber() == number + 1) {
399 out << pieces_.at(i)->currentPlace().x();
401 out << pieces_.at(i)->currentPlace().y();
403 pieces_.at(i)->pieceNumber();
404 if(!pieces_.at(i)->isVisible()) {
405 hiddenNo = number + 1;
413 out << QString("%1#%2#%3").arg(emptyPlace().x()).arg(emptyPlace().y()).arg(hiddenNo);
422 void GameView::closeEvent(QCloseEvent *event)
424 int answer = QMessageBox::question(this, tr("Save game status?"),
425 tr("Saved status will be automatically loaded when you start the application next time"),
426 QMessageBox::Yes, QMessageBox::No);
428 if(answer == QMessageBox::Yes) {