minor code improvements

changed coor from m,n to col,row as per philipp request. added typecast to uint8 before bitmaskconversion.
This commit is contained in:
g-spacewhale 2017-12-16 00:10:52 +01:00
parent 2ef3f164c7
commit ba4d757c5a
4 changed files with 49 additions and 86 deletions

View File

@ -14,24 +14,24 @@ void PuzzlePiece::randomCenterPiece()
setConnections(0b00000000); setConnections(0b00000000);
if(rand()%2) if(rand()%2)
setConnections(getConnections() | 0b01000000); setConnections(getConnections() | (uint8_t)0b01000000);
else else
setConnections(getConnections() | 0b10000000); setConnections(getConnections() | (uint8_t)0b10000000);
if(rand()%2) if(rand()%2)
setConnections(getConnections() | 0b00010000); setConnections(getConnections() | (uint8_t)0b00010000);
else else
setConnections(getConnections() | 0b00100000); setConnections(getConnections() | (uint8_t)0b00100000);
if(rand()%2) if(rand()%2)
setConnections(getConnections() | 0b00000100); setConnections(getConnections() | (uint8_t)0b00000100);
else else
setConnections(getConnections() | 0b00001000); setConnections(getConnections() | (uint8_t)0b00001000);
if(rand()%2) if(rand()%2)
setConnections(getConnections() | 0b00000001); setConnections(getConnections() | (uint8_t)0b00000001);
else else
setConnections(getConnections() | 0b00000010); setConnections(getConnections() | (uint8_t)0b00000010);
} }
//tests the myPart in all 4 rotations at position m, n //tests the myPart in all 4 rotations at position m, n
@ -60,17 +60,17 @@ cout << "Old Box: ";
printBox(myBox); printBox(myBox);
cout << endl; cout << endl;
#endif #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); removePiece(myCoor);
return i+1; return i+1;
} }
} }
//using push back, if the element was the last element in the vector chain //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); removePiece(myCoor);
return myBox.size(); return myBox.size();
} }
@ -81,10 +81,10 @@ bool Puzzle::PlaceOfPartGood(coor myCoor, PuzzlePiece& myPart)
PuzzlePiece negativePart(0); PuzzlePiece negativePart(0);
negativePart.setConnections(negativePart.getConnections() | (getPiece(myCoor.m,myCoor.n+1).getConnections() & 0b11000000)); negativePart.setConnections(negativePart.getConnections() | (getPiece(myCoor.col,myCoor.row+1).getConnections() & (uint8_t)0b11000000));
negativePart.setConnections(negativePart.getConnections() | (getPiece(myCoor.m-1,myCoor.n).getConnections() & 0b00110000)); negativePart.setConnections(negativePart.getConnections() | (getPiece(myCoor.col-1,myCoor.row).getConnections() & (uint8_t)0b00110000));
negativePart.setConnections(negativePart.getConnections() | (getPiece(myCoor.m,myCoor.n-1).getConnections() & 0b00001100)); negativePart.setConnections(negativePart.getConnections() | (getPiece(myCoor.col,myCoor.row-1).getConnections() & (uint8_t)0b00001100));
negativePart.setConnections(negativePart.getConnections() | (getPiece(myCoor.m+1,myCoor.n).getConnections() & 0b00000011)); negativePart.setConnections(negativePart.getConnections() | (getPiece(myCoor.col+1,myCoor.row).getConnections() & (uint8_t)0b00000011));
negativePart.shift(2); negativePart.shift(2);
@ -118,43 +118,6 @@ bool Puzzle::PlaceOfPartGood(coor myCoor, PuzzlePiece& myPart)
return false; 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) //prints the true puzzle (without 0 edges)
void Puzzle::printPuzzle() void Puzzle::printPuzzle()
{ {
@ -178,24 +141,24 @@ void randomBox::createRandomAbstraction1()
coor myCoor; coor myCoor;
PuzzlePiece temporaryRandomPiece(0); PuzzlePiece temporaryRandomPiece(0);
for(int i=0;i<getRows();i++) for(unsigned int i=0;i<getRows();i++)
{ {
for(int j = 0; j < getCols();) for(unsigned int j = 0; j < getCols();)
{ {
//create random piece, set edges according to position and check if piece is good //create random piece, set edges according to position and check if piece is good
temporaryRandomPiece.randomCenterPiece(); temporaryRandomPiece.randomCenterPiece();
if(i==0) if(i==0)
temporaryRandomPiece.setConnections(0b00111111 & temporaryRandomPiece.getConnections()); temporaryRandomPiece.setConnections((uint8_t)0b00111111 & temporaryRandomPiece.getConnections());
if(i==getRows()-1) if(i==getRows()-1)
temporaryRandomPiece.setConnections(0b11110011 & temporaryRandomPiece.getConnections()); temporaryRandomPiece.setConnections((uint8_t)0b11110011 & temporaryRandomPiece.getConnections());
if(j==0) if(j==0)
temporaryRandomPiece.setConnections(0b11111100 & temporaryRandomPiece.getConnections()); temporaryRandomPiece.setConnections((uint8_t)0b11111100 & temporaryRandomPiece.getConnections());
if(j==getCols()-1) if(j==getCols()-1)
temporaryRandomPiece.setConnections(0b11001111 & temporaryRandomPiece.getConnections()); temporaryRandomPiece.setConnections((uint8_t)0b11001111 & temporaryRandomPiece.getConnections());
myCoor.m = j; myCoor.col = j;
myCoor.n = i; myCoor.row = i;
if(PlaceOfPartGood(myCoor,temporaryRandomPiece)) if(PlaceOfPartGood(myCoor,temporaryRandomPiece))
{ {
temporaryRandomPiece.assignIdentifier(); temporaryRandomPiece.assignIdentifier();
@ -245,9 +208,9 @@ void randomBox::createRandomAbstraction2()
} }
void randomBox::putAllIntoBox() { void randomBox::putAllIntoBox() {
for (int i = 0; i < getRows(); i++) for (unsigned int i = 0; i < getRows(); i++)
{ {
for (int j = 0; j < getCols(); j++) for (unsigned int j = 0; j < getCols(); j++)
{ {
Box.push_back(getPiece(j,i)); Box.push_back(getPiece(j,i));
} }
@ -283,7 +246,7 @@ vector<PuzzlePiece> randomBox::shuffle()
//creates a random box size m, n, shuffles it, and then retuns it //creates a random box size m, n, shuffles it, and then retuns it
vector<PuzzlePiece> createBox(coor myCoor) vector<PuzzlePiece> createBox(coor myCoor)
{ {
randomBox myFirstPuzzleBox(myCoor.m,myCoor.n); randomBox myFirstPuzzleBox(myCoor.col,myCoor.row);
myFirstPuzzleBox.createRandomAbstraction1(); myFirstPuzzleBox.createRandomAbstraction1();
myFirstPuzzleBox.createRandomAbstraction2(); myFirstPuzzleBox.createRandomAbstraction2();
myFirstPuzzleBox.putAllIntoBox(); myFirstPuzzleBox.putAllIntoBox();

View File

@ -17,8 +17,8 @@ bool next(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat)
else if(log.back().PieceCollector.size() > 1) else if(log.back().PieceCollector.size() > 1)
{ {
//moreLayers is 0, setbest is 1 //moreLayers is 0, setbest is 1
if(SetBestorMoreLayers()) setsolution(log,p_Box,puzzleMat); //if(SetBestorMoreLayers()) setsolution(log,p_Box,puzzleMat);
else solve(log,p_Box,puzzleMat); //else solve(log,p_Box,puzzleMat);
} }
//case last log exactly one solution //case last log exactly one solution
else if(log.back().PieceCollector.size() == 1) else if(log.back().PieceCollector.size() == 1)
@ -43,7 +43,7 @@ void createNextLogElement(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Pu
{ {
log.emplace_back(LogEntry()); log.emplace_back(LogEntry());
log.back().myCoor = calculateNextCoor(log, p_Box, puzzleMat); log.back().myCoor = calculateNextCoor(log, p_Box, puzzleMat);
getLayerDestructionPowerfromSurrounding(); //getLayerDestructionPowerfromSurrounding();
solve(log, p_Box,puzzleMat); solve(log, p_Box,puzzleMat);
} }
@ -57,8 +57,8 @@ coor calculateNextCoor(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzl
return {0,0}; return {0,0};
int m= log.rbegin()[1].myCoor.m; int m= log.rbegin()[1].myCoor.col;
int n= log.rbegin()[1].myCoor.n; int n= log.rbegin()[1].myCoor.row;
if(m<puzzleMat.getCols()-1) m++; if(m<puzzleMat.getCols()-1) m++;
@ -70,7 +70,7 @@ coor calculateNextCoor(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzl
void solve(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat) void solve(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat)
{ {
getNextHighestLayerworth(puzzleMat); //sets in abstractionLevel //getNextHighestLayerworth(puzzleMat); //sets in abstractionLevel
//status(log,p_Box,puzzleMat); //status(log,p_Box,puzzleMat);
switch(log.back().abstractionLevel) switch(log.back().abstractionLevel)
{ {
@ -83,10 +83,10 @@ void solve(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMat
break; break;
} }
capLogElements(log); //capLogElements(log);
calculateWorth(log); //calculateWorth(log);
calculateTrueDestructionPower(log,puzzleMat); //calculateTrueDestructionPower(log,puzzleMat);
calculateNewCombinedProbablility(log); //calculateNewCombinedProbablility(log);
} }
@ -237,7 +237,7 @@ void status(vector<LogEntry>& log, vector<PuzzlePiece*>& p_Box, Puzzle& puzzleMa
cout << "isset: 1" << endl; cout << "isset: 1" << endl;
else else
cout << "isset: 0" << endl; 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; cout << endl;
@ -258,5 +258,5 @@ void calculateTrueDestructionPower(vector<LogEntry>& log, Puzzle& puzzleMat, flo
{ {
//hier muss noch rein, wo die zeit der Abstractionlevels gespeichter wird //hier muss noch rein, wo die zeit der Abstractionlevels gespeichter wird
float destructionPower=sqrt(Layerworth * log.back().abstractionLevel); 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);
} }

View File

@ -8,8 +8,8 @@ using namespace std;
class coor class coor
{ {
public: public:
int m, n; unsigned int col, row;
coor(int newm=-1,int newn=-1): m(newm), n(newn) coor(int newcol=-1,int newrow=-1): col(newcol), row(newrow)
{} {}
}; };
@ -84,7 +84,7 @@ public:
{ {
if(PlaceOfPartGood(myCoor,newPiece)) if(PlaceOfPartGood(myCoor,newPiece))
{ {
Matrix[myCoor.n+1][myCoor.m+1] = newPiece; Matrix[myCoor.row+1][myCoor.col+1] = newPiece;
return 1; return 1;
} }
return 0; return 0;
@ -93,7 +93,7 @@ public:
//removes piece and instead puts undefined piece //removes piece and instead puts undefined piece
bool removePiece(coor myCoor) // 6th change: works 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; return 1;
} }

View File

@ -7,7 +7,7 @@ unsigned int PuzzlePiece::idcount(0);
int main() int main()
{ {
unsigned int cols=2, rows=3; unsigned int cols=5, rows=6;
//some basic random puzzle stuff //some basic random puzzle stuff
vector<PuzzlePiece> myFirstBox = createBox(coor(cols,rows)); vector<PuzzlePiece> myFirstBox = createBox(coor(cols,rows));