-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJigsawPuzzleRotations.cpp
More file actions
59 lines (53 loc) · 1.77 KB
/
JigsawPuzzleRotations.cpp
File metadata and controls
59 lines (53 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
// Created by okleinfeld on 12/13/17.
//
#include "JigsawPuzzleRotations.h"
JigsawPuzzleRotations::JigsawPuzzleRotations(vector<PuzzlePiece> pieces) : JigsawPuzzle(){
this->puzzlePieces = pieces;
this->piecesMap = PuzzlePiecesMapWithRotate(this->puzzlePieces);
this->lastColIndex = -1;
this->lastRowIndex = -1;
this->numPieces = (int) this->puzzlePieces.size();
this->puzzleSolvedSuccessfully = false;
}
bool JigsawPuzzleRotations::solveRec(pair<int,int> nextPos){
int i = nextPos.first;
int j = nextPos.second;
if (i ==-1 || j ==-1){
return true;
}
PuzzleRequirement req = getReq(i, j);
PuzzlePiece* p = this->piecesMap.nextPiece(req);
while(p!=nullptr){
int pISD = p->getISD();
this->solutionMatrix[i][j] = pISD;
this->puzzlePieces[pISD-1] = *p; // copy c'tor
if(solveRec(getNextPos(i, j)))
return true;
else{
this->solutionMatrix[i][j] = -1;
p->setUsed(false);
req.addFalseType(PuzzleType(p->getLeftEdge(), p->getTopEdge(), p->getRightEdge(), p->getBottomEdge()));
p->resetAngle();
this->puzzlePieces[pISD-1] = *p;
p = this->piecesMap.nextPiece(req);
}
}
return false;
}
bool JigsawPuzzleRotations::solveGame(){
vector<pair<int,int> > possibleDimensions = getPossibleDimensions();
pair<int,int> topLeftCorner = pair<int,int>(0,0);
for (auto& p : possibleDimensions){
this->lastRowIndex = p.first-1;
this->lastColIndex = p.second-1;
initSolMatrix();
if(solveRec(topLeftCorner) == true){
this->puzzleSolvedSuccessfully = true;
return true;
}
}
this->lastRowIndex = -1;
this->lastColIndex = -1;
return false;
}