From ba4d757c5ae31240e1b8edc46d6078cd35ff7ab3 Mon Sep 17 00:00:00 2001 From: g-spacewhale <17550607+g-spacewhale@users.noreply.github.com> Date: Sat, 16 Dec 2017 00:10:52 +0100 Subject: [PATCH] minor code improvements changed coor from m,n to col,row as per philipp request. added typecast to uint8 before bitmaskconversion. --- Source/functions/solve/classExtension.cpp | 101 +++++++--------------- Source/functions/solve/structure.cpp | 24 ++--- Source/header/solve.h | 8 +- Source/main.cpp | 2 +- 4 files changed, 49 insertions(+), 86 deletions(-) diff --git a/Source/functions/solve/classExtension.cpp b/Source/functions/solve/classExtension.cpp index 4f5d1bd..15a1da1 100755 --- a/Source/functions/solve/classExtension.cpp +++ b/Source/functions/solve/classExtension.cpp @@ -14,24 +14,24 @@ void PuzzlePiece::randomCenterPiece() setConnections(0b00000000); if(rand()%2) - setConnections(getConnections() | 0b01000000); + setConnections(getConnections() | (uint8_t)0b01000000); else - setConnections(getConnections() | 0b10000000); + setConnections(getConnections() | (uint8_t)0b10000000); if(rand()%2) - setConnections(getConnections() | 0b00010000); + setConnections(getConnections() | (uint8_t)0b00010000); else - setConnections(getConnections() | 0b00100000); + setConnections(getConnections() | (uint8_t)0b00100000); if(rand()%2) - setConnections(getConnections() | 0b00000100); + setConnections(getConnections() | (uint8_t)0b00000100); else - setConnections(getConnections() | 0b00001000); + setConnections(getConnections() | (uint8_t)0b00001000); if(rand()%2) - setConnections(getConnections() | 0b00000001); + setConnections(getConnections() | (uint8_t)0b00000001); else - setConnections(getConnections() | 0b00000010); + setConnections(getConnections() | (uint8_t)0b00000010); } //tests the myPart in all 4 rotations at position m, n @@ -60,17 +60,17 @@ cout << "Old Box: "; printBox(myBox); cout << endl; #endif - for(int i = 0; i < myBox.size();i++) + for(unsigned int i = 0; i < myBox.size();i++) { - if(myBox[i].getBoxIdentifier()>getPiece(myCoor.m,myCoor.n).getBoxIdentifier()) + if(myBox[i].getBoxIdentifier()>getPiece(myCoor.col,myCoor.row).getBoxIdentifier()) { - myBox.insert(myBox.begin()+i,getPiece(myCoor.m,myCoor.n)); + myBox.insert(myBox.begin()+i,getPiece(myCoor.col,myCoor.row)); removePiece(myCoor); return i+1; } } //using push back, if the element was the last element in the vector chain - myBox.push_back(getPiece(myCoor.m,myCoor.n)); + myBox.push_back(getPiece(myCoor.col,myCoor.row)); removePiece(myCoor); return myBox.size(); } @@ -81,29 +81,29 @@ bool Puzzle::PlaceOfPartGood(coor myCoor, PuzzlePiece& myPart) PuzzlePiece negativePart(0); - negativePart.setConnections(negativePart.getConnections() | (getPiece(myCoor.m,myCoor.n+1).getConnections() & 0b11000000)); - negativePart.setConnections(negativePart.getConnections() | (getPiece(myCoor.m-1,myCoor.n).getConnections() & 0b00110000)); - negativePart.setConnections(negativePart.getConnections() | (getPiece(myCoor.m,myCoor.n-1).getConnections() & 0b00001100)); - negativePart.setConnections(negativePart.getConnections() | (getPiece(myCoor.m+1,myCoor.n).getConnections() & 0b00000011)); + negativePart.setConnections(negativePart.getConnections() | (getPiece(myCoor.col,myCoor.row+1).getConnections() & (uint8_t)0b11000000)); + negativePart.setConnections(negativePart.getConnections() | (getPiece(myCoor.col-1,myCoor.row).getConnections() & (uint8_t)0b00110000)); + negativePart.setConnections(negativePart.getConnections() | (getPiece(myCoor.col,myCoor.row-1).getConnections() & (uint8_t)0b00001100)); + negativePart.setConnections(negativePart.getConnections() | (getPiece(myCoor.col+1,myCoor.row).getConnections() & (uint8_t)0b00000011)); negativePart.shift(2); - if ( + 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() & 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() & 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() & 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)) ) + || (((negativePart.getConnections() & 0b00000011) == 0b00000000) && ((myPart.getConnections() & 0b00000011) == 0b00000000)) ) ) { //cout << "good Part: "; @@ -118,43 +118,6 @@ bool Puzzle::PlaceOfPartGood(coor myCoor, PuzzlePiece& myPart) return false; } - - -//TODO!! -//simpler algorithm to the first placeofpartgood -//not yet functional!!! -bool Puzzle::PlaceOfPart2Good(coor myCoor, PuzzlePiece& myPart) -{ - PuzzlePiece tmpPuzzlePiece = myPart; - - //make tmp a negative part - if(((tmpPuzzlePiece.getConnections() & 0b11000000) != 0b11000000) || ((tmpPuzzlePiece.getConnections() & 0b11000000) != 0b00000000)) - tmpPuzzlePiece.setConnections(tmpPuzzlePiece.getConnections() ^ 0b11000000); - if(((tmpPuzzlePiece.getConnections() & 0b00110000) != 0b00110000) || ((tmpPuzzlePiece.getConnections() & 0b00110000) != 0b00000000)) - tmpPuzzlePiece.setConnections(tmpPuzzlePiece.getConnections() ^ 0b00110000); - if(((tmpPuzzlePiece.getConnections() & 0b00001100) != 0b00001100)|| ((tmpPuzzlePiece.getConnections() & 0b00001100) != 0b00000000)) - tmpPuzzlePiece.setConnections(tmpPuzzlePiece.getConnections() ^ 0b00001100); - if(((tmpPuzzlePiece.getConnections() & 0b00000011) != 0b00000011) || ((tmpPuzzlePiece.getConnections() & 0b00000011) != 0b00000000)) - tmpPuzzlePiece.setConnections(tmpPuzzlePiece.getConnections() ^ 0b00000011); - - PuzzlePiece negativePart(0); - - negativePart.setConnections(negativePart.getConnections() | (getPiece(myCoor.m,myCoor.n+1).getConnections() & 0b11000000)); - negativePart.setConnections(negativePart.getConnections() | (getPiece(myCoor.m-1,myCoor.n).getConnections() & 0b00110000)); - negativePart.setConnections(negativePart.getConnections() | (getPiece(myCoor.m,myCoor.n-1).getConnections() & 0b00001100)); - negativePart.setConnections(negativePart.getConnections() | (getPiece(myCoor.m+1,myCoor.n).getConnections() & 0b00000011)); - - negativePart.shift(2); - - //check tmp part with environment - if(((negativePart.getConnections() & 0b11000000) == (tmpPuzzlePiece.getConnections() & 0b11000000)) && ((negativePart.getConnections() & 0b00110000) == (tmpPuzzlePiece.getConnections ()& 0b00110000)) && - ((negativePart.getConnections() & 0b00001100) == (tmpPuzzlePiece.getConnections() & 0b00001100)) && ((negativePart.getConnections() & 0b00000011) == (tmpPuzzlePiece.getConnections() & 0b00000011))) - return true; - - return false; - -} - //prints the true puzzle (without 0 edges) void Puzzle::printPuzzle() { @@ -178,24 +141,24 @@ void randomBox::createRandomAbstraction1() coor myCoor; PuzzlePiece temporaryRandomPiece(0); - for(int i=0;i randomBox::shuffle() //creates a random box size m, n, shuffles it, and then retuns it vector createBox(coor myCoor) { - randomBox myFirstPuzzleBox(myCoor.m,myCoor.n); + randomBox myFirstPuzzleBox(myCoor.col,myCoor.row); myFirstPuzzleBox.createRandomAbstraction1(); myFirstPuzzleBox.createRandomAbstraction2(); myFirstPuzzleBox.putAllIntoBox(); diff --git a/Source/functions/solve/structure.cpp b/Source/functions/solve/structure.cpp index 512756c..dd10799 100755 --- a/Source/functions/solve/structure.cpp +++ b/Source/functions/solve/structure.cpp @@ -17,8 +17,8 @@ bool next(vector& log, vector& p_Box, Puzzle& puzzleMat) else if(log.back().PieceCollector.size() > 1) { //moreLayers is 0, setbest is 1 - if(SetBestorMoreLayers()) setsolution(log,p_Box,puzzleMat); - else solve(log,p_Box,puzzleMat); + //if(SetBestorMoreLayers()) setsolution(log,p_Box,puzzleMat); + //else solve(log,p_Box,puzzleMat); } //case last log exactly one solution else if(log.back().PieceCollector.size() == 1) @@ -43,7 +43,7 @@ void createNextLogElement(vector& log, vector& p_Box, Pu { log.emplace_back(LogEntry()); log.back().myCoor = calculateNextCoor(log, p_Box, puzzleMat); - getLayerDestructionPowerfromSurrounding(); + //getLayerDestructionPowerfromSurrounding(); solve(log, p_Box,puzzleMat); } @@ -57,8 +57,8 @@ coor calculateNextCoor(vector& log, vector& p_Box, Puzzl return {0,0}; - int m= log.rbegin()[1].myCoor.m; - int n= log.rbegin()[1].myCoor.n; + int m= log.rbegin()[1].myCoor.col; + int n= log.rbegin()[1].myCoor.row; if(m& log, vector& p_Box, Puzzl void solve(vector& log, vector& p_Box, Puzzle& puzzleMat) { - getNextHighestLayerworth(puzzleMat); //sets in abstractionLevel + //getNextHighestLayerworth(puzzleMat); //sets in abstractionLevel //status(log,p_Box,puzzleMat); switch(log.back().abstractionLevel) { @@ -83,10 +83,10 @@ void solve(vector& log, vector& p_Box, Puzzle& puzzleMat break; } - capLogElements(log); - calculateWorth(log); - calculateTrueDestructionPower(log,puzzleMat); - calculateNewCombinedProbablility(log); + //capLogElements(log); + //calculateWorth(log); + //calculateTrueDestructionPower(log,puzzleMat); + //calculateNewCombinedProbablility(log); } @@ -237,7 +237,7 @@ void status(vector& log, vector& p_Box, Puzzle& puzzleMa cout << "isset: 1" << endl; else cout << "isset: 0" << endl; - cout << "m: " << log[i].myCoor.m << " n: " << log[i].myCoor.n << endl; + cout << "col: " << log[i].myCoor.col<< " row: " << log[i].myCoor.row << endl; } cout << endl; @@ -258,5 +258,5 @@ void calculateTrueDestructionPower(vector& log, Puzzle& puzzleMat, flo { //hier muss noch rein, wo die zeit der Abstractionlevels gespeichter wird float destructionPower=sqrt(Layerworth * log.back().abstractionLevel); - puzzleMat.setdestructionPower(log.back().myCoor,log.back().abstractionLevel,destructionPower); + //puzzleMat.setdestructionPower(log.back().myCoor,log.back().abstractionLevel,destructionPower); } \ No newline at end of file diff --git a/Source/header/solve.h b/Source/header/solve.h index c0be2db..ad72406 100755 --- a/Source/header/solve.h +++ b/Source/header/solve.h @@ -8,8 +8,8 @@ using namespace std; class coor { public: - int m, n; - coor(int newm=-1,int newn=-1): m(newm), n(newn) + unsigned int col, row; + coor(int newcol=-1,int newrow=-1): col(newcol), row(newrow) {} }; @@ -84,7 +84,7 @@ public: { if(PlaceOfPartGood(myCoor,newPiece)) { - Matrix[myCoor.n+1][myCoor.m+1] = newPiece; + Matrix[myCoor.row+1][myCoor.col+1] = newPiece; return 1; } return 0; @@ -93,7 +93,7 @@ public: //removes piece and instead puts undefined piece bool removePiece(coor myCoor) // 6th change: works { - Matrix[myCoor.n+1][myCoor.m+1].setConnections(0b11111111); + Matrix[myCoor.row+1][myCoor.col+1].setConnections(0b11111111); return 1; } diff --git a/Source/main.cpp b/Source/main.cpp index 79ebb39..d19320e 100644 --- a/Source/main.cpp +++ b/Source/main.cpp @@ -7,7 +7,7 @@ unsigned int PuzzlePiece::idcount(0); int main() { - unsigned int cols=2, rows=3; + unsigned int cols=5, rows=6; //some basic random puzzle stuff vector myFirstBox = createBox(coor(cols,rows));