BilateralDiffusionStruct.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. namespace GFGGame
  2. {
  3. //两边扩散结构
  4. //初始化时从最大值,最小值之间随机一个值
  5. //每次取值从两边随机一边取值
  6. public struct BilateralDiffusionStruct
  7. {
  8. private readonly int minValue;
  9. private readonly int maxValue;
  10. public int leftPosition;
  11. public int rightPosition;
  12. public BilateralDiffusionStruct(int minValue, int maxValue)
  13. {
  14. this.minValue = minValue;
  15. this.maxValue = maxValue;
  16. var currenValue = RandomUtil.RandomInt(minValue, maxValue);
  17. if (currenValue == maxValue)
  18. {
  19. leftPosition = currenValue;
  20. rightPosition = currenValue;
  21. }
  22. else
  23. {
  24. leftPosition = currenValue;
  25. rightPosition = currenValue + 1;
  26. }
  27. }
  28. public BilateralDiffusionStruct(int minValue, int maxValue, int leftPosition, int rightPosition)
  29. {
  30. this.minValue = minValue;
  31. this.maxValue = maxValue;
  32. this.leftPosition = leftPosition;
  33. this.rightPosition = rightPosition;
  34. }
  35. public int NextValue()
  36. {
  37. int nextValue;
  38. if (leftPosition > minValue && rightPosition < maxValue)
  39. {
  40. nextValue = RandomUtil.RandomNext(2) == 0 ? leftPosition-- : rightPosition++;
  41. }
  42. else if (leftPosition <= minValue && rightPosition >= maxValue)
  43. {
  44. return -1;
  45. }
  46. else if (leftPosition < minValue)
  47. {
  48. nextValue = rightPosition++;
  49. }
  50. else
  51. {
  52. nextValue = leftPosition--;
  53. }
  54. return nextValue;
  55. }
  56. public bool IsFull()
  57. {
  58. return leftPosition < minValue && rightPosition > maxValue;
  59. }
  60. }
  61. }