From 95cf7cc7b3b4866157ae1f444ce4b305648c8696 Mon Sep 17 00:00:00 2001 From: g-spacewhale Date: Sat, 18 Nov 2017 08:16:05 +0100 Subject: [PATCH] edited file structure to better represent our current system. added entire code base --- Source/functions/solve/classExtension.cpp | 283 ++++++++++++++++++++++ Source/functions/solve/solve.cpp | 0 Source/functions/solve/structure.cpp | 63 +++++ Source/header.h | 19 ++ 4 files changed, 365 insertions(+) create mode 100644 Source/functions/solve/classExtension.cpp delete mode 100644 Source/functions/solve/solve.cpp create mode 100644 Source/functions/solve/structure.cpp create mode 100644 Source/header.h diff --git a/Source/functions/solve/classExtension.cpp b/Source/functions/solve/classExtension.cpp new file mode 100644 index 0000000..f653fc0 --- /dev/null +++ b/Source/functions/solve/classExtension.cpp @@ -0,0 +1,283 @@ + +//shifts puzzle piece one to the right +void PuzzlePiece::shift(unsigned int moves) +{ + shifts = (shifts+moves)%4; + setConnections(((getConnections() >> (moves*2)) | (getConnections() << sizeof(unsigned char)*8 - (moves*2)))); +} + +//creates random centerpiece +void PuzzlePiece::randomCenterPiece() +{ + setConnections(0b00000000); + + if(rand()%2) + setConnections(getConnections() | 0b01000000); + else + setConnections(getConnections() | 0b10000000); + + if(rand()%2) + setConnections(getConnections() | 0b00010000); + else + setConnections(getConnections() | 0b00100000); + + if(rand()%2) + setConnections(getConnections() | 0b00000100); + else + setConnections(getConnections() | 0b00001000); + + if(rand()%2) + setConnections(getConnections() | 0b00000001); + else + setConnections(getConnections() | 0b00000010); +} + +//tries all pieces in box from separator to end and places fitting into matrix. removes fitting piece +//use separator if you have to retract to a position +//seperator may be bigger than box size, if all puzzle pieces have already been looked at. +// it immediately retracts again then (returns -1) +unsigned int Puzzle::tryAllPieces(unsigned int m, unsigned int n, vector& myBox, unsigned int separator) +{ + for(int i=separator; i& myBox) +{ +#ifdef debug +cout << "putting back" << endl; +cout << "Old Box: "; +printBox(myBox); +cout << endl; +#endif + for(int i = 0; i < myBox.size();i++) + { + if(myBox[i].getBoxIdentifier()>getPiece(m,n).getBoxIdentifier()) + { + myBox.insert(myBox.begin()+i,getPiece(m,n)); + removePiece(m,n); + return i+1; + } + } + //using push back, if the element was the last element in the vector chain + myBox.push_back(getPiece(m,n)); + removePiece(m,n); + return myBox.size(); +} + +//checks if the myPart in its current orientation is legal in position m, n +bool Puzzle::PlaceOfPartGood(unsigned int m,unsigned int n, PuzzlePiece& myPart) +{ + + PuzzlePiece negativePart(0); + + negativePart.setConnections(negativePart.getConnections() | (getPiece(m,n+1).getConnections() & 0b11000000)); + negativePart.setConnections(negativePart.getConnections() | (getPiece(m-1,n).getConnections() & 0b00110000)); + negativePart.setConnections(negativePart.getConnections() | (getPiece(m,n-1).getConnections() & 0b00001100)); + negativePart.setConnections(negativePart.getConnections() | (getPiece(m+1,n).getConnections() & 0b00000011)); + 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) == 0b00000000) && ((myPart.getConnections() & 0b11000000) == 0b00000000)) ) + && + ( ((((negativePart.getConnections() & 0b00110000) ^ (myPart.getConnections() & 0b00110000)) != 0b00000000) && (((myPart.getConnections() & 0b00110000) != 0b00000000) && (negativePart.getConnections() & 0b00110000) != 0b00000000)) + || ((((negativePart.getConnections() & 0b00110000) == 0b00110000) || ((myPart.getConnections() & 0b00110000) == 0b00110000)) && (((myPart.getConnections() & 0b00110000) != 0b00000000) && (negativePart.getConnections() & 0b00110000) != 0b00000000)) + || (((negativePart.getConnections() & 0b00110000) == 0b00000000) && ((myPart.getConnections() & 0b00110000) == 0b00000000)) ) + && + ( ((((negativePart.getConnections() & 0b00001100) ^ (myPart.getConnections() & 0b00001100)) != 0b00000000) && (((myPart.getConnections() & 0b00001100) != 0b00000000) && (negativePart.getConnections() & 0b00001100) != 0b00000000)) + || ((((negativePart.getConnections() & 0b00001100) == 0b00001100) || ((myPart.getConnections() & 0b00001100) == 0b00001100)) && (((myPart.getConnections() & 0b00001100) != 0b00000000) && (negativePart.getConnections() & 0b00001100) != 0b00000000)) + || (((negativePart.getConnections() & 0b00001100) == 0b00000000) && ((myPart.getConnections() & 0b00001100) == 0b00000000)) ) + && + ( ((((negativePart.getConnections() & 0b00000011) ^ (myPart.getConnections() & 0b00000011)) != 0b00000000) && (((myPart.getConnections() & 0b00000011) != 0b00000000) && (negativePart.getConnections() & 0b00000011) != 0b00000000)) + || ((((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 + return 1; + } + + + + return 0; +} + + +//TODO!! +//simpler algorithm to the first placeofpartgood +//not yet functional!!! +bool Puzzle::PlaceOfPart2Good(unsigned int m,unsigned int n, PuzzlePiece& myPart) +{ + + PuzzlePiece negativePart(0); + + negativePart.setConnections(negativePart.getConnections() | (getPiece(m,n+1).getConnections() & 0b11000000)); + negativePart.setConnections(negativePart.getConnections() | (getPiece(m-1,n).getConnections() & 0b00110000)); + negativePart.setConnections(negativePart.getConnections() | (getPiece(m,n-1).getConnections() & 0b00001100)); + negativePart.setConnections(negativePart.getConnections() | (getPiece(m+1,n).getConnections() & 0b00000011)); + negativePart.shift(2); + + //A and D or B and C or not A and not B and not C and not D + + if ( + ( ( (negativePart.getConnections() & 0b10000000) & (myPart.getConnections() & 0b01000000)) + || ( (negativePart.getConnections() & 0b01000000) & (myPart.getConnections() & 0b10000000)) + || ((!(negativePart.getConnections() & 0b10000000) & !(myPart.getConnections() & 0b10000000)) & (!(negativePart.getConnections() & 0b01000000) & !(myPart.getConnections() & 0b01000000))) + ) + && + ( ( (negativePart.getConnections() & 0b00100000) & (myPart.getConnections() & 0b00010000)) + || ( (negativePart.getConnections() & 0b00010000) & (myPart.getConnections() & 0b00100000)) + || ((!(negativePart.getConnections() & 0b00100000) & !(myPart.getConnections() & 0b00100000)) & (!(negativePart.getConnections() & 0b00010000) & !(myPart.getConnections() & 0b00010000))) + ) + && + ( ( (negativePart.getConnections() & 0b00001000) & (myPart.getConnections() & 0b00000100)) + || ( (negativePart.getConnections() & 0b00000100) & (myPart.getConnections() & 0b00001000)) + || ((!(negativePart.getConnections() & 0b00001000) & !(myPart.getConnections() & 0b00001000)) & (!(negativePart.getConnections() & 0b00000100) & !(myPart.getConnections() & 0b00000100))) + ) + && + ( ( (negativePart.getConnections() & 0b00000010) & (myPart.getConnections() & 0b00000001)) + || ( (negativePart.getConnections() & 0b00000001) & (myPart.getConnections() & 0b00000010)) + || ((!(negativePart.getConnections() & 0b00000010) & !(myPart.getConnections() & 0b00000010)) & (!(negativePart.getConnections() & 0b00000001) & !(myPart.getConnections() & 0b00000001))) + ) + ) + + return 1; + + cout << "nogood" << endl; + return 0; +} + +//prints the true puzzle (without 0 edges) +void Puzzle::printPuzzle() +{ + cout << "current Puzzle: " << endl; + for(int i=1;i::iterator i = Box.begin(); i != Box.end(); i++) + { + (*i).printPiece(); + cout << ' '; + } + cout << endl; +} + +//shuffles around a box, randomizing pieces and orientation +vector randomBox::shuffle() +{ + random_shuffle(Box.begin(),Box.end()); + for (vector::iterator i = Box.begin(); i != Box.end(); i++) + (*i).shift(rand()%4); + numerateBox(Box); + return Box; +} + +//creates a random box size m, n, shuffles it, and then retuns it +vector createBox(uint m, uint n) +{ + randomBox myFirstPuzzleBox(m,n); + myFirstPuzzleBox.createRandomPuzzle(); + return myFirstPuzzleBox.shuffle(); +} + +//prints contents of box +void printBox(vector myBox) +{ + cout << "current Box: " << endl; + for (vector::iterator i = myBox.begin(); i != myBox.end(); i++) + { + (*i).printPiece(); + cout << ' '; + } + cout << endl; + return; +} + +//gives every element in box a box identifier. +void numerateBox(vector& myBox) +{ + for(int i = 0; i< myBox.size();i++) + myBox[i].setBoxIdentifier(i); + + return; +} \ No newline at end of file diff --git a/Source/functions/solve/solve.cpp b/Source/functions/solve/solve.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/Source/functions/solve/structure.cpp b/Source/functions/solve/structure.cpp new file mode 100644 index 0000000..a6bd19b --- /dev/null +++ b/Source/functions/solve/structure.cpp @@ -0,0 +1,63 @@ + +bool next() +{ + + //calculates next move according to log + + //case puzzle solved + //return 0; + + //case last log empty + //backtrack + + //case last log exactly one solution + //put all remaining puzzle pieces into new log entry + //try next piece/place with solve abstraction level 0 + + //case last log multiple entries + //advance abstraction layer of last log by one and solve() + //or pick first if highest level reached + + //return 1; +} + +void solve() +{ + //case + //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 +} + +void abstractionlayer0solver() +{ + //throw all remaining puzzle pieces into log + //remove all impossible +} + +void abstractionlayer1solver() +{ + //remove all impossible according to abstraction layer one +} + +void setsolution() +{ + //put + //set pointer to log into matrix +} + +void backtrack() +{ + //following possibilities: + //last log entry empty + //delete last log + backtrack + //last log entry only one solution + //delete last log + backtrack + //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) +} \ No newline at end of file diff --git a/Source/header.h b/Source/header.h new file mode 100644 index 0000000..75f46ae --- /dev/null +++ b/Source/header.h @@ -0,0 +1,19 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + +#include "header/input.h" +#include "header/solve.h" +#include "header/output.h" + +#include "functions/input/input.cpp" +#include "functions/solve/classExtension.cpp" +#include "functions/solve/structure.cpp" +#include "functions/output/output.cpp" +