Number reversal game: Difference between revisions

Add CLU
(Added solution for Action!)
(Add CLU)
Line 1,234:
(let [flipcount (read)]
(recur (flip-at flipcount unsorted), (inc steps))))))</lang>
 
=={{header|CLU}}==
<lang clu>% This program uses the random number generator from PCLU's
% 'misc.lib'
 
% Shuffle the given array. Every item ends up in a different place,
% so if given the numbers in ascending order, it is guaranteed that
% they will not still be in ascending order at the end.
jumble = proc [T: type] (a: array[T])
for i: int in int$from_to_by(array[T]$high(a), array[T]$low(a)+1, -1) do
j: int := array[T]$low(a) + random$next(i - array[T]$low(a))
x: T := a[i]
a[i] := a[j]
a[j] := x
end
end jumble
 
% Is the array sorted?
win = proc (a: array[int]) returns (bool)
for i: int in array[int]$indexes(a) do
if i ~= a[i] then return(false) end
end
return(true)
end win
 
% Reverse the first N items of the array
reverse = proc (n: int, a: array[int])
for i: int in int$from_to(1, n/2) do
j: int := n-i+1
x: int := a[j]
a[j] := a[i]
a[i] := x
end
end reverse
 
% Play the game
start_up = proc ()
po: stream := stream$primary_output()
pi: stream := stream$primary_input()
d: date := now()
random$seed(d.second + 60*(d.minute + 60*d.hour))
score: int := 0
nums: array[int] := array[int]$[1,2,3,4,5,6,7,8,9]
jumble[int](nums)
while ~win(nums) do
for i: int in array[int]$elements(nums) do
stream$puts(po, int$unparse(i))
end
stream$puts(po, ": reverse how many? ")
n: int := int$parse(stream$getl(pi))
reverse(n, nums)
score := score + 1
end
for i: int in array[int]$elements(nums) do
stream$puts(po, int$unparse(i))
end
stream$putl(po, "\nScore = " || int$unparse(score))
end start_up</lang>
{{out}}
<pre>792184365: reverse how many? 2
972184365: reverse how many? 9
563481279: reverse how many? 5
843651279: reverse how many? 8
721563489: reverse how many? 7
436512789: reverse how many? 3
634512789: reverse how many? 6
215436789: reverse how many? 3
512436789: reverse how many? 5
342156789: reverse how many? 2
432156789: reverse how many? 4
123456789
Score = 11</pre>
 
=={{header|Common Lisp}}==
2,095

edits