added more strucuture to strucutur.cpp. commiting now to extract new debugging branch
This commit is contained in:
		| @@ -2,14 +2,14 @@ | ||||
| bool next(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat) | ||||
| { | ||||
|     //case first log entry empty | ||||
|     if(!(log.size()) | ||||
|     if(!(log.size())) | ||||
|     {    | ||||
|         log.push_back(LogEntry()); | ||||
|         solve(log, p_Box,puzzleMat);  | ||||
|     } | ||||
|  | ||||
| 	//case puzzle solved | ||||
|     else if(!(p_Box.size()) | ||||
|     else if(!(p_Box.size())) | ||||
|         return 0; | ||||
|  | ||||
|     //case last log multiple entries | ||||
| @@ -17,7 +17,7 @@ bool next(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat) | ||||
|     { | ||||
|         //advance abstraction layer of last log by one and solve() | ||||
|         //or pick first if highest level reached | ||||
|         if(log.back().abstractionLayer < MAX_ABSTRAX) | ||||
|         if(log.back().abstractionLevel < MAX_ABSTRAX) | ||||
|         { | ||||
|             log.back().advance(); | ||||
|             solve(log,p_Box,puzzleMat);  | ||||
| @@ -33,7 +33,7 @@ bool next(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat) | ||||
|     {    | ||||
|         //create new log, put next coordinates in and go into solve. then git gud | ||||
|         log.push_back(LogEntry()); | ||||
|         log.back().myCoor = getNextCoor(log, p_Box, puzzleMat);   | ||||
|         log.back().myCoor = calculateNextCoor(log, p_Box, puzzleMat);   | ||||
|         solve(log, p_Box,puzzleMat);  | ||||
|     } | ||||
|  | ||||
| @@ -59,56 +59,100 @@ coor calculateNextCoor(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzl | ||||
| { | ||||
|     //level 1: | ||||
|         //go left to right, then increase current row | ||||
| 	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(); | ||||
|  | ||||
|         //if  | ||||
|  | ||||
| ; | ||||
| 	return	coor(m,n); | ||||
|     //return nextCoor; | ||||
| } | ||||
|  | ||||
| void solve(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat) | ||||
| { | ||||
|     switch(log.back().abstraction==0) | ||||
|     switch(log.back().abstractionLevel) | ||||
|     { | ||||
|         case 0: | ||||
|         break; | ||||
|  | ||||
|         case 1: | ||||
|         break; | ||||
|     } | ||||
|     if() | ||||
|         //abstraction layer = 0 | ||||
|             //go to abstraction layer 0 solver | ||||
|     		//go to abstraction layer 0 solver | ||||
|         case 0: | ||||
|         abstractionlayer0solver(log,p_Box,puzzleMat); | ||||
|         break; | ||||
|  | ||||
|     //case | ||||
|         //abstraction layer = 1 | ||||
|             //go to abstraction layer 1 solver | ||||
|     //default | ||||
|             //random one of the parts  | ||||
|         case 1: | ||||
|         abstractionlayer1solver(log,p_Box,puzzleMat); | ||||
|         break; | ||||
|         default: | ||||
|         break; | ||||
|     } | ||||
| } | ||||
|  | ||||
| void abstractionlayer0solver() | ||||
| void abstractionlayer0solver(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat) | ||||
| { | ||||
|     //throw all remaining puzzle pieces into newest log | ||||
|     for(int i=0;i<p_Box.size();i++) | ||||
|     	log.back().PieceCollector[i]=p_Box[i]; | ||||
| } | ||||
|  | ||||
| void abstractionlayer1solver() | ||||
| void abstractionlayer1solver(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat) | ||||
| { | ||||
|     //remove all impossible from newest log according to abstraction layer one | ||||
| 	//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); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| void setsolution() | ||||
| void setsolution(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat) | ||||
| { | ||||
|     //remove first element in last logelement from box | ||||
|     //set pointer to this element into matrix  | ||||
| 	//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])); | ||||
| }   | ||||
|  | ||||
| void backtrack() | ||||
| bool backtrack(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat) | ||||
| { | ||||
|     //following possibilities: | ||||
|         //last log entry empty | ||||
|             //delete last log + backtrack | ||||
|         //last log entry only one solution | ||||
|             //delete last log + backtrack | ||||
|  | ||||
| 	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; | ||||
| 	} | ||||
|         //last log entry multiple solutions (and current one was randomed) | ||||
|             //delete randomed piece from puzzleCollector and go to next (which might random again depending on function) | ||||
|             //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; | ||||
|  | ||||
| } | ||||
| @@ -136,13 +136,12 @@ class LogEntry | ||||
| public: | ||||
|     vector<PuzzlePiece*> PieceCollector; | ||||
|     int abstractionLevel; | ||||
|     coor myCoor; | ||||
|     coor myCoor = coor(); | ||||
|  | ||||
|     void advance(){abstractionLevel++;} | ||||
|  | ||||
|     LogEntry() | ||||
|     { | ||||
|         myCoor(); | ||||
|         abstractionLevel=0; | ||||
|     } | ||||
| private: | ||||
| @@ -151,3 +150,14 @@ private: | ||||
| void printBox(vector<PuzzlePiece> myBox); | ||||
| vector<PuzzlePiece> createBox(uint m, uint n); | ||||
| void numerateBox(vector<PuzzlePiece>& myBox); | ||||
|  | ||||
| bool next(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat); | ||||
| coor calculateFirstCoor(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat); | ||||
| coor calculateNextCoor(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat); | ||||
| void solve(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat); | ||||
| void abstractionlayer0solver(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat); | ||||
| void abstractionlayer1solver(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat); | ||||
| void setsolution(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat); | ||||
| bool backtrack(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat); | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -1,4 +1,4 @@ | ||||
| #define MAX_ABSTRAX = 1 | ||||
| #define MAX_ABSTRAX 1 | ||||
|  | ||||
| #include "header.h" | ||||
|  | ||||
| @@ -15,17 +15,17 @@ int main() | ||||
| 	myFirstPuzzle.push_back(myFirstPart); | ||||
|  | ||||
| 	//some basic random puzzle stuff | ||||
| 	randomBox myFirstBox(cols,rows); | ||||
| 	myFirstBox.createRandomPuzzle(); | ||||
| 	myFirstBox.shuffle(); | ||||
| 	myFirstBox.printPuzzle(); | ||||
|  | ||||
| 	randomBox myRandomBox(cols,rows); | ||||
| 	myRandomBox.createRandomPuzzle(); | ||||
| 	vector<PuzzlePiece> myFirstBox = myRandomBox.shuffle(); | ||||
| 	myRandomBox.printPuzzle(); | ||||
|  | ||||
|  | ||||
|  | ||||
| 	//some advanced solver stuff | ||||
| 	vector<LogEntry> log; | ||||
| 	vector<PuzzlePiece*> p_myFirstBox; | ||||
|  | ||||
| 	for(int i=0;i<myFirstBox.size();i++) | ||||
| 		p_myFirstBox[i] = &myFirstBox[i]; | ||||
| 	Puzzle puzzleMat(cols, rows); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user