Logo Search packages:      
Sourcecode: filler version File versions  Download package

BitSet friendless::games::filler::OptimalRobotPlayer::getBestGoalColours ( int  goal  )  [inline, protected, inherited]

Returns:
the colour which will get us to the goal quickest.

Definition at line 39 of file OptimalRobotPlayer.java.

References friendless::games::filler::FillerPlayerSpace::distance, friendless::games::filler::FillerPlayerSpace::listed, friendless::games::filler::FillerModel::pieces, and friendless::games::filler::FillerPlayerSpace::resetListed().

Referenced by friendless::games::filler::player::Dieter::turn().

                                                  {
        int[] distance = space.distance;
        int distanceToGoal = distance[goal];
        if (distanceToGoal <= 0) new BitSet(FillerSettings.NUM_COLOURS);
        /* We now know the distance to the goal.
         * We have to find a sequence of locations with ever-decreasing distances
         * until we get to locations which are distance 0, i.e. on the border.
         * We then choose any colour from those distance 0 locations.
         */
        space.resetListed();
        boolean[] listed = space.listed;
        int[] pieces = model.pieces;
        int[] thisDistance = new int[distance.length];
        int thisDistanceIndex = 0;
        int[] lowerDistance = new int[distance.length];
        int lowerDistanceIndex = 0;
        thisDistance[thisDistanceIndex++] = goal;
        // find all the pieces of the same colour joined to the goal
        while (thisDistanceIndex > 0) {
            int p = thisDistance[--thisDistanceIndex];
            if (listed[p]) continue;
            lowerDistance[lowerDistanceIndex++] = p;
            listed[p] = true;
            int[] ns = FillerModel.neighbours(p);
            for (int i=0; i<ns.length; i++) {
                int q = ns[i];
                if (q == FillerModel.NO_NEIGHBOUR) break;
                if (listed[q]) continue;
                if (pieces[q] == pieces[goal]) {
                    thisDistance[thisDistanceIndex++] = q;
                }
            }
        }
        int[] tempArray = lowerDistance;
        lowerDistance = thisDistance;
        thisDistance = tempArray;
        thisDistanceIndex = lowerDistanceIndex;
        lowerDistanceIndex = 0;
        // now for each distance, find all the pieces in the next distance down
        while (distanceToGoal > 1) {
            while (thisDistanceIndex > 0) {
                int p = thisDistance[--thisDistanceIndex];
                int[] ns = FillerModel.neighbours(p);
                for (int i=0; i<ns.length; i++) {
                    int q = ns[i];
                    if (q == FillerModel.NO_NEIGHBOUR) break;
                    if (listed[q]) continue;
                    int distq = distance[q];
                    if (distq >= 0 && distq < distanceToGoal) {
                        lowerDistance[lowerDistanceIndex++] = q;
                        listed[q] = true;
                    }
                }
            }
            // add neighbours of same colour attached to pieces at the lower distance (yuk)
            tempArray = lowerDistance;
            lowerDistance = thisDistance;
            thisDistance = tempArray;
            thisDistanceIndex = lowerDistanceIndex;
            lowerDistanceIndex = 0;
            while (thisDistanceIndex > 0) {
                int p = thisDistance[--thisDistanceIndex];
                lowerDistance[lowerDistanceIndex++] = p;
                int[] ns = FillerModel.neighbours(p);
                for (int i=0; i<ns.length; i++) {
                    int q = ns[i];
                    if (q == FillerModel.NO_NEIGHBOUR) break;
                    if (listed[q]) continue;
                    if (pieces[p] == pieces[q]) {
                        thisDistance[thisDistanceIndex++] = q;
                        listed[q] = true;
                    }
                }
            }
            /* We have finished with the thisDistance array, we now move down to
             * the lower distance. To do this we swap the arrays and reset the
             * index on the lowerDistance one.
             */
            tempArray = lowerDistance;
            lowerDistance = thisDistance;
            thisDistance = tempArray;
            thisDistanceIndex = lowerDistanceIndex;
            lowerDistanceIndex = 0;
            distanceToGoal--;
        }
        /* All the locations at distance 0 which will get us to the target are in thisDistance. */
        BitSet colours = new BitSet(FillerSettings.NUM_COLOURS);
        for (int i=0; i<thisDistanceIndex; i++) {
            colours.set(pieces[thisDistance[i]]);
        }
        return colours;
    }


Generated by  Doxygen 1.6.0   Back to index