Number reversal game

From Rosetta Code
Revision as of 16:00, 14 May 2010 by Rdm (talk | contribs) (J: simplify)
Task
Number reversal game
You are encouraged to solve this task according to the task description, using any language you may know.

Given a jumbled list of the numbers 1 to 9 that are definitely not in ascending order, show the list then ask the player how many digits from the left to reverse. Reverse those digits, then ask again, until all the digits end up in ascending order.

The score is the count of the reversals needed to attain the ascending order.

Note: Assume the players input does not need extra validation.


C.f: Sorting algorithms/Pancake sort, Pancake sorting.

Ada

<lang ada> with Ada.Text_Io; use Ada.Text_Io; with Ada.Integer_Text_Io; use Ada.Integer_Text_Io; with Ada.Numerics.Discrete_Random;

procedure NumberReverse is

  subtype RandRange is Integer range 1..9;
  type NumArrayType is array (Integer range 1..9) of Integer;
  
  package RandNumbers is new Ada.Numerics.Discrete_Random(RandRange);
  use RandNumbers;
  
  procedure FillArray (A : in out NumArrayType) is
     G : Generator;
     Temp : RandRange;
  begin
     A := (others => 0);
     Reset(G);
     for I in 1..9 loop
        Temp := Random(G);
        while A(Temp) /= 0 loop
           Temp := Random(G);
        end loop;
        A(Temp) := I;
     end loop;
  end FillArray;
  
  procedure Put(A : in NumArrayType) is
  begin
     for I in 1..9 loop
        Put(A(I), 0);
        Put(" ");
     end loop;
  end Put;
  
  procedure Prompt (Index : out Integer) is
  begin
     New_Line;
     Put("How many numbers would you like to reverse: ");
     Get(Index);
  end Prompt;
  
  procedure ReverseArray(Arr : in out NumArrayType;
                         Index : in Integer) is
     Temp : RandRange;
  begin
     for I in 1..Index/2 loop
        Temp := Arr(I);
        Arr(I) := Arr(Index + 1 - I);
        Arr(Index + 1 - I) := Temp;
     end loop;
  end ReverseArray;
  
  Arr, Sorted : NumArrayType;
  Index, Count : Integer;
  

begin

  Sorted := (1,2,3,4,5,6,7,8,9);
  Count := 0;
  FillArray(Arr);
  while Sorted /= Arr loop
     Put(Arr);
     Prompt(Index);
     Count := Count + 1;
     ReverseArray(Arr, Index);
  end loop;
  Put(Arr);
  New_Line;
  Put("Congratulations! You win. It took " & 
        Integer'Image(Count) & " tries.");

end NumberReverse; </lang>

AutoHotkey

<lang Autohotkey>; Submitted by MasterFocus --- http://tiny.cc/iTunis

ScrambledList := CorrectList := "1 2 3 4 5 6 7 8 9" ; Declare two identical correct sequences While ( ScrambledList = CorrectList )

 Sort, ScrambledList, Random D%A_Space% ; Shuffle one of them inside a While-loop to ensure it's shuffled

Attempts := 0 While ( ScrambledList <> CorrectList ) ; Repeat until the sequence is correct {

 InputBox, EnteredNumber, Number Reversal Game, Attempts so far: %Attempts%`n`nCurrent sequence: %ScrambledList%`n`nHow many numbers (from the left) should be flipped?, , 400, 200
 If ErrorLevel
   ExitApp ; Exit if user presses ESC or Cancel
 If EnteredNumber is not integer
   Continue ; Discard attempt if entered number is not an integer
 If ( EnteredNumber <= 1 )
   Continue ; Discard attempt if entered number is <= 1
 Attempts++ ; Increase the number of attempts
 ; Reverse the first part of the string and add the second part
 ; The entered number is multiplied by 2 to deal with the spaces
 ScrambledList := Reverse(SubStr(ScrambledList,1,(EnteredNumber*2)-1)) SubStr(ScrambledList,EnteredNumber*2)

}

MsgBox, You took %Attempts% attempts to get the correct sequence. ; Final message

Return

-------------------

Reverse(Str) ; Auxiliar function (flips a string) {

 Loop, Parse, Str
   Out := A_LoopField Out
 Return Out

}</lang>

BASIC

Works with: QBasic
Works with: FreeBASIC

<lang qbasic>PRINT "Given a jumbled list of the numbers 1 to 9," PRINT "you must select how many digits from the left to reverse." PRINT "Your goal is to get the digits in order with 1 on the left and 9 on the right."

RANDOMIZE TIMER

DIM nums(1 TO 9) AS INTEGER DIM L0 AS INTEGER, n AS INTEGER, flp AS INTEGER, tries AS INTEGER, again AS INTEGER

'initial values FOR L0 = 1 TO 9

   nums(L0) = L0

NEXT

DO 'shuffle

   FOR L0 = 9 TO 2 STEP -1
       n = INT(RND * (L0)) + 1
       IF n <> L0 THEN SWAP nums(n), nums(L0)
   NEXT
   FOR L0 = 1 TO 8 'make sure it's not in order
       IF nums(L0) > nums(L0 + 1) THEN EXIT DO
   NEXT

LOOP

again = -1 DO

   IF tries < 10 THEN PRINT " ";
   PRINT tries; ":";
   FOR L0 = 1 TO 9
       PRINT nums(L0);
   NEXT
   IF NOT again THEN EXIT DO
   INPUT " -- How many numbers should be flipped"; flp
   IF flp < 0 THEN flp = 0
   IF flp > 9 THEN flp = 0
   FOR L0 = 1 TO (flp \ 2)
       SWAP nums(L0), nums(flp - L0 + 1)
   NEXT
   again = 0
   'check for order
   FOR L0 = 1 TO 8
       IF nums(L0) > nums(L0 + 1) THEN
           again = -1
           EXIT FOR
       END IF
   NEXT
   IF flp > 0 THEN tries = tries + 1

LOOP

PRINT : PRINT "You took "; LTRIM$(RTRIM$(STR$(tries))); " tries to put the digits in order."</lang>

Sample output:

Given a jumbled list of the numbers 1 to 9,
you must select how many digits from the left to reverse.
Your goal is to get the digits in order with 1 on the left and 9 on the right.
  0 : 1  4  5  3  2  7  6  8  9  -- How many numbers should be flipped? 7
  1 : 6  7  2  3  5  4  1  8  9  -- How many numbers should be flipped? 2
  2 : 7  6  2  3  5  4  1  8  9  -- How many numbers should be flipped? 7
  3 : 1  4  5  3  2  6  7  8  9  -- How many numbers should be flipped? 3
  4 : 5  4  1  3  2  6  7  8  9  -- How many numbers should be flipped? 5
  5 : 2  3  1  4  5  6  7  8  9  -- How many numbers should be flipped? 2
  6 : 3  2  1  4  5  6  7  8  9  -- How many numbers should be flipped? 3
  7 : 1  2  3  4  5  6  7  8  9
You took 7  tries to put the digits in order.

C#

C# 3.0 <lang csharp>using System; using System.Linq;

class Program {

   static void Main(string[] args)
   {
       var r = new Random();
       var sorted = Enumerable.Range(1, 9).ToList();
       var values = sorted.OrderBy(x => r.Next(-1, 1)).ToList();
       //var values = "1 3 9 2 7 5 4 8 6".Split().Select(x => int.Parse(x)).ToList();
       var tries = 1;
       while (!Enumerable.SequenceEqual(sorted, values))
       {
           Console.Write("# {0}: LIST: {1} - Flip how many? ", tries, String.Join(" ", values));
           values.Reverse(0, int.Parse(Console.ReadLine()));
           tries += 1;
       }
       Console.WriteLine("\nYou took {0} attempts to put the digits in order!", tries - 1);
       Console.ReadLine();
   }

}</lang>

C# 1.0 <lang csharp>class Program { static void Main(string[] args) { int[] values = new int[9]; Random tRandom = new Random(); int tries = 0;

for (int x = 0; x < values.Length; x++) { values[x] = x + 1; }

values = RandomPermutation<int>(values);

do { Console.Write("Numbers: "); for (int x = 0; x < values.Length; x++) { Console.Write(" "); Console.Write(values[x]); } Console.WriteLine(". Enter number of numbers from the left to reverse: ");

string tIn = ""; do { tIn = Console.ReadLine(); } while (tIn.Length != 1);

int nums = Convert.ToInt32(tIn.ToString());

int[] newValues = new int[9]; for (int x = nums; x < newValues.Length; x++) { // Move those not reversing newValues[x] = values[x]; } for (int x = 0; x < nums; x++) { // Reverse the rest newValues[x] = values[nums - 1 - x]; } values = newValues; tries++; } while (!check(values));

Console.WriteLine("Success!"); Console.WriteLine("Attempts: " + tries);

Console.Read(); }

public static bool check(int[] p) { // Check all items for (int x = 0; x < p.Length - 1; x++) { if (p[x + 1] <= p[x]) return false; }

return true; }

public static T[] RandomPermutation<T>(T[] array) { T[] retArray = new T[array.Length]; array.CopyTo(retArray, 0);

Random random = new Random(); for (int i = 0; i < array.Length; i += 1) { int swapIndex = random.Next(i, array.Length); if (swapIndex != i) { T temp = retArray[i]; retArray[i] = retArray[swapIndex]; retArray[swapIndex] = temp; } }

return retArray; } }</lang>


D

D 2.0 <lang>import std.stdio, std.random, std.string, std.conv, std.algorithm;

void main() {

   int[] data = [1, 2, 3, 4, 5, 6, 7, 8, 9];
   randomShuffle(data);
   int trial;
   while (!isSorted(data)) {
       writef("%d: (%s) How many numbers to flip? ", ++trial, data);
       data[0 .. to!int(chomp(readln))].reverse;
   }
   writefln("\nYou took %d attempts.", trial);

}</lang>

F#

<lang fsharp>let rand = System.Random() while true do

 let xs = [|for i in 1..9 -> rand.Next(), i|] |> Array.sort |> Array.map snd
 while xs <> Array.sort xs do
   printf "\n%A\n\nReverse how many digits?\n> " xs
   let n = stdin.ReadLine() |> int
   Array.blit (Array.rev xs.[0..n-1]) 0 xs 0 n
 printf "Well done!\n\n"</lang>

Factor

<lang factor>USING: formatting io kernel math math.parser math.ranges namespaces random sequences strings ; IN: rosetta.number-reversal

make-jumbled-array ( -- sorted jumbled )
   CHAR: 1 CHAR: 9 [a,b] [ 1string ] map dup clone randomize
   [ 2dup = ] [ randomize ] while ;

SYMBOL: trials

prompt ( jumbled -- n )
   trials get "#%2d: " printf
   ", " join write
   "   Flip how many? " write flush
   readln string>number ;
game-loop ( sorted jumbled -- )
   2dup = [
       2drop trials get
       "\nYou took %d attempts to put the digits in order!\n" printf
       flush
   ] [
       trials [ 1 + ] change
       dup dup prompt head-slice reverse! drop
       game-loop
   ] if ;
play ( -- )
   0 trials set
   make-jumbled-array game-loop ;</lang>

J

Solution: <lang j>require 'misc' NB. for the verb prompt

INTRO=: noun define Number Reversal Game Flip groups of numbers from the left of the list until the numbers are sorted in ascending order. )

reversegame=: verb define

 nums=. 1+ 9?9                    NB. 1-9 in random order
 score=. 0
 smoutput INTRO                   NB. Display instructions
 while. -.(-: /:~)nums do.
  score=. 1+ score                NB. increment score
  nnum=. 0".prompt (":score),': ',(":nums),' How many numbers to flip?: '
  if. 0 = #nnum do. return. end.  NB. exit on ENTER without number
  nums=. (C.i.-nnum) C. nums      NB. reverse first nnum numbers
 end.
 'You took ',(": score), ' attempts to put the numbers in order.'

)</lang> Example Usage: <lang j> reversegame Number Reversal Game Sort the numbers in ascending order by repeatedly flipping sets of numbers from the left.

1: 4 7 5 6 9 1 2 8 3 How many numbers to flip?: 5 2: 9 6 5 7 4 1 2 8 3 How many numbers to flip?: 9 3: 3 8 2 1 4 7 5 6 9 How many numbers to flip?: 2 4: 8 3 2 1 4 7 5 6 9 How many numbers to flip?: 8 5: 6 5 7 4 1 2 3 8 9 How many numbers to flip?: 3 6: 7 5 6 4 1 2 3 8 9 How many numbers to flip?: 7 7: 3 2 1 4 6 5 7 8 9 How many numbers to flip?: 5 8: 6 4 1 2 3 5 7 8 9 How many numbers to flip?: 6 9: 5 3 2 1 4 6 7 8 9 How many numbers to flip?: 5 10: 4 1 2 3 5 6 7 8 9 How many numbers to flip?: 4 11: 3 2 1 4 5 6 7 8 9 How many numbers to flip?: 3 You took 11 attempts to put the numbers in order.</lang>

JavaScript

Use knuth_shuffle() function from here.

Works with: Firefox

<lang html4strict><html> <head> <script type='text/javascript'>

   function knuth_shuffle(a) {
       var n = a.length;
       var r, temp;
       while (n > 1) {
           r = Math.floor(n * Math.random());
           n--;
           temp = a[n];
           a[n] = a[r];
           a[r] = temp;
       }
       return a;
   }
   function isSorted(ary) {
       return ary.toString() === ary.slice(0).sort().toString();
   }
   function playGame(start_id, progress_id, score_id) {
       var ary = knuth_shuffle([1,2,3,4,5,6,7,8,9]);
       document.getElementById(start_id).innerHTML = ary.toString();
       var count = 0;
       var progress = document.getElementById(progress_id);
       while (! isSorted(ary)) {
           var n = parseInt( prompt('How many elements to reverse?', ) );
           if (isNaN(n) || n < 1 || n > ary.length) break;
           ary = ary.slice(0, n).reverse().concat(ary.slice(n));

progress.innerHTML += '

' + ary.toString() + '

\n';

           count ++;
       }
       document.getElementById(score_id).innerHTML = isSorted(ary) 
           ? "You sorted the numbers in " + count + " turns." 
           : "Game aborted.";
   }

</script> </head>

<body onload='playGame("start","progress","score");'>

</body> </html></lang>

OCaml

Imperative

<lang ocaml>let swap ar i j =

 let tmp = ar.(i) in
 ar.(i) <- ar.(j);
 ar.(j) <- tmp

let shuffle ar =

 for i = pred(Array.length ar) downto 1 do
   let j = Random.int (i + 1) in
   swap ar i j
 done

let reversal ar n =

 for i = 0 to pred(n/2) do
   let j = (pred n) - i in
   swap ar i j
 done

let sorted ar =

 try
   let prev = ref ar.(0) in
   for i = 1 to pred(Array.length ar) do
     if ar.(i) < !prev then raise Exit;
     prev := ar.(i)
   done;
   (true)
 with Exit ->
   (false)

let () =

 print_endline "\
 Number Reversal Game
 Sort the numbers in ascending order by repeatedly 
 flipping sets of numbers from the left.";
 Random.self_init();
 let nums = Array.init 9 (fun i -> succ i) in
 shuffle nums;
 let n = ref 1 in
 while not(sorted nums) do
   Printf.printf "#%2d: " !n;
   Array.iter (Printf.printf " %d") nums;
   print_newline();
   let r = read_int() in
   reversal nums r;
   incr n;
 done;
 print_endline "Congratulations!";
 Printf.printf "You took %d attempts to put the digits in order.\n" !n;
</lang>

Functional

<lang ocaml>let revert li n =

 let rec aux acc i = function
 | [] -> acc
 | xs when i <= 0 -> acc @ xs
 | x::xs -> aux (x::acc) (pred i) xs
 in
 aux [] n li

let take li n =

 let rec aux acc i = function
 | x::xs when i = n -> (x, List.rev_append acc xs)
 | x::xs -> aux (x::acc) (succ i) xs
 | _ -> invalid_arg "take"
 in
 aux [] 0 li

let shuffle li =

 let rec aux acc len = function
 | [] -> acc
 | li ->
     let x, xs = take li (Random.int len) in
     aux (x::acc) (pred len) xs
 in
 aux [] (List.length li) li

let rec sorted = function

 | [] -> (true)
 | x::y::_ when x > y -> (false)
 | x::xs -> sorted xs

let () =

 print_endline "\
 Number Reversal Game
 Sort the numbers in ascending order by repeatedly 
 flipping sets of numbers from the left.";
 Random.self_init();
 let li = shuffle [1; 2; 3; 4; 5; 6; 7; 8; 9] in
 let rec loop n li =
   Printf.printf "#%2d: " n;
   List.iter (Printf.printf " %d") li;
   Printf.printf "  ? %!";
   let r = read_int() in
   let li = revert li r in
   if not(sorted li)
   then loop (succ n) li
   else Printf.printf "You took %d attempts to put the digits in order.\n" n;
 in
 loop 1 li
</lang>

Oz

<lang oz>declare

 proc {Main}
    proc {Loop N Xs}
       if {Not {IsSorted Xs}} then
          Num NewXs
       in
          {System.printInfo N#": "}
          {System.print Xs}
          {System.printInfo " -- Reverse how many? "}
          Num = {String.toInt {ReadLine}}
          NewXs = {Append
                   {Reverse {List.take Xs Num}}
                   {List.drop Xs Num}}
          {Loop N+1 NewXs}
       else
          {System.showInfo "You took "#N#" tries to put the digits in order."}
       end
    end
 in
    {Loop 0 {Shuffle {List.number 1 9 1}}}
 end
 fun {IsSorted Xs}
    {Sort Xs Value.'<'} == Xs
 end
 
 local
    class TextFile from Open.file Open.text end
    StdIn = {New TextFile init(name:stdin)}
 in
    fun {ReadLine}
       {StdIn getS($)}
    end
 end
 
 fun {Shuffle Xs}
    {FoldL Xs
     fun {$ Z _}
        {Pick {Diff Xs Z}}|Z
     end
     nil}
 end

 fun {Pick Xs}
    {Nth Xs {OS.rand} mod {Length Xs} + 1}
 end

 fun {Diff Xs Ys}
    {FoldL Ys List.subtract Xs}
 end

in

 {Main}</lang>

PL/I

<lang PL/I> digits: procedure options (main); /* 23 April 2010 */

  declare s character (9) varying;
  declare i fixed binary;
  declare digit character (1);

restart:

  put skip list ('In this game, you are given a group of digits.');
  put skip list ('You will specify one of those digits.');
  put skip list ('The computer will then reverse the digits up to the one you nominate.');
  put skip list ('Your task is to repeat this process a number of times until all the digits');
  put skip list ('are in order, left to right, 1 to 9.');
  put skip list ('Here are your digits');
  s = ;
  do until (length(s) = 9);
     digit = trim ( fixed(trunc (1+random()*9) ) );
     if index(s, digit) = 0 then s = s || digit;
  end;

loop:

  do forever;
     put skip list (s);
     if s = '123456789' then leave;
     get edit (digit) (a(1));
     i = index(s, digit);
     if i = 0 then do; put skip list ('invalid request'); iterate loop; end;
     s = reverse( substr(s, 1, i) ) || substr(s, i+1, length(s)-i);
  end;
  put skip list ('Congratulations');
  go to restart;

end digits; </lang>

PureBasic

<lang PureBasic>Dim MyList(9)

Declare is_list_sorted()

If OpenConsole()

 Define score, indata, i, txt$
 
 For i=1 To 9         ;- Initiate the list
   MyList(i)=i
 Next
 While is_list_sorted()
   For i=0 To 50      ;- Mix it up & verify it gets scrambeled
     Swap MyList(Random(8)+1),MyList(Random(8)+1)
   Next
 Wend
 
 ;- Start the Game
 Repeat
   score+1
   txt$=RSet(str(score), 3)+": "      ;- Show current list
   For i=1 To 9
     txt$+str(MyList(i))+" "
   Next
   Repeat                             ;- Get input & swap
     Print(txt$+"| How many numbers should be flipped? "): indata=Val(Input())
   Until indata>=1 And indata<=9      ;- Verify the input
   For i=1 To (indata/2)
     Swap MyList(i),MyList(indata-i+1)
   Next
 Until is_list_sorted()
 
 ;- Present result & wait for users input before closing down
 PrintN(#CRLF$+"You did it in "+str(score)+" moves")
 Print("Press ENTER to exit"): Input()
 CloseConsole()

EndIf

Procedure is_list_sorted()

 Protected i
 Shared MyList()
 For i=1 To 9
   If MyList(i)<>i
     ProcedureReturn #False
   EndIf
 Next
 ProcedureReturn #True

EndProcedure</lang>

Python

<lang python> number reversal game

   Given a jumbled list of the numbers 1 to 9
   Show the list.
   Ask the player how many digits from the left to reverse.
   Reverse those digits then ask again.
   until all the digits end up in ascending order.

import random

print(__doc__) data, trials = list('123456789'), 0 while data == sorted(data):

   random.shuffle(data)

while data != sorted(data):

   trials += 1
   flip = int(input('#%2i: LIST: %r Flip how many?: ' % (trials, ' '.join(data))))
   data[:flip] = reversed(data[:flip])

print('\nYou took %2i attempts to put the digits in order!' % trials)</lang>

Sample output:

number reversal game
    Given a jumbled list of the numbers 1 to 9
    Show the list.
    Ask the player how many digits from the left to reverse.
    Reverse those digits then ask again.
    until all the digits end up in ascending order.


# 1: LIST: '1 3 9 2 7 5 4 8 6' Flip how many?: 9
# 2: LIST: '6 8 4 5 7 2 9 3 1' Flip how many?: 6
# 3: LIST: '2 7 5 4 8 6 9 3 1' Flip how many?: 8
# 4: LIST: '3 9 6 8 4 5 7 2 1' Flip how many?: 7
# 5: LIST: '7 5 4 8 6 9 3 2 1' Flip how many?: 3
# 6: LIST: '4 5 7 8 6 9 3 2 1' Flip how many?: 6
# 7: LIST: '9 6 8 7 5 4 3 2 1' Flip how many?: 2
# 8: LIST: '6 9 8 7 5 4 3 2 1' Flip how many?: 4
# 9: LIST: '7 8 9 6 5 4 3 2 1' Flip how many?: 3
#10: LIST: '9 8 7 6 5 4 3 2 1' Flip how many?: 9

You took 10 attempts to put the digits in order!

R

<lang>reversalGame <- function(){

 cat("Welcome to the Number Reversal Game! \n")
 cat("Sort the numbers into ascending order by repeatedly \n",
     "reversing the first n digits, where you specify n. \n \n", sep="")
 # Generate a list that's definitely not in ascending order, per instuctions
 data <- sample(1:9, 9)
 while (all(data == 1:9)){
   cat("What were the chances...? \n")
   data <- sample(1:9, 9)
 }
 trials <- 0
 # Play the game
 while (any(data != 1:9)){
   trials <- trials + 1
   cat("Trial", sprintf("%02d", trials), " # ", data, " #  ")
   answer <- readline(paste("Flip how many? "))
   data[1:answer] <- data[answer:1]  
 }
 
 # Victory!
 cat("Well done.  You needed", trials, "flips. \n")

}</lang>

Sample output: <lang>>reversalGame() Welcome to the Number Reversal Game! Sort the numbers into ascending order by repeatedly reversing the first n digits, where you specify n.

Trial 01 # 4 3 2 1 7 9 8 5 6 # Flip how many? 7 Trial 02 # 8 9 7 1 2 3 4 5 6 # Flip how many? 2 Trial 03 # 9 8 7 1 2 3 4 5 6 # Flip how many? 9 Trial 04 # 6 5 4 3 2 1 7 8 9 # Flip how many? 6 Well done. You needed 4 flips.</lang>

Ruby

<lang ruby>ary = (1..9).to_a.shuffle score = 0 until ary == ary.sort

 print "#{ary.inspect} -- How many digits to reverse? "
 num = gets.to_i  # should validate input
 ary[0, num] = ary[0, num].reverse
 score += 1

end p ary puts "Your score: #{score}"</lang>

sample output:

$ ruby number_reversal_game.rb
[4, 2, 1, 8, 7, 6, 3, 5, 9] -- How many digits to reverse? 4
[8, 1, 2, 4, 7, 6, 3, 5, 9] -- How many digits to reverse? 8
[5, 3, 6, 7, 4, 2, 1, 8, 9] -- How many digits to reverse? 4
[7, 6, 3, 5, 4, 2, 1, 8, 9] -- How many digits to reverse? 7
[1, 2, 4, 5, 3, 6, 7, 8, 9] -- How many digits to reverse? 4
[5, 4, 2, 1, 3, 6, 7, 8, 9] -- How many digits to reverse? 5
[3, 1, 2, 4, 5, 6, 7, 8, 9] -- How many digits to reverse? 3
[2, 1, 3, 4, 5, 6, 7, 8, 9] -- How many digits to reverse? 2
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Your score: 8

Tcl

<lang tcl>package require Tcl 8.5

  1. Simple shuffler, not very efficient but good enough for here

proc shuffle list {

   set result {}
   while {[llength $list]} {

set i [expr {int([llength $list] * rand())}] lappend result [lindex $list $i] set list [lreplace $list $i $i]

   }
   return $result

}

  1. Returns the list with the prefix of it reversed

proc flipfirst {list n} {

   concat [lreverse [lrange $list 0 $n-1]] [lrange $list $n end]

}

  1. Core game engine; list to play with is optional argument

proc nrgame Template:Target "1 2 3 4 5 6 7 8 9" {

   set nums $target
   while {$nums eq $target} {set nums [shuffle $nums]}
   set goes 0
   while {$nums ne $target} {

incr goes puts -nonewline "#${goes}: List is '[join $nums {, }]', how many to reverse? " flush stdout gets stdin n if {$n eq "q"} {return quit} # Input validation would go here set nums [flipfirst $nums $n]

   }
   return $goes

}

  1. Print some instructions and wait for the user to win

puts "Welcome to the Number Reversal Game!" puts "------------------------------------" puts "I'll show you a list of numbers, you need to reverse prefixes of them" puts "to get the whole list in ascending order. A 'q' will quit early.\n" puts "" set outcome [nrgame] if {$outcome ne "quit"} {

   puts "\nYou took $outcome attempts to put the digits in order."

}</lang> Sample output:

Welcome to the Number Reversal Game!
------------------------------------
I'll show you a list of numbers, you need to reverse prefixes of them
to get the whole list in ascending order. A 'q' will quit early.


#1: List is '8, 6, 2, 5, 7, 9, 3, 1, 4', how many to reverse? 6
#2: List is '9, 7, 5, 2, 6, 8, 3, 1, 4', how many to reverse? 9
#3: List is '4, 1, 3, 8, 6, 2, 5, 7, 9', how many to reverse? 4
#4: List is '8, 3, 1, 4, 6, 2, 5, 7, 9', how many to reverse? 8
#5: List is '7, 5, 2, 6, 4, 1, 3, 8, 9', how many to reverse? 7
#6: List is '3, 1, 4, 6, 2, 5, 7, 8, 9', how many to reverse? 4
#7: List is '6, 4, 1, 3, 2, 5, 7, 8, 9', how many to reverse? 6
#8: List is '5, 2, 3, 1, 4, 6, 7, 8, 9', how many to reverse? 5
#9: List is '4, 1, 3, 2, 5, 6, 7, 8, 9', how many to reverse? 4
#10: List is '2, 3, 1, 4, 5, 6, 7, 8, 9', how many to reverse? 2
#11: List is '3, 2, 1, 4, 5, 6, 7, 8, 9', how many to reverse? 3

You took 11 attempts to put the digits in order.