指的是计算递归被调用的次数,或者如何确定是否完成了所有递归并将代码返回到第一次调用



我需要一种方法来检查这个函数的递归部分是否被激活了多少次,以检查第一次调用何时完成,这是设计的真正游戏的一部分,需要在没有实现所有目标的情况下完成第一次调用。

IEnumerator CompositeRun(string name)
{
BotOperation operation;
compositeOps.TryGetValue (name, out operation);
CompositeOperation compOp = operation as CompositeOperation;
List<BotOperation> botOpList = compOp.opList;
if (botOpList != null)
{
for (int i = 0; i < botOpList.Count; i++)
{
BotOperation op = botOpList [i];
CompositeOperation comp = op as CompositeOperation;
if (comp != null)
{
// If this operation is a composite type, then we need to execute it first accordingly.
yield return StartCoroutine (CompositeRun(comp.name));
}
if (op != null && op.ValidateOperation(gameObject, levelDef))
{
op.RunOperation (gameObject, levelDef);
if (CheckGameOver ())
{
// If all objectives are fulfilled, then there's no need to continue running the processes, the stage is concluded.
StopAllCoroutines ();
}
}

yield return new WaitForSeconds (operationDelay);
}
}
yield return null;
}

我建议从传递递归深度值开始

但是,您是否100%确定不使用递归就无法解决此问题?递归似乎是一门重炮,在这里杀死一只苍蝇

IEnumerator CompositeRun(string label, uint recursionDepth = 0)
{
compositeOps.TryGetValue (label, out var operation);
var compOp = operation as CompositeOperation;
var botOpList = compOp.opList;
if (botOpList != null)
{
var delay = new WaitForSeconds (operationDelay);
for (int i = 0; i < botOpList.Count; i++)
{
var op = botOpList[i];
var comp = op as CompositeOperation;
if (comp != null)
{
// If this operation is a composite type, then we need to execute it first accordingly.
yield return StartCoroutine (CompositeRun(comp.name,recursionDepth+1));
Debug.Log($"we need to go deeper @ depth:{recursionDepth+1}");
}
if ( op?.ValidateOperation(gameObject, levelDef))
{
op.RunOperation (gameObject, levelDef);
if (CheckGameOver ())
{
// If all objectives are fulfilled, then there's no need to continue running the processes, the stage is concluded.
StopAllCoroutines ();
}
}

yield return delay;
}
}
yield return null;
}

最新更新