namespace GFGGame { //两边扩散结构 //初始化时从最大值,最小值之间随机一个值 //每次取值从两边随机一边取值 public struct BilateralDiffusionStruct { private readonly int minValue; private readonly int maxValue; public int leftPosition; public int rightPosition; public BilateralDiffusionStruct(int minValue, int maxValue) { this.minValue = minValue; this.maxValue = maxValue; var currenValue = RandomUtil.RandomInt(minValue, maxValue); if (currenValue == maxValue) { leftPosition = currenValue; rightPosition = currenValue; } else { leftPosition = currenValue; rightPosition = currenValue + 1; } } public BilateralDiffusionStruct(int minValue, int maxValue, int leftPosition, int rightPosition) { this.minValue = minValue; this.maxValue = maxValue; this.leftPosition = leftPosition; this.rightPosition = rightPosition; } public int NextValue() { int nextValue; if (leftPosition > minValue && rightPosition < maxValue) { nextValue = RandomUtil.RandomNext(2) == 0 ? leftPosition-- : rightPosition++; } else if (leftPosition <= minValue && rightPosition >= maxValue) { return -1; } else if (leftPosition < minValue) { nextValue = rightPosition++; } else { nextValue = leftPosition--; } return nextValue; } public bool IsFull() { return leftPosition < minValue && rightPosition > maxValue; } } }