2017-11-18 08:16:05 +01:00
|
|
|
|
2017-11-18 22:48:40 +01:00
|
|
|
bool next(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat)
|
2017-11-18 08:16:05 +01:00
|
|
|
{
|
2017-11-18 22:48:40 +01:00
|
|
|
//case first log entry empty
|
2017-11-19 17:52:02 +01:00
|
|
|
if(!(log.size()))
|
2017-11-18 22:48:40 +01:00
|
|
|
{
|
|
|
|
log.push_back(LogEntry());
|
|
|
|
solve(log, p_Box,puzzleMat);
|
|
|
|
}
|
|
|
|
|
|
|
|
//case puzzle solved
|
2017-11-19 17:52:02 +01:00
|
|
|
else if(!(p_Box.size()))
|
2017-11-18 22:48:40 +01:00
|
|
|
return 0;
|
2017-11-18 08:16:05 +01:00
|
|
|
|
2017-11-18 22:48:40 +01:00
|
|
|
//case last log multiple entries
|
|
|
|
else if(log.back().PieceCollector.size() > 1)
|
|
|
|
{
|
|
|
|
//advance abstraction layer of last log by one and solve()
|
|
|
|
//or pick first if highest level reached
|
2017-11-19 17:52:02 +01:00
|
|
|
if(log.back().abstractionLevel < MAX_ABSTRAX)
|
2017-11-18 22:48:40 +01:00
|
|
|
{
|
|
|
|
log.back().advance();
|
|
|
|
solve(log,p_Box,puzzleMat);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setsolution(log,p_Box,puzzleMat);
|
|
|
|
}
|
|
|
|
}
|
2017-11-18 08:16:05 +01:00
|
|
|
|
2017-11-18 22:48:40 +01:00
|
|
|
//case last log exactly one solution
|
|
|
|
else if(log.back().PieceCollector.size() == 1)
|
|
|
|
{
|
|
|
|
//create new log, put next coordinates in and go into solve. then git gud
|
|
|
|
log.push_back(LogEntry());
|
2017-11-19 17:52:02 +01:00
|
|
|
log.back().myCoor = calculateNextCoor(log, p_Box, puzzleMat);
|
2017-11-18 22:48:40 +01:00
|
|
|
solve(log, p_Box,puzzleMat);
|
|
|
|
}
|
2017-11-18 08:16:05 +01:00
|
|
|
|
2017-11-18 22:48:40 +01:00
|
|
|
//case last log empty
|
|
|
|
//backtrack
|
|
|
|
else if(log.back().PieceCollector.size() == 0)
|
|
|
|
{
|
|
|
|
backtrack(log,p_Box,puzzleMat);
|
|
|
|
}
|
2017-11-18 08:16:05 +01:00
|
|
|
|
2017-11-18 22:48:40 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2017-11-18 08:16:05 +01:00
|
|
|
|
|
|
|
|
2017-11-18 22:48:40 +01:00
|
|
|
coor calculateFirstCoor(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat)
|
|
|
|
{
|
|
|
|
//returns coor of first piece
|
|
|
|
coor firstCoor(0,0);
|
|
|
|
return firstCoor;
|
2017-11-18 08:16:05 +01:00
|
|
|
}
|
|
|
|
|
2017-11-18 22:48:40 +01:00
|
|
|
coor calculateNextCoor(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat)
|
2017-11-18 08:16:05 +01:00
|
|
|
{
|
2017-11-18 22:48:40 +01:00
|
|
|
//level 1:
|
|
|
|
//go left to right, then increase current row
|
2017-11-19 17:52:02 +01:00
|
|
|
int m= log.back().myCoor.m;
|
|
|
|
int n= log.back().myCoor.n;
|
|
|
|
if(m<puzzleMat.getCols()) m++;
|
|
|
|
else if(n<puzzleMat.getRows()){ m=0; n++;}
|
|
|
|
else return coor();
|
|
|
|
|
|
|
|
;
|
|
|
|
return coor(m,n);
|
2017-11-18 22:48:40 +01:00
|
|
|
//return nextCoor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void solve(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat)
|
|
|
|
{
|
2017-11-19 17:52:02 +01:00
|
|
|
switch(log.back().abstractionLevel)
|
2017-11-18 22:48:40 +01:00
|
|
|
{
|
2017-11-19 17:52:02 +01:00
|
|
|
//abstraction layer = 0
|
|
|
|
//go to abstraction layer 0 solver
|
2017-11-18 22:48:40 +01:00
|
|
|
case 0:
|
2017-11-19 17:52:02 +01:00
|
|
|
abstractionlayer0solver(log,p_Box,puzzleMat);
|
2017-11-18 22:48:40 +01:00
|
|
|
break;
|
|
|
|
|
2017-11-19 17:52:02 +01:00
|
|
|
//abstraction layer = 1
|
|
|
|
//go to abstraction layer 1 solver
|
2017-11-18 22:48:40 +01:00
|
|
|
case 1:
|
2017-11-19 17:52:02 +01:00
|
|
|
abstractionlayer1solver(log,p_Box,puzzleMat);
|
|
|
|
break;
|
|
|
|
default:
|
2017-11-18 22:48:40 +01:00
|
|
|
break;
|
|
|
|
}
|
2017-11-18 08:16:05 +01:00
|
|
|
}
|
|
|
|
|
2017-11-19 17:52:02 +01:00
|
|
|
void abstractionlayer0solver(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat)
|
2017-11-18 08:16:05 +01:00
|
|
|
{
|
2017-11-18 22:48:40 +01:00
|
|
|
//throw all remaining puzzle pieces into newest log
|
2017-11-19 17:52:02 +01:00
|
|
|
for(int i=0;i<p_Box.size();i++)
|
|
|
|
log.back().PieceCollector[i]=p_Box[i];
|
2017-11-18 08:16:05 +01:00
|
|
|
}
|
|
|
|
|
2017-11-19 17:52:02 +01:00
|
|
|
void abstractionlayer1solver(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat)
|
2017-11-18 08:16:05 +01:00
|
|
|
{
|
2017-11-19 17:52:02 +01:00
|
|
|
//remove all that do not fit according to abstraction layer 0
|
|
|
|
for(int i=0;i<log.back().PieceCollector.size();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);
|
|
|
|
}
|
2017-11-18 08:16:05 +01:00
|
|
|
}
|
|
|
|
|
2017-11-19 17:52:02 +01:00
|
|
|
void setsolution(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat)
|
2017-11-18 08:16:05 +01:00
|
|
|
{
|
2017-11-19 17:52:02 +01:00
|
|
|
//remove first element in last logelement from box
|
|
|
|
for(int i=0;i<p_Box.size();i++)
|
|
|
|
if(p_Box[i]==log.back().PieceCollector[0])
|
|
|
|
p_Box.erase(p_Box.begin()+i);
|
|
|
|
|
|
|
|
//set to this element into matrix
|
|
|
|
puzzleMat.setPiece(log.back().myCoor.m, log.back().myCoor.n, *(log.back().PieceCollector[0]));
|
2017-11-18 08:16:05 +01:00
|
|
|
}
|
|
|
|
|
2017-11-19 17:52:02 +01:00
|
|
|
bool backtrack(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat)
|
2017-11-18 08:16:05 +01:00
|
|
|
{
|
|
|
|
//following possibilities:
|
|
|
|
//last log entry empty
|
|
|
|
//delete last log + backtrack
|
2017-11-19 17:52:02 +01:00
|
|
|
|
|
|
|
if(!(log.back().PieceCollector.size()))
|
|
|
|
{
|
|
|
|
log.back().PieceCollector.pop_back();
|
|
|
|
backtrack(log,p_Box,puzzleMat);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//last log entry only one solution
|
|
|
|
//delete last logd put back into box + backtrack
|
|
|
|
else if((log.back().PieceCollector.size())==1)
|
|
|
|
{
|
|
|
|
p_Box.push_back(log.back().PieceCollector[0]);
|
|
|
|
log.back().PieceCollector.pop_back();
|
|
|
|
//TODO remove from puzzle as well!!!
|
|
|
|
backtrack(log,p_Box,puzzleMat);
|
|
|
|
return 1;
|
|
|
|
}
|
2017-11-18 08:16:05 +01:00
|
|
|
//last log entry multiple solutions (and current one was randomed)
|
2017-11-19 17:52:02 +01:00
|
|
|
//delete randomed piece from PieceCollector and go to next (which might random again depending on function)
|
|
|
|
else if((log.back().PieceCollector.size())>1)
|
|
|
|
{
|
|
|
|
p_Box.push_back(log.back().PieceCollector[0]);
|
|
|
|
log.back().PieceCollector.erase(log.back().PieceCollector.begin());
|
|
|
|
setsolution(log,p_Box,puzzleMat);
|
|
|
|
return 1;
|
|
|
|
//no need to remove from puzzle mat, as sersolution overwrites it anyway
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
|
2017-11-18 08:16:05 +01:00
|
|
|
}
|