Jump to content

Number reversal game: Difference between revisions

Added solution for Action!
(Added solution for Action!)
Line 219:
 
You took 10 attempts to put the digits in order!
</pre>
 
=={{header|Action!}}==
<lang Action!>PROC KnuthShuffle(BYTE ARRAY tab BYTE size)
BYTE i,j,tmp
 
i=size-1
WHILE i>0
DO
j=Rand(i+1)
tmp=tab(i)
tab(i)=tab(j)
tab(j)=tmp
i==-1
OD
RETURN
 
BYTE FUNC IsSorted(BYTE ARRAY tab BYTE size)
BYTE i
 
FOR i=0 TO size-2
DO
IF tab(i)>tab(i+1) THEN
RETURN (0)
FI
OD
RETURN (1)
 
PROC Swap(BYTE ARRAY tab BYTE size,count)
BYTE i,j,tmp
 
i=0 j=count-1
WHILE i<j
DO
tmp=tab(i)
tab(i)=tab(j)
tab(j)=tmp
i==+1 j==-1
OD
RETURN
 
PROC Main()
DEFINE SIZE="9"
BYTE ARRAY a(SIZE)
BYTE i,j,n,tmp
BYTE LMARGIN=$52,oldLMARGIN
 
oldLMARGIN=LMARGIN
LMARGIN=0 ;remove left margin on the screen
Put(125) PutE() ;clear the screen
 
FOR i=0 TO SIZE-1
DO
a(i)=i+1
OD
KnuthShuffle(a,SIZE)
 
i=0
DO
PrintF("%B: ",i)
FOR j=0 TO SIZE-1
DO
PrintB(a(j))
OD
IF IsSorted(a,SIZE) THEN
EXIT
FI
PrintF(" How many to flip (2-%B)? ",SIZE)
n=InputB()
IF n>=2 AND n<=SIZE THEN
Swap(a,SIZE,n)
i==+1
FI
OD
 
PrintF("%E%EYou solved it in %B moves!",i)
 
LMARGIN=oldLMARGIN ;restore left margin on the screen
RETURN</lang>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Number_reversal_game.png Screenshot from Atari 8-bit computer]
<pre>
0: 987152346 How many to flip (2-9)? 9
1: 643251789 How many to flip (2-9)? 6
2: 152346789 How many to flip (2-9)? 2
3: 512346789 How many to flip (2-9)? 5
4: 432156789 How many to flip (2-9)? 4
5: 123456789
 
You solved it in 5 moves!
</pre>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.