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);
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<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
temporaryRandomPiece.randomCenterPiece();
if(i==0)
temporaryRandomPiece.setConnections(0b00111111 & temporaryRandomPiece.getConnections());
temporaryRandomPiece.setConnections((uint8_t)0b00111111 & temporaryRandomPiece.getConnections());
if(i==getRows()-1)
temporaryRandomPiece.setConnections(0b11110011 & temporaryRandomPiece.getConnections());
temporaryRandomPiece.setConnections((uint8_t)0b11110011 & temporaryRandomPiece.getConnections());
if(j==0)
temporaryRandomPiece.setConnections(0b11111100 & temporaryRandomPiece.getConnections());
temporaryRandomPiece.setConnections((uint8_t)0b11111100 & temporaryRandomPiece.getConnections());
if(j==getCols()-1)
temporaryRandomPiece.setConnections(0b11001111 & temporaryRandomPiece.getConnections());
temporaryRandomPiece.setConnections((uint8_t)0b11001111 & temporaryRandomPiece.getConnections());
myCoor.m = j;
myCoor.n = i;
myCoor.col = j;
myCoor.row = i;
if(PlaceOfPartGood(myCoor,temporaryRandomPiece))
{
temporaryRandomPiece.assignIdentifier();
@ -245,9 +208,9 @@ void randomBox::createRandomAbstraction2()
}
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));
}
@ -283,7 +246,7 @@ vector<PuzzlePiece> randomBox::shuffle()
//creates a random box size m, n, shuffles it, and then retuns it
vector<PuzzlePiece> createBox(coor myCoor)
{
randomBox myFirstPuzzleBox(myCoor.m,myCoor.n);
randomBox myFirstPuzzleBox(myCoor.col,myCoor.row);
myFirstPuzzleBox.createRandomAbstraction1();
myFirstPuzzleBox.createRandomAbstraction2();
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)
{
//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<LogEntry>& log, vector<PuzzlePiece*>& 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<LogEntry>& log, vector<PuzzlePiece*>& 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<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)
{
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<LogEntry>& log, vector<PuzzlePiece*>& 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<LogEntry>& log, vector<PuzzlePiece*>& 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<LogEntry>& 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);
}

View File

@ -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;
}

View File

@ -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<PuzzlePiece> myFirstBox = createBox(coor(cols,rows));