import java.util.*; public class BackTrack { protected Application app; /** * Initializes this BackTrack object from an application. * * @param app the application */ public BackTrack (Application app) { this.app = app; } // constructor /** * Attempts to reach a goal through a given position. * * @param pos the given position. * * @return true if the attempt succeeds; otherwise, false. */ public boolean tryToReachGoal (Position pos) { boolean success = false; Iterator itr = app.iterator (pos); while (!success && itr.hasNext()) { pos = itr.next(); if (app.isOK (pos)) { app.markAsPossible (pos); if (app.isGoal (pos)) success = true; else { success = tryToReachGoal (pos); if (!success) app.markAsDeadEnd (pos); } // goal not reached yet } // pos may be on a path to a goal } // while return success; } // method tryToReachGoal } // class BackTrack