/* this version is stripped-down. It is meant to find the solution and the ONLY OUTPUT TO THE SCREEN ARE THE KUIP COMMANDS FOR A PAW MACRO. Hence, it is meant to have stdout redirected to a file, which will then be fed to paw */ #include #include #include #include #include #include "Space.h" #include "Piece.h" #include "Board.h" #include "Move.h" using namespace std; bool SucceedInserting(Piece* piece,Board* board,int icol,int irow){ while (board->InsertPiece(piece,icol,irow)!=0){ // doesnt fit in this configuration. Try rotating if (piece->RotateCounterClockwise()!=0){ // we have tried all rotations at this centerpoint. Try recentering if (piece->RecenterAtNextBlackSquare()!=0){ // that's it. We have tried everything. It does not fit at all return false; // means doesn't fit } } } return true; // that means there was no problem. The Piece was inserted at the next available Space on the Board } // in this following function, we increment the configuration once first, then go to the SucceedInserting() bool ReconfigureThenTryInserting(Piece* piece,Board* board,int icol, int irow){ if (piece->RotateCounterClockwise()!=0){ // we have tried all rotations at this centerpoint. Try recentering if (piece->RecenterAtNextBlackSquare()!=0){ // that's it. We have tried everything. It does not fit at all return false; // means doesn't fit } } return SucceedInserting(piece,board,icol,irow); } bool IncrementConfiguration(Piece* p){ if (p->RotateCounterClockwise()!=0){ if (p->RecenterAtNextBlackSquare()!=0){ return false; } } return true; } int main(){ int junky; Board TheBoard; // cout << "\n Empty Board\n"; // TheBoard.Draw(); vector AllPieces; Space* sp; Piece* thePiece; // define Piece#1 thePiece = new Piece(); thePiece->SetId(1); sp = new Space(black,0,0); // first Square should always be at 0,0 then others are relative to it sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(1); sp->SetBorder(2); thePiece->AddSquare(sp); sp = new Space(red,-1,0); sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(black,-1,-1); sp->SetOccupied(); sp->SetBorder(1); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,-1,-2); sp->SetOccupied(); sp->SetBorder(1); sp->SetBorder(2); sp->SetBorder(3); thePiece->AddSquare(sp); AllPieces.push_back(thePiece); // define Piece#2 thePiece = new Piece(); thePiece->SetId(2); sp = new Space(black,0,0); // first Square should always be at 0,0 then others are relative to it sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(1); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,0,-1); sp->SetOccupied(); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(black,1,-1); sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(1); sp->SetBorder(2); thePiece->AddSquare(sp); sp = new Space(black,0,-2); sp->SetOccupied(); sp->SetBorder(1); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,0,-3); sp->SetOccupied(); sp->SetBorder(1); sp->SetBorder(2); sp->SetBorder(3); thePiece->AddSquare(sp); AllPieces.push_back(thePiece); // define Piece#3 thePiece = new Piece(); thePiece->SetId(3); sp = new Space(black,0,0); // first Square should always be at 0,0 then others are relative to it sp->SetOccupied(); sp->SetBorder(2); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,0,1); sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(1); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,1,0); sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(1); thePiece->AddSquare(sp); sp = new Space(black,1,-1); sp->SetOccupied(); sp->SetBorder(1); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,1,-2); sp->SetOccupied(); sp->SetBorder(1); sp->SetBorder(2); sp->SetBorder(3); thePiece->AddSquare(sp); AllPieces.push_back(thePiece); // define Piece#4 (note #4 and #12 are identical pieces) thePiece = new Piece(); thePiece->SetId(4); sp = new Space(black,0,0); // first Square should always be at 0,0 then others are relative to it sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(2); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,1,0); sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(1); thePiece->AddSquare(sp); sp = new Space(black,1,-1); sp->SetOccupied(); sp->SetBorder(1); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,1,-2); sp->SetOccupied(); sp->SetBorder(2); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(black,2,-2); sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(1); sp->SetBorder(2); thePiece->AddSquare(sp); AllPieces.push_back(thePiece); // define Piece#5 thePiece = new Piece(); thePiece->SetId(5); sp = new Space(black,0,0); // first Square should always be at 0,0 then others are relative to it sp->SetOccupied(); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,0,1); sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(1); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,1,0); sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(1); thePiece->AddSquare(sp); sp = new Space(red,0,-1); sp->SetOccupied(); sp->SetBorder(2); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(black,1,-1); sp->SetOccupied(); sp->SetBorder(2); thePiece->AddSquare(sp); sp = new Space(red,2,-1); sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(1); sp->SetBorder(2); thePiece->AddSquare(sp); AllPieces.push_back(thePiece); // define Piece#6 thePiece = new Piece(); thePiece->SetId(6); sp = new Space(black,0,0); // first Square should always be at 0,0 then others are relative to it sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(1); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,0,-1); sp->SetOccupied(); sp->SetBorder(1); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(black,0,-2); sp->SetOccupied(); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,0,-3); sp->SetOccupied(); sp->SetBorder(1); sp->SetBorder(2); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,1,-2); sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(1); sp->SetBorder(2); thePiece->AddSquare(sp); AllPieces.push_back(thePiece); // define Piece#7 thePiece = new Piece(); thePiece->SetId(7); sp = new Space(black,0,0); // first Square should always be at 0,0 then others are relative to it sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(1); thePiece->AddSquare(sp); sp = new Space(red,0,-1); sp->SetOccupied(); sp->SetBorder(1); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(black,0,-2); sp->SetOccupied(); sp->SetBorder(1); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,0,-3); sp->SetOccupied(); sp->SetBorder(1); sp->SetBorder(2); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,-1,0); sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(2); sp->SetBorder(3); thePiece->AddSquare(sp); AllPieces.push_back(thePiece); // define Piece#8 thePiece = new Piece(); thePiece->SetId(8); sp = new Space(black,0,0); // first Square should always be at 0,0 then others are relative to it sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(1); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,0,-1); sp->SetOccupied(); sp->SetBorder(1); sp->SetBorder(2); thePiece->AddSquare(sp); sp = new Space(black,-1,-1); sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,-1,-2); sp->SetOccupied(); sp->SetBorder(1); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(black,-1,-3); sp->SetOccupied(); sp->SetBorder(1); sp->SetBorder(2); sp->SetBorder(3); thePiece->AddSquare(sp); AllPieces.push_back(thePiece); // define Piece#9 thePiece = new Piece(); thePiece->SetId(9); sp = new Space(black,0,0); // first Square should always be at 0,0 then others are relative to it sp->SetOccupied(); sp->SetBorder(1); thePiece->AddSquare(sp); sp = new Space(red,0,1); sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(1); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,0,-1); sp->SetOccupied(); sp->SetBorder(1); thePiece->AddSquare(sp); sp = new Space(black,0,-2); sp->SetOccupied(); sp->SetBorder(1); sp->SetBorder(2); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,-1,0); sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(black,-1,-1); sp->SetOccupied(); sp->SetBorder(2); sp->SetBorder(3); thePiece->AddSquare(sp); AllPieces.push_back(thePiece); // define Piece#10 thePiece = new Piece(); thePiece->SetId(10); sp = new Space(black,0,0); // first Square should always be at 0,0 then others are relative to it sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(2); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,1,0); sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(1); thePiece->AddSquare(sp); sp = new Space(black,1,-1); sp->SetOccupied(); sp->SetBorder(1); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,1,-2); sp->SetOccupied(); sp->SetBorder(1); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(black,1,-3); sp->SetOccupied(); sp->SetBorder(1); sp->SetBorder(2); sp->SetBorder(3); thePiece->AddSquare(sp); AllPieces.push_back(thePiece); // define Piece#11 thePiece = new Piece(); thePiece->SetId(11); sp = new Space(black,0,0); // first Square should always be at 0,0 then others are relative to it sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(1); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,0,-1); sp->SetOccupied(); thePiece->AddSquare(sp); sp = new Space(black,0,-2); sp->SetOccupied(); thePiece->AddSquare(sp); sp = new Space(red,0,-3); sp->SetOccupied(); sp->SetBorder(1); sp->SetBorder(2); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(black,-1,-1); sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,-1,-2); sp->SetOccupied(); sp->SetBorder(2); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(black,1,-1); sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(1); thePiece->AddSquare(sp); sp = new Space(red,1,-2); sp->SetOccupied(); sp->SetBorder(1); sp->SetBorder(2); thePiece->AddSquare(sp); AllPieces.push_back(thePiece); // define Piece#12 (note #4 and #12 are identical pieces) thePiece = new Piece(); thePiece->SetId(12); sp = new Space(black,0,0); // first Square should always be at 0,0 then others are relative to it sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(2); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,1,0); sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(1); thePiece->AddSquare(sp); sp = new Space(black,1,-1); sp->SetOccupied(); sp->SetBorder(1); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(red,1,-2); sp->SetOccupied(); sp->SetBorder(2); sp->SetBorder(3); thePiece->AddSquare(sp); sp = new Space(black,2,-2); sp->SetOccupied(); sp->SetBorder(0); sp->SetBorder(1); sp->SetBorder(2); thePiece->AddSquare(sp); AllPieces.push_back(thePiece); list PiecesOnBoard; list PiecesOffBoard; // none of the Pieces, as yet, are on the Board for(vector::iterator iter = AllPieces.begin(); iter < AllPieces.end(); iter++){ PiecesOffBoard.push_back(*iter); } //-------------------------- end of visualization/debugging section ----------------------------- //---- Get real. Solve the puzzle // These are the lists defined above: // list PiecesOnBoard; // list PiecesOffBoard; // vector AllPieces; vector TheMoves; Move* FirstMove = new Move(AllPieces, TheBoard.NextAvailableBlackSpace().Column(), TheBoard.NextAvailableBlackSpace().Row()); TheMoves.push_back(FirstMove); int NumConfigsLookedAt=0; while (TheMoves.size() < AllPieces.size()+1){ // cout << "Number of moves made is " << TheMoves.size() << endl; Move* CurrentMove = TheMoves[TheMoves.size()-1]; if (CurrentMove->Attempt(&TheBoard) == 0){ // 0 return value means successful attempt TheBoard.DrawForPaw(); NumConfigsLookedAt++; cout << "title " << NumConfigsLookedAt << endl; /* ---- SKIP THE .GIF OUTPUT FOR NOW -- REINSERT TO MAKE THE GIFS! if (NumConfigsLookedAt < 10){ cout << "pic/print p0000" << NumConfigsLookedAt << ".gif" << endl;} else{ if(NumConfigsLookedAt < 100){ cout << "pic/print p000" << NumConfigsLookedAt << ".gif" << endl;} else{ if(NumConfigsLookedAt < 1000){ cout << "pic/print p00" << NumConfigsLookedAt << ".gif" << endl;} else{ if(NumConfigsLookedAt < 10000){ cout << "pic/print p0" << NumConfigsLookedAt << ".gif" << endl;} else{ cout << "pic/print p" << NumConfigsLookedAt << ".gif" << endl;} } } } */ // cout << " Move worked -- "; if (TheMoves.size()==12){ // cout << " and that's it!!\n"; break; } else { // cout << " make the next move " << endl; } Move* NextMove = new Move(AllPieces, TheBoard.NextAvailableBlackSpace().Column(), TheBoard.NextAvailableBlackSpace().Row()); TheMoves.push_back(NextMove); } else{ // cout << " Move failed -- delete it " << endl; delete CurrentMove; TheMoves.pop_back(); } assert(TheMoves.size()); // essentially demanding that at least ONE move must be working !!! } // done !! // cout << "Number of configurations looked at was " << NumConfigsLookedAt << endl; /* TheBoard.Draw(); */ /* cout << "Here are the Pieces\n"; */ /* for (int ip=0; ipIsOnBoard()){ */ /* cout << "Piece #" << AllPieces[ip]->GetId() << " is at (col,row)=(" */ /* << AllPieces[ip]->ColumnOfPieceOnBoard() << "," */ /* << AllPieces[ip]->RowOfPieceOnBoard() << ")\n"; */ /* } */ /* else{ */ /* cout << "Piece #" << AllPieces[ip]->GetId() << " is not on the board\n"; */ /* } */ /* } */ TheBoard.DrawForPaw(); return 0; }