//checks if the puzzle is complete and updates each layer in the process bool IsComplete(ComboLayer currentLayer) { List <PowerLink> poweredNodes = new List <PowerLink>(); //find all of the powered downNodes in this layer foreach (PowerLink node in currentLayer.GetRecieverNodes()) { if (node.IsPowered()) { poweredNodes.Add(node); } } ComboLayer nextLayer = currentLayer.GetNextLayer(); //this part implies that the final layer needs only one node to be powered, or that the layer has only one node //this section could be modified so that a set of nodes needs to be powered instead. if (poweredNodes.Count > 0 && nextLayer == null) { return(true); } if (nextLayer == null) { return(false); } //turn off all the downNodes in the next layer foreach (PowerLink node in nextLayer.GetRecieverNodes()) { node.SetPower(false); } //if there are no powered nodes here, then this cannot be the solution if (poweredNodes.Count == 0) { IsComplete(nextLayer); //this has to be done in order to turn off all the nodes on the layers above return(false); } //for each powered upNode in this layer, find and turn on each corresponding downNode in the next layer foreach (PowerLink node in poweredNodes) { foreach (GameObject upNode in node.GetAttachedOutNodes()) { foreach (PowerLink downNode in nextLayer.GetRecieverNodes()) { if (NodesInLine(upNode, downNode)) { downNode.SetPower(true); } } } } //do recursion onto the next layer return(IsComplete(nextLayer)); }
void TurnOffLayer(ComboLayer currentLayer) { if (currentLayer == null) { return; } //find all of the powered downNodes in this layer foreach (PowerLink node in currentLayer.GetRecieverNodes()) { node.SetPower(false); } TurnOffLayer(currentLayer.GetNextLayer()); }
public void PuzzleOff() { TurnOffLayer(baseLayer.GetNextLayer()); }