#include "gameview.h"
#include "puzzleitem.h"
#include "defines.h"
+#include "introitem.h"
#include <QGraphicsScene>
#include <QDateTime>
#include <QPropertyAnimation>
#include <QParallelAnimationGroup>
#include <QFont>
+#include <QMessageBox>
#include <QDebug>
hiddenIndex_ = -1;
setScene(scene_);
+ introItem_ = new IntroItem;
+ introItem_->setText("Select new game from menu to play");
+ scene_->addItem(introItem_);
+
+ verticalStep_ = 0;
+ horizontalStep_ = 0;
+
qsrand(QDateTime::currentDateTime().toTime_t());
}
}
int verticalCount = pieces_.count() / horizontalCount;
- int horizontalStep = IMAGE_WIDTH / horizontalCount + 5;
- int verticalStep = IMAGE_HEIGHT / verticalCount + 5;
+ horizontalStep_ = IMAGE_WIDTH / horizontalCount + 5;
+ verticalStep_ = IMAGE_HEIGHT / verticalCount + 5;
int pieceNumber = 0;
for(int i = 0; i < verticalCount; ++i) {
for(int j = 0; j < horizontalCount; ++j) {
scene_->addItem(pieces_.at(pieceNumber));
- QPointF point(j * horizontalStep, i * verticalStep);
+ QPointF point(j * horizontalStep_, i * verticalStep_);
pieces_.at(pieceNumber)->setPos(point);
pieces_.at(pieceNumber)->setCorrectPlace(point);
pieces_.at(pieceNumber)->setCurrentPlace(point);
}
// Wait a second
- QTimer::singleShot(1000, this, SLOT(shufflePieces()));
+ QTimer::singleShot(750, this, SLOT(shufflePieces()));
}
void GameView::shufflePieces()
return;
}
- // TODO Give pieces ramdom locations
- int rounds = 5;
+ // Give pieces ramdom locations
+ int rounds = 5; //TODO
for(int j = 0; j < rounds; ++j) {
for(int i = 0; i < pieces_.count(); ++i) {
QPointF tmp;
bool GameView::areAllPiecesOk() const
{
for(int i = 0; i < pieces_.count(); ++i) {
+ // Skip hidden piece
if(i == hiddenIndex_) {
continue;
}
+ // Id piece is not in it's place
else if(pieces_.at(i)->correctPlace() != pieces_.at(i)->currentPlace()) {
return false;
}
}
+ // Show hidden piece and move it to it's place
pieces_.at(hiddenIndex_)->show();
pieces_.at(hiddenIndex_)->moveMeTo(emptyPlace_);
+ // Set all pieces not movable
for(int i = 0; i < pieces_.count(); ++i) {
pieces_.at(i)->setMovable(false);
}
+ // Show dialog with move count
+ QMessageBox::about(const_cast<GameView *>(this), tr("You won"), QString("Puzzle completed with %1 moves").arg(PuzzleItem::moveCount()));
+
return true;
}
+
+void GameView::setMovingPieces()
+{
+ if(pieces_.isEmpty()) {
+ qDebug() << "Empty list @ GameView::setMovingPieces";
+ return;
+ }
+
+ QPointF point = QPointF();
+ for(int i = 0; i < pieces_.count(); ++i) {
+ point = pieces_.at(i)->currentPlace();
+
+ // Is piece on the left side of the empty space
+ if(emptyPlace_.y() == point.y() && point.x() + horizontalStep_ == emptyPlace_.x()) {
+ pieces_.at(i)->setMovable(true);
+ }
+
+ // Is piece on the right side of the empty space
+ else if(emptyPlace_.y() == point.y() && point.x() - horizontalStep_ == emptyPlace_.x()) {
+ pieces_.at(i)->setMovable(true);
+ }
+
+ // Is piece below the empty space
+ else if(emptyPlace_.x() == point.x() && point.y() - verticalStep_ == emptyPlace_.y()) {
+ pieces_.at(i)->setMovable(true);
+ }
+
+ // Is piece on top of the empty space
+ else if(emptyPlace_.x() == point.x() && point.y() + verticalStep_ == emptyPlace_.y()) {
+ pieces_.at(i)->setMovable(true);
+ }
+
+ // The piece is somewhere else
+ else {
+ pieces_.at(i)->setMovable(false);
+ }
+ }
+}
--- /dev/null
+#include "introitem.h"
+#include "defines.h"
+
+#include <QPainter>
+#include <QFontMetricsF>
+
+IntroItem::IntroItem(QGraphicsItem *parent) :
+ QGraphicsItem(parent)
+{
+ text_ = "";
+}
+
+QRectF IntroItem::boundingRect() const
+{
+ return QRectF(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
+}
+
+void IntroItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
+{
+ Q_UNUSED(option)
+ Q_UNUSED(widget)
+
+ painter->save();
+
+ painter->setBrush(Qt::NoBrush);
+ painter->setPen(Qt::white);
+
+ // Get font metrics
+ QFontMetricsF fontMetricsF(painter->font());
+ QRectF textRectF = fontMetricsF.boundingRect(text_);
+ int horizontalIntend = (IMAGE_WIDTH - textRectF.width()) / 2;
+ int verticalIntend = (IMAGE_HEIGHT - textRectF.height()) / 2;
+
+ // Draw text aligned to the center of boundingRect
+ painter->drawText(boundingRect()
+ .adjusted(horizontalIntend, verticalIntend,-horizontalIntend, -verticalIntend),
+ text_);
+
+ painter->restore();
+}
+
+QString IntroItem::text() const
+{
+ return text_;
+}
+
+void IntroItem::setText(const QString &txt)
+{
+ text_ = txt;
+
+ if(isVisible()) {
+ update();
+ }
+}
#include <QGraphicsSceneMouseEvent>
#include <QPropertyAnimation>
+int PuzzleItem::moveCount_ = 0;
+
PuzzleItem::PuzzleItem(QGraphicsItem *parent) :
QGraphicsPixmapItem(parent)
{
GameView::instance()->setEmptyPlace(tmp);
event->accept();
+ moveCount_++;
+
// If piece is in its place check if we won the game
+ bool won = false;
if(currentPlace() == correctPlace()) {
- GameView::instance()->areAllPiecesOk();
+ won = GameView::instance()->areAllPiecesOk();
+ }
+
+ // if we didn't win set pieces that can be moved
+ if(!won) {
+ GameView::instance()->setMovingPieces();
}
}
else {
{
event->ignore();
}
+
+int PuzzleItem::moveCount()
+{
+ return moveCount_;
+}
+
+void PuzzleItem::resetMoveCount()
+{
+ moveCount_ = 0;
+}