nitlanguage
/
nit.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
manual: add nitish and avoid name conflicts
[nit.git]
/
tests
/
example_hanoi.nit
diff --git
a/tests/example_hanoi.nit
b/tests/example_hanoi.nit
index
d99994e
..
3bb4205
100644
(file)
--- a/
tests/example_hanoi.nit
+++ b/
tests/example_hanoi.nit
@@
-18,32
+18,32
@@
class Tower
# A tower is a stack of discus.
class Tower
# A tower is a stack of discus.
- meth top: Int
+ fun top: Int
# Diameter of the last discus.
do
return _t.last
end
# Diameter of the last discus.
do
return _t.last
end
- meth height: Int
+ fun height: Int
# Number of discus.
do
return _t.length
end
# Number of discus.
do
return _t.length
end
- meth push(i: Int)
+ fun push(i: Int)
# Put an discus of diameter `i'.
do
_t.push(i)
end
# Put an discus of diameter `i'.
do
_t.push(i)
end
- meth pop: Int
+ fun pop: Int
# Remove the last discus and get its diameter.
do
assert not_empty: not _t.is_empty
return _t.pop
end
# Remove the last discus and get its diameter.
do
assert not_empty: not _t.is_empty
return _t.pop
end
- redef meth to_s: String
+ redef fun to_s: String
# Display the tower
do
if _t.is_empty then
# Display the tower
do
if _t.is_empty then
@@
-53,13
+53,13
@@
class Tower
end
end
end
end
- attr _t: Array[Int] # The stack of discus (only the diameter is stored).
+ var t = new Array[Int] # The stack of discus (only the diameter is stored).
init full(n: Int)
# Build a new tower with `n' discus.
do
assert positive: n >= 0
init full(n: Int)
# Build a new tower with `n' discus.
do
assert positive: n >= 0
- _t = new Array[Int].with_capacity(n)
+ t.enlarge(n)
for i in [0..n[ do
push(n-i)
end
for i in [0..n[ do
push(n-i)
end
@@
-68,18
+68,17
@@
class Tower
init empty
# Build a empty tower.
do
init empty
# Build a empty tower.
do
- _t = new Array[Int]
end
end
class Hanoi
# Hanoi is a city with 3 towers.
end
end
class Hanoi
# Hanoi is a city with 3 towers.
- meth run
+ fun run
do
move(_tower1.height, _tower1, _tower2, _tower3)
end
do
move(_tower1.height, _tower1, _tower2, _tower3)
end
- private meth move(nb: Int, source: Tower, intermediate: Tower, destination: Tower)
+ private fun move(nb: Int, source: Tower, intermediate: Tower, destination: Tower)
do
if nb <= 0 then
return
do
if nb <= 0 then
return
@@
-90,14
+89,14
@@
class Hanoi
move(nb-1, intermediate, source, destination)
end
move(nb-1, intermediate, source, destination)
end
- redef meth to_s: String
+ redef fun to_s: String
do
return "{_tower1} {_tower2} {_tower3}"
end
do
return "{_tower1} {_tower2} {_tower3}"
end
- attr _tower1: Tower
- attr _tower2: Tower
- attr _tower3: Tower
+ var tower1: Tower
+ var tower2: Tower
+ var tower3: Tower
init(nb: Int)
do
init(nb: Int)
do
@@
-107,7
+106,7
@@
class Hanoi
end
end
end
end
-meth usage
+fun usage
do
print("Usage: hanoi <number of discus>")
exit(0)
do
print("Usage: hanoi <number of discus>")
exit(0)