From 15940f41dad6f7e5102dd5e44a222d75dfe83c94 Mon Sep 17 00:00:00 2001 From: g-spacewhale Date: Sun, 19 Nov 2017 17:52:02 +0100 Subject: [PATCH] added more strucuture to strucutur.cpp. commiting now to extract new debugging branch --- Source/functions/solve/structure.cpp | 98 ++++++++++++++++++++-------- Source/header/solve.h | 16 ++++- Source/main.cpp | 12 ++-- 3 files changed, 90 insertions(+), 36 deletions(-) diff --git a/Source/functions/solve/structure.cpp b/Source/functions/solve/structure.cpp index 05a62d5..772eef5 100644 --- a/Source/functions/solve/structure.cpp +++ b/Source/functions/solve/structure.cpp @@ -2,14 +2,14 @@ bool next(vector& log, vector& 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& log, vector& 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& log, vector& 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& log, vector& 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& log, vector& p_Box, Puzzle& puzzleMat) { - switch(log.back().abstraction==0) + switch(log.back().abstractionLevel) { + //abstraction layer = 0 + //go to abstraction layer 0 solver case 0: + abstractionlayer0solver(log,p_Box,puzzleMat); break; - case 1: - break; - } - if() - //abstraction layer = 0 - //go to abstraction layer 0 solver - - //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& log, vector& p_Box, Puzzle& puzzleMat) { //throw all remaining puzzle pieces into newest log + for(int i=0;i& log, vector& 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, vector& 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& log, vector& 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; + } \ No newline at end of file diff --git a/Source/header/solve.h b/Source/header/solve.h index 27b7618..5dd593f 100644 --- a/Source/header/solve.h +++ b/Source/header/solve.h @@ -136,13 +136,12 @@ class LogEntry public: vector PieceCollector; int abstractionLevel; - coor myCoor; + coor myCoor = coor(); void advance(){abstractionLevel++;} LogEntry() { - myCoor(); abstractionLevel=0; } private: @@ -150,4 +149,15 @@ private: void printBox(vector myBox); vector createBox(uint m, uint n); -void numerateBox(vector& myBox); \ No newline at end of file +void numerateBox(vector& myBox); + +bool next(vector& log, vector& p_Box, Puzzle& puzzleMat); +coor calculateFirstCoor(vector& log, vector& p_Box, Puzzle& puzzleMat); +coor calculateNextCoor(vector& log, vector& p_Box, Puzzle& puzzleMat); +void solve(vector& log, vector& p_Box, Puzzle& puzzleMat); +void abstractionlayer0solver(vector& log, vector& p_Box, Puzzle& puzzleMat); +void abstractionlayer1solver(vector& log, vector& p_Box, Puzzle& puzzleMat); +void setsolution(vector& log, vector& p_Box, Puzzle& puzzleMat); +bool backtrack(vector& log, vector& p_Box, Puzzle& puzzleMat); + + diff --git a/Source/main.cpp b/Source/main.cpp index a2a86ec..8ca7e67 100644 --- a/Source/main.cpp +++ b/Source/main.cpp @@ -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 myFirstBox = myRandomBox.shuffle(); + myRandomBox.printPuzzle(); //some advanced solver stuff vector log; vector p_myFirstBox; + for(int i=0;i