001    package aima.search.nqueens;
002    
003    import java.util.List;
004    
005    import aima.basic.XYLocation;
006    import aima.search.framework.GoalTest;
007    
008    /**
009     * @author Ravi Mohan
010     * 
011     */
012    
013    public class NQueensGoalTest implements GoalTest {
014            NQueensBoard board;
015    
016            public boolean isGoalState(Object state) {
017    
018                    board = (NQueensBoard) state;
019                    return (allQueensPlaced() && allQueenPositionsHaveZeroAttacks(board
020                                    .getQueenPositions()));
021            }
022    
023            private boolean allQueensPlaced() {
024                    return board.getNumberOfQueensOnBoard() == board.getSize();
025            }
026    
027            private boolean allQueenPositionsHaveZeroAttacks(List positions) {
028    
029                    for (int i = 0; i < positions.size(); i++) {
030                            XYLocation location = (XYLocation) positions.get(i);
031                            if (board.getNumberOfAttacksOn(location) != 0) {
032                                    return false;
033                            }
034                    }
035                    return true;
036            }
037    }