Number reversal game: Difference between revisions

Added MATLAB example.
(→‎{{header|F Sharp|F#}}: Remove incorrect tag.)
(Added MATLAB example.)
Line 1,508:
</body>
</html></lang>
 
=={{header|MATLAB}}==
<lang MATLAB>function NumberReversalGame
list = randperm(9);
while issorted(list)
list = randperm(9);
end
fprintf('Given a list of numbers, try to put them into ascending order\n')
fprintf('by sequentially reversing everything left of a point you choose\n')
fprintf('in the array. Try to do it in as few reversals as possible.\n')
fprintf('No input will quit the game.\n')
fprintf('Position Num:%s\n', sprintf(' %d', 1:length(list)))
fprintf('Current List:%s', sprintf(' %d', list))
pivot = 1;
nTries = 0;
while ~isempty(pivot) && ~issorted(list)
pivot = input(' Enter position of reversal limit: ');
if pivot
list(1:pivot) = list(pivot:-1:1);
fprintf('Current List:%s', sprintf(' %d', list))
nTries = nTries+1;
end
end
if issorted(list)
fprintf('\nCongratulations! You win! Only %d reversals.\n', nTries)
else
fprintf('\nPlay again soon!\n')
end
end</lang>
{{out}}
<pre>Given a list of numbers, try to put them into ascending order
by sequentially reversing everything left of a point you choose
in the array. Try to do it in as few reversals as possible.
No input will quit the game.
Position Num: 1 2 3 4 5 6 7 8 9
Current List: 1 6 3 2 8 7 9 4 5 Enter position of reversal limit: 7
Current List: 9 7 8 2 3 6 1 4 5 Enter position of reversal limit: 9
Current List: 5 4 1 6 3 2 8 7 9 Enter position of reversal limit: 7
Current List: 8 2 3 6 1 4 5 7 9 Enter position of reversal limit: 8
Current List: 7 5 4 1 6 3 2 8 9 Enter position of reversal limit: 7
Current List: 2 3 6 1 4 5 7 8 9 Enter position of reversal limit: 3
Current List: 6 3 2 1 4 5 7 8 9 Enter position of reversal limit: 6
Current List: 5 4 1 2 3 6 7 8 9 Enter position of reversal limit: 5
Current List: 3 2 1 4 5 6 7 8 9 Enter position of reversal limit: 3
Current List: 1 2 3 4 5 6 7 8 9
Congratulations! You win! Only 9 reversals.</pre>
 
=={{header|Mathematica}}==
Anonymous user