From aa00553bdb4d0047327ba2ead57702488e979aa4 Mon Sep 17 00:00:00 2001 From: g-spacewhale Date: Sun, 19 Nov 2017 22:53:00 +0100 Subject: [PATCH] removed bugs. some puzzles take forever. most can be solved --- Source/functions/solve/classExtension.cpp | 25 ++-- Source/functions/solve/structure.cpp | 164 ++++++++++++++++++---- Source/header/solve.h | 14 +- Source/main.cpp | 11 +- 4 files changed, 165 insertions(+), 49 deletions(-) diff --git a/Source/functions/solve/classExtension.cpp b/Source/functions/solve/classExtension.cpp index ed20ce6..30eb77c 100644 --- a/Source/functions/solve/classExtension.cpp +++ b/Source/functions/solve/classExtension.cpp @@ -57,15 +57,12 @@ bool Puzzle::testRotationPiece(unsigned int m, unsigned int n, PuzzlePiece& myPa { for(int rotation=0; rotation < 4; rotation++) { - if(PlaceOfPartGood(m,n, myPart)) + if(PlaceOfPartGood(m,n,myPart)) return 1; myPart.shift(1); } -#ifdef debug -cout << "bad part: "; -myPart.printPiece(); -cout << endl; -#endif + + //cout << "Was a bad part" << endl; return 0; } @@ -107,10 +104,9 @@ bool Puzzle::PlaceOfPartGood(unsigned int m,unsigned int n, PuzzlePiece& myPart) negativePart.shift(2); - if ( ( ((((negativePart.getConnections() & 0b11000000) ^ (myPart.getConnections() & 0b11000000)) != 0b00000000) && (((myPart.getConnections() & 0b11000000) != 0b00000000) && (negativePart.getConnections() & 0b11000000) != 0b00000000)) - || ((((negativePart.getConnections() & 0b11000000) == 0b11000000) || ((myPart.getConnections() & 0b11000000) == 0b11000000)) && (( myPart.getConnections() & 0b11000000) != 0b00000000) && (negativePart.getConnections() & 0b11000000) != 0b00000000) + || ((((negativePart.getConnections() & 0b11000000) == 0b11000000) || ((myPart.getConnections() & 0b11000000) == 0b11000000)) && (((myPart.getConnections() & 0b11000000) != 0b00000000) && (negativePart.getConnections() & 0b11000000) != 0b00000000)) || (((negativePart.getConnections() & 0b11000000) == 0b00000000) && ((myPart.getConnections() & 0b11000000) == 0b00000000)) ) && ( ((((negativePart.getConnections() & 0b00110000) ^ (myPart.getConnections() & 0b00110000)) != 0b00000000) && (((myPart.getConnections() & 0b00110000) != 0b00000000) && (negativePart.getConnections() & 0b00110000) != 0b00000000)) @@ -125,14 +121,15 @@ bool Puzzle::PlaceOfPartGood(unsigned int m,unsigned int n, PuzzlePiece& myPart) || ((((negativePart.getConnections() & 0b00000011) == 0b00000011) || ((myPart.getConnections() & 0b00000011) == 0b00000011)) && (((myPart.getConnections() & 0b00000011) != 0b00000000) && (negativePart.getConnections() & 0b00000011) != 0b00000000)) || (((negativePart.getConnections() & 0b00000011) == 0b00000000) && ((myPart.getConnections() & 0b00000011) == 0b00000000)) ) ) - { -#ifdef debug -cout << "good Part: "; -myPart.printPiece(); -cout << endl; -#endif + { + //cout << "good Part: "; + //myPart.printPiece(); + //cout << endl; return 1; } + //cout << "bad Part: "; + //myPart.printPiece(); + //cout << endl; return 0; diff --git a/Source/functions/solve/structure.cpp b/Source/functions/solve/structure.cpp index 772eef5..5c723f1 100644 --- a/Source/functions/solve/structure.cpp +++ b/Source/functions/solve/structure.cpp @@ -1,48 +1,84 @@ +void status(vector& log, vector& p_Box, Puzzle& puzzleMat); + bool next(vector& log, vector& p_Box, Puzzle& puzzleMat) { - //case first log entry empty + + //status(log,p_Box,puzzleMat); + + //log not yet started if(!(log.size())) { + //cout << "creating new log" << endl; log.push_back(LogEntry()); + log.back().myCoor = calculateFirstCoor(log, p_Box, puzzleMat); solve(log, p_Box,puzzleMat); } - //case puzzle solved - else if(!(p_Box.size())) - return 0; + + //last log element is set, create new log element + else if(log.back().isSet()) + { + if(!(p_Box.size())) + { + cout << "box done" << endl; + return 0; + } + log.push_back(LogEntry()); + log.back().myCoor = calculateNextCoor(log, p_Box, puzzleMat); + solve(log, p_Box,puzzleMat); + } + //last log element is empty, backtrack + else if(!(log.back().PieceCollector.size())) + { + //cout << "backtracking" << endl; + backtrack(log,p_Box,puzzleMat); + } - //case last log multiple entries + //case last log element has 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 + //is not yet max abstracted if(log.back().abstractionLevel < MAX_ABSTRAX) { + //cout << "advancing abstraction layer" << endl; log.back().advance(); solve(log,p_Box,puzzleMat); } + //no more layers, pick first else { + //cout << "setting first as solution" << endl; + log.back().advanceRandomed(); setsolution(log,p_Box,puzzleMat); } } //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()); - log.back().myCoor = calculateNextCoor(log, p_Box, puzzleMat); - solve(log, p_Box,puzzleMat); - } - - //case last log empty - //backtrack - else if(log.back().PieceCollector.size() == 0) - { - backtrack(log,p_Box,puzzleMat); + { + //cout << "exactly one solution" << endl; + if(log.back().hasRandomed()) + { + if(log.back().abstractionLevel < MAX_ABSTRAX) + { + //cout << "advancing abstraction layer" << endl; + log.back().advance(); + solve(log,p_Box,puzzleMat); + } + else + { + //cout << "setting as solution bec fit" << endl; + setsolution(log,p_Box,puzzleMat); + } + } + else + { + //cout << "setting as solution bec trivial" << endl; + setsolution(log,p_Box,puzzleMat); + } } + //cout << "next" << endl; return 1; } @@ -59,10 +95,10 @@ 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, Puzzl void solve(vector& log, vector& p_Box, Puzzle& puzzleMat) { + //status(log,p_Box,puzzleMat); switch(log.back().abstractionLevel) { //abstraction layer = 0 @@ -88,35 +125,55 @@ void solve(vector& log, vector& p_Box, Puzzle& puzzleMat default: break; } + //status(log,p_Box,puzzleMat); } 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 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 - for(int i=0;i& log, vector& p_Box, Puzzle& puzzleMat) @@ -127,7 +184,10 @@ bool backtrack(vector& log, vector& p_Box, Puzzle& puzzl if(!(log.back().PieceCollector.size())) { - log.back().PieceCollector.pop_back(); + //cout << "none" << endl; + puzzleMat.removePiece(log.back().myCoor.m, log.back().myCoor.n); + log.pop_back(); + //status(log,p_Box,puzzleMat); backtrack(log,p_Box,puzzleMat); return 1; } @@ -136,9 +196,12 @@ bool backtrack(vector& log, vector& p_Box, Puzzle& puzzl //delete last logd put back into box + backtrack else if((log.back().PieceCollector.size())==1) { + //cout << "one" << endl; p_Box.push_back(log.back().PieceCollector[0]); - log.back().PieceCollector.pop_back(); - //TODO remove from puzzle as well!!! + 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; } @@ -146,8 +209,15 @@ bool backtrack(vector& log, vector& p_Box, Puzzle& puzzl //delete randomed piece from PieceCollector and go to next (which might random again depending on function) else if((log.back().PieceCollector.size())>1) { + //cout << "multiple" << endl; p_Box.push_back(log.back().PieceCollector[0]); log.back().PieceCollector.erase(log.back().PieceCollector.begin()); + + if(log.back().PieceCollector.size()==1) + log.back().decreaseRandomed(); + + //cout << "erased first element" << endl; + //status(log,p_Box,puzzleMat); setsolution(log,p_Box,puzzleMat); return 1; //no need to remove from puzzle mat, as sersolution overwrites it anyway @@ -155,4 +225,36 @@ bool backtrack(vector& log, vector& p_Box, Puzzle& puzzl else return 0; +} + +void status(vector& log, vector& p_Box, Puzzle& puzzleMat) +{ + cout << "----------------------------" << endl; + cout << "status:" << endl; + cout << "hasrandomed: " << log[0].hasRandomed() << endl; + for(int i=0;i myBox); vector createBox(uint m, uint n); void numerateBox(vector& myBox); diff --git a/Source/main.cpp b/Source/main.cpp index 8ca7e67..e9eaeb8 100644 --- a/Source/main.cpp +++ b/Source/main.cpp @@ -1,4 +1,5 @@ #define MAX_ABSTRAX 1 +#define structdebug #include "header.h" @@ -7,7 +8,7 @@ int main() { - int cols=3, rows=2; + int cols=10, rows=10; //some basic part stuff vector myFirstPuzzle; Part myFirstPart; @@ -18,7 +19,6 @@ int main() randomBox myRandomBox(cols,rows); myRandomBox.createRandomPuzzle(); vector myFirstBox = myRandomBox.shuffle(); - myRandomBox.printPuzzle(); @@ -26,9 +26,14 @@ int main() vector log; vector p_myFirstBox; + cout << "original puzzle: " << endl; + myRandomBox.printPuzzle(); + cout << endl; for(int i=0;i