//Raphael Maenle - 11.11.2017 #include #include #include #include #include #include using namespace std; class PuzzlePiece { public: unsigned char Ringbuffer; PuzzlePiece(unsigned int flag = 0) { shifts=0; boxidentifier=-1; switch(flag) { case 0: Ringbuffer=0b00000000; break; case 1: Ringbuffer=0b11111111; break; case 3: randomCenterPiece(); break; } } void Shift(unsigned int moves) { //Shift ringbuffer to the right n number of times shifts = (shifts+moves)%4; Ringbuffer = ((Ringbuffer >> (moves*2)) | (Ringbuffer << sizeof(unsigned char)*8 - (moves*2))); } void printPiece() { cout << bitset (Ringbuffer); } //makes piece to random center piece void randomCenterPiece() { this->Ringbuffer= 0b00000000; if(rand()%2) this->Ringbuffer |= 0b01000000; else this->Ringbuffer |= 0b10000000; if(rand()%2) this->Ringbuffer |= 0b00010000; else this->Ringbuffer |= 0b00100000; if(rand()%2) this->Ringbuffer |= 0b00000100; else this->Ringbuffer |= 0b00001000; if(rand()%2) this->Ringbuffer |= 0b00000001; else this->Ringbuffer |= 0b00000010; } void assignIdentifier() { identifier = idcount; idcount++; } unsigned int getIdentifier() { return identifier; } void setBoxIdentifier(int new_boxid) { boxidentifier = new_boxid; } int getBoxIdentifier() { return boxidentifier; } private: unsigned int shifts; unsigned int identifier; int boxidentifier; static unsigned int idcount; }; unsigned int PuzzlePiece::idcount(0); //saves a matrix cluster of puzzlepieces class Puzzle { public: Puzzle(uint m = 7, uint n = 4): col(m), row(n) { Matrix = new PuzzlePiece* [n+2]; //Box = new PuzzlePiece[n*m]; for(int i = 0;iPlaceOfPartGood(m,n,newPiece)) { Matrix[n+1][m+1] = newPiece; return 1; } return 0; } bool removePiece(uint m,uint n) { Matrix[n+1][m+1] = 0b11111111; return 1; } PuzzlePiece getPiece(uint m,uint n) { return Matrix[n+1][m+1]; } PuzzlePiece sudogetPiece(uint m,uint n) { return Matrix[n][m]; } //check if place of the part in the matrix is correct bool PlaceOfPartGood(unsigned int m, unsigned int n, PuzzlePiece& myPart); bool testRotationPiece(unsigned int m, unsigned int n, PuzzlePiece& myPart); 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); void printPuzzle(); uint getCols() { return col; } uint getRows() { return row; } //creates a random puzzle of size m columns, n rows void createRandomPuzzle(); //shuffles Box; vector Shuffle(); //prints box void printBox(); private: uint row; uint col; PuzzlePiece** Matrix; vector Box; void sudosetPiece(uint m,uint n,PuzzlePiece newPiece) { if(this->PlaceOfPartGood(m,n,newPiece)) Matrix[n][m] = newPiece; } void sudoprintPuzzle() { for(int i=0;i (topPart) << endl; unsigned char rightPart = ((negativePart.Ringbuffer & 0b00110000) ^ (tmpPuzzlePiece.Ringbuffer & 0b00110000)); cout << "rightPart: " << bitset (rightPart) << endl; unsigned char lowPart = ((negativePart.Ringbuffer & 0b00001100) ^ (tmpPuzzlePiece.Ringbuffer & 0b00001100)); cout << "lowPart: " << bitset (lowPart) << endl; unsigned char leftPart = ((negativePart.Ringbuffer & 0b00000011) ^ (tmpPuzzlePiece.Ringbuffer & 0b00000011)); cout << "leftPart: " << bitset (leftPart) << endl; */ if ( //simplify ( ((((negativePart.Ringbuffer & 0b11000000) ^ (tmpPuzzlePiece.Ringbuffer & 0b11000000)) != 0b00000000) && (tmpPuzzlePiece.Ringbuffer & 0b11000000) != 0b00000000) || ((((negativePart.Ringbuffer & 0b11000000) == 0b11000000) || ((tmpPuzzlePiece.Ringbuffer & 0b11000000) == 0b11000000)) && (tmpPuzzlePiece.Ringbuffer & 0b11000000) != 0b00000000) || (((negativePart.Ringbuffer & 0b11000000) == 0b00000000) && ((tmpPuzzlePiece.Ringbuffer & 0b11000000) == 0b00000000)) ) && ( ((((negativePart.Ringbuffer & 0b00110000) ^ (tmpPuzzlePiece.Ringbuffer & 0b00110000)) != 0b00000000) && (tmpPuzzlePiece.Ringbuffer & 0b001100) != 0b00000000) || ((((negativePart.Ringbuffer & 0b00110000) == 0b00110000) || ((tmpPuzzlePiece.Ringbuffer & 0b00110000) == 0b00110000)) && (tmpPuzzlePiece.Ringbuffer & 0b00110000) != 0b00000000) || (((negativePart.Ringbuffer & 0b00110000) == 0b00000000) && ((tmpPuzzlePiece.Ringbuffer & 0b00110000) == 0b00000000)) ) && ( ((((negativePart.Ringbuffer & 0b00001100) ^ (tmpPuzzlePiece.Ringbuffer & 0b00001100)) != 0b00000000) && (tmpPuzzlePiece.Ringbuffer & 0b00001100) != 0b00000000) || ((((negativePart.Ringbuffer & 0b00001100) == 0b00001100) || ((tmpPuzzlePiece.Ringbuffer & 0b00001100) == 0b00001100)) && (tmpPuzzlePiece.Ringbuffer & 0b00001100) != 0b00000000) || (((negativePart.Ringbuffer & 0b00001100) == 0b00000000) && ((tmpPuzzlePiece.Ringbuffer & 0b00001100) == 0b00000000)) ) && ( ((((negativePart.Ringbuffer & 0b00000011) ^ (tmpPuzzlePiece.Ringbuffer & 0b00000011)) != 0b00000000) && (tmpPuzzlePiece.Ringbuffer & 0b00000011) != 0b00000000) || ((((negativePart.Ringbuffer & 0b00000011) == 0b00000011) || ((tmpPuzzlePiece.Ringbuffer & 0b00000011) == 0b00000011)) && (tmpPuzzlePiece.Ringbuffer & 0b00000011) != 0b00000000) || (((negativePart.Ringbuffer & 0b00000011) == 0b00000000) && ((tmpPuzzlePiece.Ringbuffer & 0b00000011) == 0b00000000)) ) ) return 1; return 0; } void Puzzle::printPuzzle() { for(int i=1;i::iterator i = Box.begin(); i != Box.end(); i++) { (*i).printPiece(); cout << ' '; } cout << endl; } vector Puzzle::Shuffle() { random_shuffle(Box.begin(),Box.end()); return Box; } bool Puzzle::testRotationPiece(unsigned int m, unsigned int n, PuzzlePiece& myPart) { for(int rotation=0; rotation < 4; rotation++) { myPart.Shift(1); myPart.printPiece(); cout << endl; if(PlaceOfPartGood(m,n, myPart)) { return 1; } } return 0; } //tries all pieces in box from separator to end and places fitting into matrix. removes fitting piece //use separator if you have to retract to a position unsigned int Puzzle::tryAllPieces(unsigned int m, unsigned int n, vector& myBox, unsigned int separator) { for(int i=separator; i& myBox) { for(int i = 0; i < myBox.size();i++) { if(myBox[i].getBoxIdentifier()>getPiece(m,n).getBoxIdentifier()) { myBox.insert(myBox.begin()+i,getPiece(m,n)); removePiece(m,n); return i; } } } vector createBox(uint m, uint n) { Puzzle myFirstPuzzle(m,n); myFirstPuzzle.createRandomPuzzle(); return myFirstPuzzle.Shuffle(); }