Ejemplo n.º 1
0
 /**
  * Returns all networks in a DList regardless of length starting from Chip start.
  *
  * @param start is the starting Chip of the network.
  * @param depth records depth of the method in a recursive call.
  */
 DList allNetwork(Chip start, int depth) throws InvalidNodeException {
   DList set = new DList();
   if (inGoal(start)) {
     DList network = new DList();
     network.insertFront(start);
     set.insertFront(network);
     return set;
   } else if (depth > 10) {
     return set;
   } else if (connections(start).length() == 0) {
     return set;
   } else {
     DListNode current = (DListNode) connections(start).front();
     int length = connections(start).length();
     for (int i = 0; i < length; i++) {
       if (!inStartGoal((Chip) current.item())) {
         DList rest = allNetwork((Chip) current.item(), depth + 1);
         if (rest.length() > 0) {
           DListNode currentNetworkNode = (DListNode) rest.front();
           for (int j = 0; j < rest.length(); j++) {
             DList currentNetwork = (DList) currentNetworkNode.item();
             currentNetwork.insertFront(start);
             if (isTurnCorner(currentNetwork) && noSameChip(currentNetwork)) {
               set.insertFront(currentNetwork);
             }
             currentNetworkNode = (DListNode) currentNetworkNode.next();
           }
         }
       }
       current = (DListNode) current.next();
     }
     return set;
   }
 }