text cover

Data Abstraction and Problem Solving with C++

Walls and Mirrors

by Frank M. Carrano

Addison Wesley Logo

placeQueens.cpp

Go to the documentation of this file.
00001 
00016 bool Board::placeQueens(Queen *queenPtr)
00017 {
00018    // Base case.  Trying to place Queen in a non-existent column.
00019    if (queenPtr->getCol() >= BOARD_SIZE)
00020    {  delete queenPtr;
00021       return true;
00022    }  // end if
00023 
00024    bool isQueenPlaced = false;
00025 
00026    while (!isQueenPlaced && queenPtr->getRow() < BOARD_SIZE)
00027    {  // If the queen can be attacked, then try moving it to the
00028       // next row in the current column.
00029       if (queenPtr->isUnderAttack())
00030     queenPtr->nextRow();
00031       // Else put this queen on the board and try putting a new
00032       // queen in the first row of the next column.
00033       else
00034       {  setQueen(queenPtr);
00035          Queen *newQueenPtr = new Queen(0, queenPtr->getCol() + 1);
00036          isQueenPlaced = placeQueens(newQueenPtr);
00037          // If it wasn't possible to put the new Queen in the next
00038     // column, backtrack by deleting the new Queen and
00039     // removing the last Queen placed and moving it down one
00040     // row.
00041     if (!isQueenPlaced)
00042     {  delete newQueenPtr;
00043             removeQueen();
00044        queenPtr->nextRow();
00045     }  // end if
00046       }  // end if
00047    }  // end while
00048    return isQueenPlaced;
00049 }  // end placeQueens

Generated on Sun Aug 27 13:27:58 2006 for AWLogo by  doxygen 1.4.6