Guest User

Untitled

a guest
Dec 10th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. /*
  2. * REVISITING THE COMBINATION LOCK
  3. * - We can make our machine non-deterministic
  4. */
  5.  
  6. using System;
  7. using System.Collections.Generic;
  8.  
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. UnlockUsingCorrectCombination();
  14. }
  15.  
  16. public static void UnlockUsingCorrectCombination()
  17. {
  18. var combinationLock = new CombinationLock();
  19. combinationLock.Start();
  20. Console.WriteLine(string.Format("Starting State: {0}", combinationLock.CurrentState));
  21.  
  22. combinationLock.EnterNumber(7);
  23. Console.WriteLine(combinationLock.CurrentState);
  24.  
  25. combinationLock.EnterNumber(1);
  26. Console.WriteLine(combinationLock.CurrentState);
  27.  
  28. combinationLock.EnterNumber(3);
  29. Console.WriteLine(combinationLock.CurrentState);
  30.  
  31. Console.WriteLine(string.Format("Ending State: {0}", combinationLock.CurrentState));
  32. }
  33. }
  34.  
  35. public enum CombinationLockState
  36. {
  37. NoneRight,
  38. OneRight,
  39. TwoRight,
  40. Open
  41. }
  42.  
  43. public class CombinationLock : IteratorStateMachine<CombinationLockState>
  44. {
  45. private int? firstNumber;
  46. private int? secondNumber;
  47. private int? thirdNumber;
  48.  
  49. public void EnterNumber(int number)
  50. {
  51. this.SetNumber(number);
  52.  
  53. Console.WriteLine(string.Format("Entered {0}...", number));
  54. this.Trigger();
  55. }
  56.  
  57. protected override IEnumerable<CombinationLockState> States()
  58. {
  59. /*
  60. * Of course, nobody in their right mind would build a system that confirms
  61. * each value after input as our example is doing. Doing so reduces the
  62. * number of attempts required to crack the code from a maximum of 1000 to
  63. * just 30!
  64. */
  65.  
  66. while (true)
  67. {
  68. if (this.firstNumber == 7 && this.secondNumber == 1 && this.thirdNumber == 3)
  69. {
  70. yield return CombinationLockState.Open;
  71. }
  72. else if (this.firstNumber == 7 && this.secondNumber == 1 && this.thirdNumber != 3)
  73. {
  74. yield return CombinationLockState.TwoRight;
  75. }
  76. else if (this.firstNumber == 7 && this.secondNumber != 1)
  77. {
  78. yield return CombinationLockState.OneRight;
  79. }
  80. else
  81. {
  82. yield return CombinationLockState.NoneRight;
  83. }
  84. }
  85. }
  86.  
  87. private void SetNumber(int number)
  88. {
  89. if (this.firstNumber == null)
  90. {
  91. this.firstNumber = number;
  92. }
  93. else if (this.secondNumber == null)
  94. {
  95. this.secondNumber = number;
  96. }
  97. else if (this.thirdNumber == null)
  98. {
  99. this.thirdNumber = number;
  100. }
  101. else
  102. {
  103. this.firstNumber = number;
  104. this.secondNumber = null;
  105. this.thirdNumber = null;
  106. }
  107. }
  108. }
  109.  
  110. public abstract class IteratorStateMachine<TState>
  111. {
  112. private IEnumerator<TState> enumerator;
  113.  
  114. public TState CurrentState
  115. {
  116. get
  117. {
  118. return this.enumerator.Current;
  119. }
  120. }
  121.  
  122. public void Start()
  123. {
  124. this.enumerator = this.States().GetEnumerator();
  125. this.enumerator.MoveNext();
  126. }
  127.  
  128. protected abstract IEnumerable<TState> States();
  129.  
  130. protected void Trigger()
  131. {
  132. this.enumerator.MoveNext();
  133. }
  134. }
Add Comment
Please, Sign In to add comment