/tutorial/hanoi/tower.e

http://github.com/tybor/Liberty · Specman e · 139 lines · 125 code · 10 blank · 4 comment · 6 complexity · 43ba742d3e1c0b4f6aa60a718355a97b MD5 · raw file

  1. class TOWER
  2. creation {ANY}
  3. full, empty
  4. feature {}
  5. t: ARRAY[INTEGER]
  6. top: INTEGER
  7. feature {}
  8. full (n: INTEGER) is
  9. require
  10. n >= 1
  11. local
  12. i: INTEGER
  13. do
  14. create t.make(1, n)
  15. from
  16. i := n
  17. until
  18. i = 0
  19. loop
  20. t.put(n - i + 1, i)
  21. i := i - 1
  22. end
  23. top := n
  24. ensure
  25. nb = n
  26. top = nb
  27. t.item(top) = 1
  28. end
  29. empty (n: INTEGER) is
  30. require
  31. n >= 1
  32. do
  33. create t.make(1, n)
  34. top := 1
  35. ensure
  36. nb = n
  37. top = 1
  38. end
  39. feature {HANOI}
  40. nb: INTEGER is
  41. do
  42. Result := t.upper
  43. end
  44. show_a_discus (d: INTEGER; picture: STRING) is
  45. require
  46. 1 <= d
  47. d <= nb
  48. picture /= Void
  49. local
  50. nb_of_free_slots, nb_of_used_slots, i: INTEGER
  51. do
  52. nb_of_used_slots := t.item(d)
  53. nb_of_free_slots := nb - nb_of_used_slots
  54. from
  55. i := nb_of_free_slots
  56. until
  57. i = 0
  58. loop
  59. picture.extend(' ')
  60. i := i - 1
  61. end
  62. from
  63. i := nb_of_used_slots
  64. until
  65. i = 0
  66. loop
  67. picture.extend('=')
  68. i := i - 1
  69. end
  70. picture.extend('|')
  71. from
  72. i := nb_of_used_slots
  73. until
  74. i = 0
  75. loop
  76. picture.extend('=')
  77. i := i - 1
  78. end
  79. from
  80. i := nb_of_free_slots
  81. until
  82. i = 0
  83. loop
  84. picture.extend(' ')
  85. i := i - 1
  86. end
  87. end
  88. remove_discus: INTEGER is
  89. do
  90. debug
  91. if t.item(top) = 0 then
  92. std_error.put_string("Error in 'remove_discus'.%N")
  93. crash
  94. end
  95. end
  96. Result := t.item(top)
  97. t.put(0, top)
  98. if top > 1 then
  99. top := top - 1
  100. end
  101. ensure
  102. top >= 1
  103. end
  104. add_discus (d: INTEGER) is
  105. do
  106. debug
  107. if top = nb then
  108. std_error.put_string("Error in 'add_discus', %
  109. %the tower was already full.%N")
  110. crash
  111. end
  112. if d > t.item(top) then
  113. -- std_error.put_string("Error in 'add_discus', the %
  114. -- %discus you wanted to put is larger %
  115. -- %than allowed.");
  116. -- crash;
  117. end
  118. end
  119. if t.item(top) > d then
  120. top := top + 1
  121. t.put(d, top)
  122. end
  123. if t.item(top) = 0 then
  124. t.put(d, top)
  125. end
  126. ensure
  127. top <= nb
  128. end
  129. end -- class TOWER