Number reversal game: Difference between revisions

Content added Content deleted
No edit summary
Line 271: Line 271:


Sample output:
Sample output:
<pre>
Given a jumbled list of the numbers 1 to 9,
Given a jumbled list of the numbers 1 to 9,
you must select how many digits from the left to reverse.
you must select how many digits from the left to reverse.
Line 283: Line 284:
7 : 1 2 3 4 5 6 7 8 9
7 : 1 2 3 4 5 6 7 8 9
You took 7 tries to put the digits in order.
You took 7 tries to put the digits in order.
</pre>

=={{header|Batch File}}==
Note that I did not use the FOR command for looping. I used Batch File labels instead.
<lang dos>
::
::Number Reversal Game Task from Rosetta Code Wiki
::Batch File Implementation
::
::Please do not open this from command prompt.
::Directly Open the Batch File to play...
::

@echo off
setlocal enabledelayedexpansion
title Number Reversal Game

:begin
set score=0

::The ascending list of 9 digits
set list=123456789


::Generating a random set of 9 digits...
set cyc=9
:gen
set /a tmp1=%random%%%%cyc%
set n%cyc%=!list:~%tmp1%,1!
set tmp2=!n%cyc%!
set list=!list:%tmp2%=!
if not %cyc%==2 (
set /a cyc-=1
goto :gen
)
set /a n1=%list%

::Display the Game
cls
echo.
echo ***Number Reversal Game***
:loopgame
echo.
echo Current arrangement: %n1%%n2%%n3%%n4%%n5%%n6%%n7%%n8%%n9%
set /p move=How many digits from the left should I reverse?

::Reverse digits according to the player's input
::NOTE: The next command uses the fact that in Batch File,
::The output for the division operation is only the integer part of the quotient.
set /a lim=(%move%+1)/2

set cyc2=1
:reverse
set /a tmp4=%move%-%cyc2%+1
set tmp5=!n%cyc2%!
set n%cyc2%=!n%tmp4%!
set n%tmp4%=%tmp5%
if not %cyc2%==%lim% (
set /a cyc2+=1
goto :reverse
)

::Increment the number of moves took by the player
set /a score+=1

::IF already won...
if %n1%%n2%%n3%%n4%%n5%%n6%%n7%%n8%%n9%==123456789 (
echo.
echo Set: %n1%%n2%%n3%%n4%%n5%%n6%%n7%%n8%%n9% DONE^^!
echo You took %score% moves to arrange the numbers in ascending order.
pause>nul
exit
) else (
goto :loopgame
)
</lang>
Sample Output:
<pre>
***Number Reversal Game***

Current arrangement: 349718652
How many digits from the left should I reverse?3

Current arrangement: 943718652
How many digits from the left should I reverse?9

Current arrangement: 256817349
How many digits from the left should I reverse?4

Current arrangement: 865217349
How many digits from the left should I reverse?8

Current arrangement: 437125689
How many digits from the left should I reverse?3

Current arrangement: 734125689
How many digits from the left should I reverse?7

Current arrangement: 652143789
How many digits from the left should I reverse?6

Current arrangement: 341256789
How many digits from the left should I reverse?2

Current arrangement: 431256789
How many digits from the left should I reverse?4

Current arrangement: 213456789
How many digits from the left should I reverse?2

Set: 123456789 DONE!
You took 10 moves to arrange the numbers in ascending order.
</pre>


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==