- int rounds = 5; //TODO
- for(int j = 0; j < rounds; ++j) {
- for(int i = 0; i < pieces_.count(); ++i) {
- QPointF tmp;
- int changeIndex = 0;
- while(changeIndex == i) {
- changeIndex = qrand() % pieces_.count();
+ hiddenIndex_ = qrand() % pieces_.count();
+ emptyPlace_ = pieces_.at(hiddenIndex_)->currentPlace();
+
+ QPointF topLeft = pieces_.at(0)->correctPlace();
+ QPointF bottomRight = pieces_.last()->correctPlace();
+
+ for(int i = 0; i < pieces_.count() * 10; ++i) {
+ int rand = qrand() % 4;
+
+ switch(rand) {
+ // up
+ case 0:
+ if(pieces_.at(hiddenIndex_)->currentPlace().y() > topLeft.y()) {
+ QPointF tmp = pieces_.at(hiddenIndex_)->currentPlace();
+ PuzzleItem *item = dynamic_cast<PuzzleItem *>(scene()->itemAt(tmp + QPointF(0, -verticalStep_)));
+ emptyPlace_ = item->currentPlace();
+ pieces_.at(hiddenIndex_)->setCurrentPlace(item->currentPlace());
+ item->setCurrentPlace(tmp);
+ }
+ break;
+ // down
+ case 1:
+ if(pieces_.at(hiddenIndex_)->currentPlace().y() < bottomRight.y()) {
+ QPointF tmp = pieces_.at(hiddenIndex_)->currentPlace();
+ PuzzleItem *item = dynamic_cast<PuzzleItem *>(scene()->itemAt(tmp + QPointF(0, verticalStep_)));
+ emptyPlace_ = item->currentPlace();
+ pieces_.at(hiddenIndex_)->setCurrentPlace(item->currentPlace());
+ item->setCurrentPlace(tmp);
+ }
+ break;
+ // left
+ case 2:
+ if(pieces_.at(hiddenIndex_)->currentPlace().x() > topLeft.x()) {
+ QPointF tmp = pieces_.at(hiddenIndex_)->currentPlace();
+ PuzzleItem *item = dynamic_cast<PuzzleItem *>(scene()->itemAt(tmp + QPointF(-horizontalStep_, 0)));
+ emptyPlace_ = item->currentPlace();
+ pieces_.at(hiddenIndex_)->setCurrentPlace(item->currentPlace());
+ item->setCurrentPlace(tmp);