Added rotation functionality, minor cleanups on the way

This commit is contained in:
2017-11-28 21:35:49 +01:00
parent 3567b77cec
commit 2c71dd7a47
3 changed files with 61 additions and 81 deletions

View File

@ -59,6 +59,7 @@ bool Puzzle::testRotationPiece(unsigned int m, unsigned int n, PuzzlePiece& myPa
{ {
if(PlaceOfPartGood(m,n,myPart)) if(PlaceOfPartGood(m,n,myPart))
return 1; return 1;
//cout << "was rotated in testRotationPiece" << endl;
myPart.shift(1); myPart.shift(1);
} }

View File

@ -3,12 +3,10 @@ void status(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMa
bool next(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat) bool next(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat)
{ {
//status(log,p_Box,puzzleMat);
//log not yet started //log not yet started
if(!(log.size())) if(!(log.size()))
{ {
//cout << "creating new log" << endl; log.push_back(LogEntry());
log.push_back(LogEntry());
log.back().myCoor = calculateFirstCoor(log, p_Box, puzzleMat); log.back().myCoor = calculateFirstCoor(log, p_Box, puzzleMat);
solve(log, p_Box,puzzleMat); solve(log, p_Box,puzzleMat);
} }
@ -28,10 +26,7 @@ bool next(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat)
//last log element is empty, backtrack //last log element is empty, backtrack
else if(!(log.back().PieceCollector.size())) else if(!(log.back().PieceCollector.size()))
{
//cout << "backtracking" << endl;
backtrack(log,p_Box,puzzleMat); backtrack(log,p_Box,puzzleMat);
}
//case last log element has multiple entries //case last log element has multiple entries
else if(log.back().PieceCollector.size() > 1) else if(log.back().PieceCollector.size() > 1)
@ -39,14 +34,12 @@ bool next(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat)
//is not yet max abstracted //is not yet max abstracted
if(log.back().abstractionLevel < MAX_ABSTRAX) if(log.back().abstractionLevel < MAX_ABSTRAX)
{ {
//cout << "advancing abstraction layer" << endl; log.back().advance();
log.back().advance();
solve(log,p_Box,puzzleMat); solve(log,p_Box,puzzleMat);
} }
//no more layers, pick first //no more layers, pick first
else else
{ {
//cout << "setting first as solution" << endl;
log.back().advanceRandomed(); log.back().advanceRandomed();
setsolution(log,p_Box,puzzleMat); setsolution(log,p_Box,puzzleMat);
} }
@ -55,32 +48,18 @@ bool next(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat)
//case last log exactly one solution //case last log exactly one solution
else if(log.back().PieceCollector.size() == 1) else if(log.back().PieceCollector.size() == 1)
{ {
//cout << "exactly one solution" << endl;
if(log.back().hasRandomed()) if(log.back().hasRandomed())
{ {
if(log.back().abstractionLevel < MAX_ABSTRAX) if(log.back().abstractionLevel < MAX_ABSTRAX)
{ {
//cout << "advancing abstraction layer" << endl; log.back().advance();
log.back().advance();
solve(log,p_Box,puzzleMat); solve(log,p_Box,puzzleMat);
} }
else else
{ setsolution(log,p_Box,puzzleMat);
//cout << "setting as solution bec fit" << endl;
setsolution(log,p_Box,puzzleMat);
}
} }
else else
{ setsolution(log,p_Box,puzzleMat);
//cout << "setting as solution bec trivial" << endl;
setsolution(log,p_Box,puzzleMat);
}
}
//cout << "next" << endl;
if(log.back().myCoor.n>8)
{
cout << "m: " << log.back().myCoor.m << " n: " << log.back().myCoor.n << endl;
// status(log,p_Box,puzzleMat);
} }
return 1; return 1;
} }
@ -128,7 +107,6 @@ void solve(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat
default: default:
break; break;
} }
//status(log,p_Box,puzzleMat);
} }
void abstractionlayer0solver(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat) void abstractionlayer0solver(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat)
@ -146,17 +124,15 @@ void abstractionlayer1solver(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box,
(*(log.back().PieceCollector[i])).resetShift(); (*(log.back().PieceCollector[i])).resetShift();
//TODO: change checker from checking every box piece to only checking the simplifyed version ob the box with abstraction layer one //TODO: change checker from checking every box piece to only checking the simplifyed version ob the box with abstraction layer one
if(!(puzzleMat.testRotationPiece(log.back().myCoor.m, log.back().myCoor.n, *(log.back().PieceCollector[i])))) if(!(puzzleMat.testRotationPiece(log.back().myCoor.m, log.back().myCoor.n, *(log.back().PieceCollector[i]))))
{
log.back().PieceCollector.erase(log.back().PieceCollector.begin()+i); log.back().PieceCollector.erase(log.back().PieceCollector.begin()+i);
}
else else
{
i++; //otherwise loop stops before end! i++; //otherwise loop stops before end!
//set shift to 0 so that we have a defined starting position for all pieces
while(log.back().PieceCollector[i]->getShift())
log.back().PieceCollector[i]->shift(1);
}
} }
//for(int i=0;i<log.back().PieceCollector.size();i++)
//{
// (*(log.back().PieceCollector[i])).printPiece();
// cout << endl;
//}
} }
void setsolution(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat) void setsolution(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat)
@ -184,69 +160,71 @@ void setsolution(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puz
bool backtrack(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat) bool backtrack(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat)
{ {
//following possibilities: //following possibilities:
//last log entry empty //last log entry empty - delete last log + backtrack
//delete last log + backtrack
if(!(log.back().PieceCollector.size())) if(!(log.back().PieceCollector.size()))
{ {
//cout << "none" << endl;
puzzleMat.removePiece(log.back().myCoor.m, log.back().myCoor.n); puzzleMat.removePiece(log.back().myCoor.m, log.back().myCoor.n);
log.pop_back(); log.pop_back();
//status(log,p_Box,puzzleMat);
backtrack(log,p_Box,puzzleMat); backtrack(log,p_Box,puzzleMat);
return 1; return 1;
} }
//last log entry only one solution //last log entry only one solution - delete last logd put back into box + backtrack
//delete last logd put back into box + backtrack
else if((log.back().PieceCollector.size())==1) else if((log.back().PieceCollector.size())==1)
{ {
(*(log.back().PieceCollector[0])).shift(1); (log.back().PieceCollector[0])->shift(1);
if(puzzleMat.testRotationPiece(log.back().myCoor.m, log.back().myCoor.n, *(log.back().PieceCollector[0]), 4-(*(log.back().PieceCollector[0])).getShift()))
setsolution(log,p_Box,puzzleMat); while((log.back().PieceCollector[0])->getShift() !=0 && (log.back().PieceCollector[0])->getShift() !=3)
else
{ {
//cout << "one" << endl; log.back().PieceCollector[0]->shift(1);
p_Box.push_back(log.back().PieceCollector[0]); if(puzzleMat.testRotationPiece(log.back().myCoor.m, log.back().myCoor.n, *(log.back().PieceCollector[0]), 1))
//shuffleup {
random_shuffle(p_Box.begin(),p_Box.end()); setsolution(log,p_Box,puzzleMat);
puzzleMat.removePiece(log.back().myCoor.m, log.back().myCoor.n); return 1;
log.pop_back(); }
//cout << "removed" << endl;
//status(log,p_Box,puzzleMat);
backtrack(log,p_Box,puzzleMat);
} }
p_Box.push_back(log.back().PieceCollector[0]);
//shuffleup
random_shuffle(p_Box.begin(),p_Box.end());
puzzleMat.removePiece(log.back().myCoor.m, log.back().myCoor.n);
log.pop_back();
//cout << "removed" << endl;
//status(log,p_Box,puzzleMat);
backtrack(log,p_Box,puzzleMat);
return 1; return 1;
} }
//last log entry multiple solutions (and current one was randomed)
//delete randomed piece from PieceCollector and go to next (which might random again depending on function) //last log entry multiple solutions (and current one was randomed) - delete randomed piece and go to next
else if((log.back().PieceCollector.size())>1) else if((log.back().PieceCollector.size())>1)
{ {
//check if piece has second rotation solution //check if piece has second rotation solution
(*(log.back().PieceCollector[0])).shift(1); (*(log.back().PieceCollector[0])).shift(1);
if(puzzleMat.testRotationPiece(log.back().myCoor.m, log.back().myCoor.n, *(log.back().PieceCollector[0]), 4-(*(log.back().PieceCollector[0])).getShift()))
setsolution(log,p_Box,puzzleMat); while((log.back().PieceCollector[0])->getShift() !=0 && (log.back().PieceCollector[0])->getShift() !=3)
else
{ {
log.back().PieceCollector[0]->shift(1);
//cout << "multiple" << endl; if(puzzleMat.testRotationPiece(log.back().myCoor.m, log.back().myCoor.n, *(log.back().PieceCollector[0]), 1))
p_Box.push_back(log.back().PieceCollector[0]); {
//shuffleup setsolution(log,p_Box,puzzleMat);
random_shuffle(p_Box.begin(),p_Box.end()); return 1;
log.back().PieceCollector.erase(log.back().PieceCollector.begin()); }
if(log.back().PieceCollector.size()==1)
log.back().decreaseRandomed();
//for abstraction layer 1 so that first rotation solution is set.
(*(log.back().PieceCollector[0])).resetShift();
//cout << "erased first element" << endl;
//status(log,p_Box,puzzleMat);
setsolution(log,p_Box,puzzleMat);
} }
p_Box.push_back(log.back().PieceCollector[0]);
//shuffleup
random_shuffle(p_Box.begin(),p_Box.end());
log.back().PieceCollector.erase(log.back().PieceCollector.begin());
if(log.back().PieceCollector.size()==1)
log.back().decreaseRandomed();
//for abstraction layer 1 so that first rotation solution is set.
(*(log.back().PieceCollector[0])).resetShift();
setsolution(log,p_Box,puzzleMat);
return 1; return 1;
//no need to remove from puzzle mat, as sersolution overwrites it anyway //no need to remove from puzzle mat, as sersolution overwrites it anyway
} }
@ -270,7 +248,7 @@ void status(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMa
cout << "isset: 0" << endl; cout << "isset: 0" << endl;
//cout << "Abstraction: " << log[i].abstractionLevel << endl; //cout << "Abstraction: " << log[i].abstractionLevel << endl;
//cout << "m: " << log[i].myCoor.m << " n: " << log[i].myCoor.n << endl; cout << "m: " << log[i].myCoor.m << " n: " << log[i].myCoor.n << endl;
/*for(int j=0;j<log[i].PieceCollector.size();j++) /*for(int j=0;j<log[i].PieceCollector.size();j++)
{ {
(*(log[i].PieceCollector[j])).printPiece(); (*(log[i].PieceCollector[j])).printPiece();

View File

@ -9,7 +9,7 @@
int main() int main()
{ {
int cols=10, rows=10; int cols=4, rows=4;
//some basic part stuff //some basic part stuff
vector<Part> myFirstPuzzle; vector<Part> myFirstPuzzle;
Part myFirstPart; Part myFirstPart;
@ -40,7 +40,8 @@ int main()
Puzzle puzzleMat(cols, rows); Puzzle puzzleMat(cols, rows);
//vector<vector<PuzzlePiece*>> ab1class = abstractionLayer1classify(log, p_myFirstBox,puzzleMat); //vector<vector<PuzzlePiece*>> ab1class = abstractionLayer1classify(log, p_myFirstBox,puzzleMat);
for(int i=0;i<myFirstBox.size();i++)
cout << (*p_myFirstBox[i]).getShift() << endl;
while(next(log, p_myFirstBox,puzzleMat)); while(next(log, p_myFirstBox,puzzleMat));
puzzleMat.printPuzzle(); puzzleMat.printPuzzle();