/** * This is the actual, recursive function that checks one branch of the graph/tree for * connections. Calls it self if certain conditions are met, mostly working with two booleans * "isTaken" and "isVisited" * * @param townA will always be this.townA * @param townB will always be this.townB * @param flf The incremented "i" of the for loop called in "areConnected()" * @return returns false if it did not find a connecting path, returns true if it does. */ public boolean _areConnected(Town townA, Town townB, int flf, Players player) { Town current = townA; for (int i = 0; i < current.getConnections().size(); i++) { // check all the connections if (current.getConnection(i).getIsTaken() && current.getConnection(i).getTakenByPlayer(player)) { // if they are taken if (current.getConnection(i).getTownB().getName() == townB.getName() || // if names match the target name current.getConnection(i).getTownA().getName() == townB.getName()) { // if names match the target name return true; } } } boolean returnValue = false; // variable for dynamic return value for (int i = 0 + flf; i < current.getConnections().size(); i++) { // loop through connections to change the current town if (current.getConnection(i).getIsTaken() && current.getConnection(i).getTakenByPlayer(player) && current.getConnection(i).getIsVisited() == false) { // if its taken and not visited current.getConnection(i).setIsVisited(true); // set it as visited visitedConnections.add(current.getConnection(i)); // also store it in our Arraylist if (current.getConnection(i).getTownB().getName() == current .getName()) { // if the town on the other side of the connection is the same as // current current = townA.getConnection(i).getTownA(); // then set current as the other town break; // dont continue looping } else { current = townA.getConnection(i).getTownB(); // else set current as that town break; // dont continue looping } } } for (int i = 0; i < current.getConnections().size(); i++) { // current has been changed if (current.getConnection(i).getIsTaken() && current.getConnection(i).getTakenByPlayer(player)) { // check "taken" towns if (!current.getConnection(i).getIsVisited()) { // skip visited towns returnValue = returnValue || _areConnected( current, townB, flf, player); // call the function on it self, it might return true } } } return returnValue; // returns false often, but should return true at least once if there is a // connection. }