Number reversal game: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
imported>Arakov
(One intermediate revision by one other user not shown)
Line 389:
end NumberReverse;
</syntaxhighlight>
 
=={{header|ALGOL 68}}==
Using code from the [[Knuth shuffle#ALGOL 68|Knuth shuffle]] task.
<syntaxhighlight lang="algol68">
BEGIN # play the number reversal game #
 
CO begin code from the Knuth shuffle task CO
PROC between = (INT a, b)INT :
(
ENTIER (random * ABS (b-a+1) + (a<b|a|b))
);
 
PROC knuth shuffle = (REF[]INT a)VOID:
(
FOR i FROM LWB a TO UPB a DO
INT j = between(LWB a, UPB a);
INT t = a[i];
a[i] := a[j];
a[j] := t
OD
);
CO end code from the Knuth shuffle task CO
 
[]INT ordered digits = ( 1, 2, 3, 4, 5, 6, 7, 8, 9 );
[ 1 : 9 ]INT digits := ordered digits;
knuth shuffle( digits );
 
# ignore invalid data in stand in #
on value error( stand in, ( REF FILE f )BOOL: TRUE );
 
PROC print digits = VOID: # prints the digits #
BEGIN
print( ( "The digits are:" ) );
FOR i TO 9 DO print( ( whole( digits[ i ], -2 ) ) ) OD;
print( ( newline ) )
END # print digits # ;
 
OP = = ( []INT a, b )BOOL: # returns TRUE if a = b #
IF LWB a /= LWB b OR UPB a /= UPB b THEN
FALSE # a and b have different bounds #
ELSE
# a and b ave the same bounds #
BOOL same := TRUE;
FOR i FROM LWB a TO UPB a WHILE same DO same := a[ i ] = b[ i ] OD;
same
FI # = # ;
OP /= = ( []INT a, b )BOOL: NOT ( a = b ); # returns TRUE if a not = b #
 
INT count := 0;
 
print digits;
WHILE digits /= ordered digits DO
print( ( "How many digits to reverse (2-9)? " ) );
INT n := 0;
read( ( n, newline ) );
IF n >= 2 AND n <= 9 THEN
# reverse the n left-most digits #
count +:= 1;
INT r pos := n;
FOR pos TO n OVER 2 DO
INT t = digits[ r pos ];
digits[ r pos ] := digits[ pos ];
digits[ pos ] := t;
r pos -:= 1
OD;
print digits
FI
OD;
print( ( newline, "You ordered the digits in ", whole( count, 0 ), " moves", newline ) )
 
END
</syntaxhighlight>
{{out}}
<pre>
The digits are: 9 6 4 3 8 7 1 5 2
How many digits to reverse (2-9)? 4
The digits are: 3 4 6 9 8 7 1 5 2
How many digits to reverse (2-9)? 6
The digits are: 7 8 9 6 4 3 1 5 2
How many digits to reverse (2-9)? 3
The digits are: 9 8 7 6 4 3 1 5 2
How many digits to reverse (2-9)? 9
The digits are: 2 5 1 3 4 6 7 8 9
How many digits to reverse (2-9)? 2
The digits are: 5 2 1 3 4 6 7 8 9
How many digits to reverse (2-9)? 5
The digits are: 4 3 1 2 5 6 7 8 9
How many digits to reverse (2-9)? 4
The digits are: 2 1 3 4 5 6 7 8 9
How many digits to reverse (2-9)? 2
The digits are: 1 2 3 4 5 6 7 8 9
 
You ordered the digits in 8 moves
</pre>
 
=={{header|APL}}==
Line 416 ⟶ 510:
1 2 3 4 5 6 7 8 9: Congratulations!
Swaps: 10</pre>
 
=={{header|Applesoft BASIC}}==
<syntaxhighlight lang="applesoftbasic"> 100 LET M$ = CHR$ (13)
Line 1,965 ⟶ 2,060:
 
=={{header|Elena}}==
ELENA 46.x:
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 1,971 ⟶ 2,066:
public program()
{
var sorted := Array.allocate(9).populate::(n => n + 1 );
var values := sorted.clone().randomize:(9);
while (sorted.sequenceEqual:(values))
{
values := sorted.randomize:(9)
};
var tries := new Integer();
until (sorted.sequenceEqual:(values))
{
tries.append:(1);
console.print("# ",tries," : LIST : ",values," - Flip how many?");
Anonymous user