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

static void friendless::games::filler::FillerModel::allocateDistance ( FillerModel  model,
FillerPlayerSpace  space 
) [inline, static, package]

Assumes that allocateTypes has already been called with this model and this space, so the information in space accurately reflects the state of the model.

Definition at line 312 of file FillerModel.java.

References BORDER, friendless::games::filler::FillerPlayerSpace::border, friendless::games::filler::FillerPlayerSpace::counted, friendless::games::filler::FillerPlayerSpace::distance, friendless::games::filler::FillerPlayerSpace::listed, NO_DISTANCE, NO_NEIGHBOUR, pieces, friendless::games::filler::FillerPlayerSpace::resetListed(), SHARED_BORDER, UNKNOWN_DISTANCE, and UNREACHABLE_DISTANCE.

                                                                             {
        int[] counted = space.counted;
        int[] distance = space.distance;
        int[] border = space.border;
        int[] pieces = model.pieces;
        space.resetListed();
        boolean[] listed = space.listed;
        int idx = 0;
        // allocate the distances we know immediately
        for (int i=0; i<distance.length; i++) {
            int ci = counted[i];
            if (!valid(i)) {
                distance[i] = UNREACHABLE_DISTANCE;
            } else if (MUST_BE_MINE.get(ci)) {
                distance[i] = NO_DISTANCE;
            } else if (MUST_BE_HIS.get(ci)) {
                distance[i] = UNREACHABLE_DISTANCE;
            } else if (ci == BORDER || ci == SHARED_BORDER) {
                distance[i] = 1;
                // remember where the border is
                border[idx++] = i;
                listed[i] = true;
            } else {
                distance[i] = UNKNOWN_DISTANCE;
            }
        }
        // figure out the unknown distances
        while (idx > 0) {
            // take a point which is currently on the border
            int p = border[--idx];
            listed[p] = false;
            int distp = distance[p];
            int[] ns = neighs[p];
            for (int i=0; i<ns.length; i++) {
                int q = ns[i];
                if (q == NO_NEIGHBOUR) break;
                int distq = distance[q];
                if (distq == UNREACHABLE_DISTANCE || distq == NO_DISTANCE) {
                    continue;
                }
                // expected distance to q
                int exdistq = (pieces[q] == pieces[p]) ? distp : (distp + 1);
                if (distq == UNKNOWN_DISTANCE || exdistq < distq) {
                    // found a new or shorter route to q
                    distance[q] = exdistq;
                    if (!listed[q]) {
                        border[idx++] = q;
                        listed[q] = true;
                    }
                }
            }
        }
    }


Generated by  Doxygen 1.6.0   Back to index