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