queenExistsAt() public method

public queenExistsAt ( XYLocation l ) : bool
l AIMA.Core.Util.DataStructure.XYLocation
return bool
        //
        // START - Interface FitnessFunction
        public double getValue(String individual)
        {
            double fitness = 0;

            NQueensBoard board     = getBoardForIndividual(individual);
            int          boardSize = board.getSize();

            // Calculate the number of non-attacking pairs of queens (refer to AIMA
            // page 117).
            List <XYLocation> qPositions = board.getQueenPositions();

            for (int fromX = 0; fromX < (boardSize - 1); fromX++)
            {
                for (int toX = fromX + 1; toX < boardSize; toX++)
                {
                    int  fromY            = qPositions.get(fromX).getYCoOrdinate();
                    bool nonAttackingPair = true;
                    // Check right beside
                    int toY = fromY;
                    if (board.queenExistsAt(new XYLocation(toX, toY)))
                    {
                        nonAttackingPair = false;
                    }
                    // Check right and above
                    toY = fromY - (toX - fromX);
                    if (toY >= 0)
                    {
                        if (board.queenExistsAt(new XYLocation(toX, toY)))
                        {
                            nonAttackingPair = false;
                        }
                    }
                    // Check right and below
                    toY = fromY + (toX - fromX);
                    if (toY < boardSize)
                    {
                        if (board.queenExistsAt(new XYLocation(toX, toY)))
                        {
                            nonAttackingPair = false;
                        }
                    }

                    if (nonAttackingPair)
                    {
                        fitness += 1.0;
                    }
                }
            }

            return(fitness);
        }
            public HashSet <Action> actions(Object state)
            {
                HashSet <Action> actions = new LinkedHashSet <Action>();
                NQueensBoard     board   = (NQueensBoard)state;

                for (int i = 0; i < board.getSize(); i++)
                {
                    for (int j = 0; j < board.getSize(); j++)
                    {
                        XYLocation loc = new XYLocation(i, j);
                        if (!board.queenExistsAt(loc))
                        {
                            actions
                            .Add(new QueenAction(QueenAction.MOVE_QUEEN,
                                                 loc));
                        }
                    }
                }
                return(actions);
            }
Example #3
0
        public override bool Equals(Object o)
        {
            if (this == o)
            {
                return(true);
            }
            if ((o == null) || (this.getClass() != o.getClass()))
            {
                return(false);
            }
            NQueensBoard      aBoard = (NQueensBoard)o;
            bool              retVal = true;
            List <XYLocation> locs   = getQueenPositions();

            foreach (XYLocation loc in locs)
            {
                if (!(aBoard.queenExistsAt(loc)))
                {
                    retVal = false;
                }
            }
            return(retVal);
        }