changed m,n to Coor class

changed every unsigned int m,n to coor myCoor. except of getPiece
This commit is contained in:
d-kaselautzke 2017-12-06 20:56:01 +01:00
parent 067f64c9ab
commit e66aa080bb
3 changed files with 60 additions and 53 deletions

View File

@ -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<PuzzlePiece>& myBox, unsigned int separator)
unsigned int Puzzle::tryAllPieces(coor myCoor, vector<PuzzlePiece>& myBox, unsigned int separator)
{
for(int i=separator; i<myBox.size();i++)
{
if(testRotationPiece(m,n,myBox[i]))
if(testRotationPiece(myCoor,myBox[i]))
{
setPiece(m,n,myBox[i]);
//setPiece(myCoor.m,myCoor.n,myBox[i]);
setPiece(myCoor,myBox[i]);
myBox.erase(myBox.begin()+i);
return i;
}
@ -53,11 +54,12 @@ unsigned int Puzzle::tryAllPieces(unsigned int m, unsigned int n, vector<PuzzleP
}
//tests the myPart in all 4 rotations at position m, n
bool Puzzle::testRotationPiece(unsigned int m, unsigned int n, PuzzlePiece& myPart, int nrOfRotations)
bool Puzzle::testRotationPiece(coor myCoor, PuzzlePiece& myPart, int nrOfRotations)
{
for(int rotation=0; rotation < nrOfRotations; rotation++)
{
if(PlaceOfPartGood(m,n,myPart))
//coor myCoor(m,n);
if(PlaceOfPartGood(myCoor,myPart))
return 1;
//cout << "was rotated in testRotationPiece" << endl;
myPart.shift(1);
@ -69,7 +71,7 @@ bool Puzzle::testRotationPiece(unsigned int m, unsigned int n, PuzzlePiece& myPa
//insterts piece at position in box according to boxidentifier and removes piece from puzzle
//this returns the position after!! the puzzle piece was put back in! not the boxidentifier of the piece. look that up in other function.
unsigned int Puzzle::putBackIntoBox(unsigned int m, unsigned int n, vector<PuzzlePiece>& myBox)
unsigned int Puzzle::putBackIntoBox(coor myCoor, vector<PuzzlePiece>& 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<getRows();i++)
{
for(int j = 0; j < getCols();)
@ -208,10 +212,12 @@ void randomBox::createRandomPuzzle()
if(j==getCols()-1)
temporaryRandomPiece.setConnections(0b11001111 & temporaryRandomPiece.getConnections());
if(PlaceOfPartGood(j,i,temporaryRandomPiece))
myCoor.m = j;
myCoor.n = i;
if(PlaceOfPartGood(myCoor,temporaryRandomPiece))
{
temporaryRandomPiece.assignIdentifier();
setPiece(j,i,temporaryRandomPiece);
setPiece(myCoor,temporaryRandomPiece);
j++;
Box.push_back(temporaryRandomPiece);
}
@ -242,9 +248,9 @@ vector<PuzzlePiece> randomBox::shuffle()
}
//creates a random box size m, n, shuffles it, and then retuns it
vector<PuzzlePiece> createBox(unsigned int m, unsigned int n)
vector<PuzzlePiece> createBox(coor myCoor)
{
randomBox myFirstPuzzleBox(m,n);
randomBox myFirstPuzzleBox(myCoor.m, myCoor.n);
myFirstPuzzleBox.createRandomPuzzle();
return myFirstPuzzleBox.shuffle();
}

View File

@ -131,7 +131,7 @@ void abstractionlayer1solver(vector<LogEntry>& log, vector<PuzzlePiece*>& 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<LogEntry>& log, vector<PuzzlePiece*>& 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<LogEntry>& log, vector<PuzzlePiece*>& 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<LogEntry>& log, vector<PuzzlePiece*>& 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<LogEntry>& log, vector<PuzzlePiece*>& 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<LogEntry>& log, vector<PuzzlePiece*>& 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;

View File

@ -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<PuzzlePiece>& myBox, unsigned int separator);
unsigned int putBackIntoBox(unsigned int m, unsigned int n, vector<PuzzlePiece>& myBox);
bool testRotationPiece(coor myCoor, PuzzlePiece& myPart, int nrOfRotations=4); // 9th change: works
unsigned int tryAllPieces(coor myCoor, vector<PuzzlePiece>& myBox, unsigned int separator); // 3rd change: works
unsigned int putBackIntoBox(coor myCoor, vector<PuzzlePiece>& 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<PuzzlePiece> myBox);
vector<PuzzlePiece> createBox(unsigned int m, unsigned int n);
vector<PuzzlePiece> createBox(coor boxCoor); // 1st change: works
void numerateBox(vector<PuzzlePiece>& myBox);
bool next(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat);