From e66aa080bb0ea3c403bf0ddd5f41cc651447f350 Mon Sep 17 00:00:00 2001 From: d-kaselautzke <33751201+d-kaselautzke@users.noreply.github.com> Date: Wed, 6 Dec 2017 20:56:01 +0100 Subject: [PATCH] changed m,n to Coor class changed every unsigned int m,n to coor myCoor. except of getPiece --- Source/functions/solve/classExtension.cpp | 58 +++++++++++++---------- Source/functions/solve/structure.cpp | 15 +++--- Source/header/solve.h | 40 ++++++++-------- 3 files changed, 60 insertions(+), 53 deletions(-) diff --git a/Source/functions/solve/classExtension.cpp b/Source/functions/solve/classExtension.cpp index dd3bd46..4d1aa63 100755 --- a/Source/functions/solve/classExtension.cpp +++ b/Source/functions/solve/classExtension.cpp @@ -37,13 +37,14 @@ void PuzzlePiece::randomCenterPiece() //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) +unsigned int Puzzle::tryAllPieces(coor myCoor, vector& myBox, unsigned int separator) { for(int i=separator; i& myBox) +unsigned int Puzzle::putBackIntoBox(coor myCoor, vector& myBox) { #ifdef debug cout << "putting back" << endl; @@ -79,29 +81,29 @@ cout << endl; #endif for(int i = 0; i < myBox.size();i++) { - if(myBox[i].getBoxIdentifier()>getPiece(m,n).getBoxIdentifier()) + if(myBox[i].getBoxIdentifier()>getPiece(myCoor.m,myCoor.n).getBoxIdentifier()) { - myBox.insert(myBox.begin()+i,getPiece(m,n)); - removePiece(m,n); + myBox.insert(myBox.begin()+i,getPiece(myCoor.m,myCoor.n)); + removePiece(myCoor); 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); + myBox.push_back(getPiece(myCoor.m,myCoor.n)); + removePiece(myCoor); 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) +bool Puzzle::PlaceOfPartGood(coor myCoor, 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.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); @@ -140,7 +142,7 @@ bool Puzzle::PlaceOfPartGood(unsigned int m,unsigned int n, PuzzlePiece& myPart) //TODO!! //simpler algorithm to the first placeofpartgood //not yet functional!!! -bool Puzzle::PlaceOfPart2Good(unsigned int m,unsigned int n, PuzzlePiece& myPart) +bool Puzzle::PlaceOfPart2Good(coor myCoor, PuzzlePiece& myPart) { PuzzlePiece tmpPuzzlePiece = myPart; @@ -156,10 +158,10 @@ 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.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); @@ -191,7 +193,9 @@ void Puzzle::printPuzzle() //creates a legal puzzle out of random pieces void randomBox::createRandomPuzzle() { + 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(unsigned int m, unsigned int n) +vector createBox(coor myCoor) { - randomBox myFirstPuzzleBox(m,n); + randomBox myFirstPuzzleBox(myCoor.m, myCoor.n); myFirstPuzzleBox.createRandomPuzzle(); return myFirstPuzzleBox.shuffle(); } diff --git a/Source/functions/solve/structure.cpp b/Source/functions/solve/structure.cpp index fb94944..d108222 100755 --- a/Source/functions/solve/structure.cpp +++ b/Source/functions/solve/structure.cpp @@ -131,7 +131,7 @@ void abstractionlayer1solver(vector& log, vector& p_Box, { (*(log.back().PieceCollector[i])).resetShift(); //TODO: change checker from checking every box piece to only checking the simplifyed version ob the box with abstraction layer one - if(!(puzzleMat.testRotationPiece(log.back().myCoor.m, log.back().myCoor.n, *(log.back().PieceCollector[i])))) + if(!(puzzleMat.testRotationPiece(log.back().myCoor, *(log.back().PieceCollector[i])))) log.back().PieceCollector.erase(log.back().PieceCollector.begin()+i); else { @@ -153,9 +153,10 @@ void setsolution(vector& log, vector& p_Box, Puzzle& puz i++; //turn piece until it fits and then set element into matrix - if(puzzleMat.testRotationPiece(log.back().myCoor.m, log.back().myCoor.n,*(log.back().PieceCollector[0]))) + if(puzzleMat.testRotationPiece(log.back().myCoor,*(log.back().PieceCollector[0]))) //error if it turned - puzzleMat.setPiece(log.back().myCoor.m, log.back().myCoor.n, *(log.back().PieceCollector[0])); + //puzzleMat.setPiece(log.back().myCoor.m, log.back().myCoor.n, *(log.back().PieceCollector[0])); + puzzleMat.setPiece(log.back().myCoor, *(log.back().PieceCollector[0])); else { cout << "fatal error, wrong piece saved" << endl; @@ -172,7 +173,7 @@ bool backtrack(vector& log, vector& p_Box, Puzzle& puzzl //last log entry empty - delete last log + backtrack if(!(log.back().PieceCollector.size())) { - puzzleMat.removePiece(log.back().myCoor.m, log.back().myCoor.n); + puzzleMat.removePiece(log.back().myCoor); log.pop_back(); backtrack(log,p_Box,puzzleMat); return 1; @@ -187,7 +188,7 @@ bool backtrack(vector& log, vector& p_Box, Puzzle& puzzl while((log.back().PieceCollector[0])->getShift() !=0 && (log.back().PieceCollector[0])->getShift() !=3) { log.back().PieceCollector[0]->shift(1); - if(puzzleMat.testRotationPiece(log.back().myCoor.m, log.back().myCoor.n, *(log.back().PieceCollector[0]), 1)) + if(puzzleMat.testRotationPiece(log.back().myCoor, *(log.back().PieceCollector[0]), 1)) { setsolution(log,p_Box,puzzleMat); return 1; @@ -197,7 +198,7 @@ bool backtrack(vector& log, vector& p_Box, Puzzle& puzzl p_Box.push_back(log.back().PieceCollector[0]); //shuffleup random_shuffle(p_Box.begin(),p_Box.end()); - puzzleMat.removePiece(log.back().myCoor.m, log.back().myCoor.n); + puzzleMat.removePiece(log.back().myCoor); log.pop_back(); //cout << "removed" << endl; //status(log,p_Box,puzzleMat); @@ -216,7 +217,7 @@ bool backtrack(vector& log, vector& p_Box, Puzzle& puzzl while((log.back().PieceCollector[0])->getShift() !=0 && (log.back().PieceCollector[0])->getShift() !=3) { log.back().PieceCollector[0]->shift(1); - if(puzzleMat.testRotationPiece(log.back().myCoor.m, log.back().myCoor.n, *(log.back().PieceCollector[0]), 1)) + if(puzzleMat.testRotationPiece(log.back().myCoor, *(log.back().PieceCollector[0]), 1)) { setsolution(log,p_Box,puzzleMat); return 1; diff --git a/Source/header/solve.h b/Source/header/solve.h index efce116..f1d85a0 100755 --- a/Source/header/solve.h +++ b/Source/header/solve.h @@ -1,6 +1,13 @@ - using namespace std; +class coor +{ +public: + int m, n; + coor(int newm=-1,int newn=-1): m(newm), n(newn) + {} +}; + class PuzzlePiece: public Part { public: @@ -68,36 +75,36 @@ public: } } - bool setPiece(unsigned int m,unsigned int n,PuzzlePiece newPiece) + bool setPiece(coor myCoor,PuzzlePiece newPiece) // 5th change: works { - if(PlaceOfPartGood(m,n,newPiece)) + if(PlaceOfPartGood(myCoor,newPiece)) { - Matrix[n+1][m+1] = newPiece; + Matrix[myCoor.n+1][myCoor.m+1] = newPiece; return 1; } return 0; } //removes piece and instead puts undefined piece - bool removePiece(unsigned int m,unsigned int n) + bool removePiece(coor myCoor) // 6th change: works { - Matrix[n+1][m+1].setConnections(0b11111111); + Matrix[myCoor.n+1][myCoor.m+1].setConnections(0b11111111); return 1; } //getter - PuzzlePiece getPiece(unsigned int m,unsigned int n) { return Matrix[n+1][m+1]; } + PuzzlePiece getPiece(unsigned int m, unsigned int n) { return Matrix[n+1][m+1]; } // 7th change: error m+1, n+1 unsigned int getCols(){ return col; } unsigned int getRows(){ return row; } //functtion definitions void printPuzzle(); - bool PlaceOfPartGood(unsigned int m, unsigned int n, PuzzlePiece& myPart); - bool PlaceOfPart2Good(unsigned int m,unsigned int n, PuzzlePiece& myPart); + bool PlaceOfPartGood(coor myCoor, PuzzlePiece& myPart); // 8th change: works + bool PlaceOfPart2Good(coor myCoor, PuzzlePiece& myPart); // 2nd change: works - bool testRotationPiece(unsigned int m, unsigned int n, PuzzlePiece& myPart, int nrOfRotations=4); - unsigned int tryAllPieces(unsigned int m, unsigned int n, vector& myBox, unsigned int separator); - unsigned int putBackIntoBox(unsigned int m, unsigned int n, vector& myBox); + bool testRotationPiece(coor myCoor, PuzzlePiece& myPart, int nrOfRotations=4); // 9th change: works + unsigned int tryAllPieces(coor myCoor, vector& myBox, unsigned int separator); // 3rd change: works + unsigned int putBackIntoBox(coor myCoor, vector& myBox); // 4th change: works private: @@ -124,13 +131,6 @@ private: }; -class coor -{ -public: - int m, n; - coor(int newm=-1,int newn=-1): m(newm), n(newn) - {} -}; class LogEntry { @@ -170,7 +170,7 @@ unsigned int PuzzlePiece::idcount(0); void printBox(vector myBox); -vector createBox(unsigned int m, unsigned int n); +vector createBox(coor boxCoor); // 1st change: works void numerateBox(vector& myBox); bool next(vector& log, vector& p_Box, Puzzle& puzzleMat);