private void visitPath(ExercisePath path) { //Adding the step to the solution ExerciseStep step = new ExerciseStep(getPinContainingRing(path.from), path.destination, path.from, solution.Count + 1); solution.Add(step); path.from.step = step.index; //Changing the exercise status step.from.ringPop(); //Remove the node from the current pin //Are we moving the biggest ring? (it is only moved once) if (step.ring.size == biggestRing.size) { solutionPinIndex = step.to.index; step.ring.isSolved = true; } //is this the final position for the ring? else if (step.to.index == solutionPinIndex) { RingNode rn = step.to.ringPeek(); if (rn.isSolved && rn.size == step.ring.size + 1) { step.ring.isSolved = true; } } step.to.ringPush(step.ring); //Adding the ring to the destination //Erase pending paths from the previous step if (solution.Count > 1) { solution[solution.Count - 2].ring.emptyPendingPaths(); } }
private void fillPendingPaths(RingNode ring) { PinNode pin; for (int i = 0; i < pins.Count; i++) { pin = pins[i]; //This ring could fit in the pin if (pin.isEmpty() || ring.size < pin.ringPeek().size) { //NEW PATH ExercisePath path = new ExercisePath(0, pin, ring); //Calculate the path weight (so we can decide wich path is better) //Less weight makes a better path //if is occupied then it weights more than an empty one if (!pin.isEmpty()) { path.weight += 1; //If the firs ring in the pin isn't solved it could have advantage based on size if (!pin.ringPeek().isSolved) { //Better than an empty one. path.weight -= (pin.getFirstUnsolvedRing().size - ring.size) * 2; } } //If the path is the solution for this ring it has more weight if (pin.isEmpty()) { //Is the biggest one if (ring.size == biggestRing.size) { path.weight -= rings.Count * 2; } } else { RingNode rn = pin.ringPeek(); if (rn.isSolved && ring.size + 1 == pin.ringPeek().size) { path.weight -= rings.Count * 2; } } //add the path to the ring ring.addPath(path); } } }
/** * REmove and retrieve first path **/ public ExercisePath pathShift() { if (pendingPaths.Count > 0) { ExercisePath result = pendingPaths[0]; pendingPaths.RemoveAt(0); return(result); } return(null); }
public void addPath(ExercisePath path) { //Tha paths are sorted by weight (from lowest to highest) if (pendingPaths.Count == 0) { pendingPaths.Add(path); } else { for (int i = 0; i < pendingPaths.Count; i++) { if (path.weight <= pendingPaths[i].weight) { pendingPaths.Insert(i, path); break; } } } }