1: // Created by Frank M. Carrano and Tim Henry. 2: // Copyright (c) 2013 __Pearson Education__. All rights reserved. 4: bool Board::placeQueens(Queen* queenPtr) 5: { 6: // Base case: Try to place a queen in a nonexistent column. 7: if (queenPtr->getCol() >= BOARD_SIZE) 8: { 9: delete queenPtr; 10: return true; 11: } // end if 12: 13: bool isQueenPlaced = false; 14: while (!isQueenPlaced && queenPtr->getRow() < BOARD_SIZE) 15: { 16: // If the queen can be attacked, try moving it 17: // to the next row in the current column 18: if (queenPtr->isUnderAttack()) 19: queenPtr->nextRow(); 20: else 21: { 22: // Put this queen on the board and try putting a 23: // new queen in the first row of the next column 24: setQueen(queenPtr); 25: Queen* newQueenPtr = new Queen(0, queenPtr->getCol() + 1); 26: isQueenPlaced = placeQueens(newQueenPtr); 27: 28: // If it wasn't possible to put the new queen in the next column, 29: // backtrack by deleting the new queen and moving the last 30: // queen placed down one row 31: if (!isQueenPlaced) 32: { 33: delete newQueenPtr; 34: removeQueen(); 35: queenPtr->nextRow(); 36: } // end if 37: } // end if 38: } // end while 39: 40: return isQueenPlaced; 41: } // end placeQueens