Task

Given a jumbled list of the numbers   1   to   9   that are definitely   not   in
ascending order.

Show the list,   and 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 player's input does not need extra validation.



Related tasks

   Sorting algorithms/Pancake sort
   Pancake sorting.
   Topswops


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
  whilst. (-: /:~)nums do.
    nums=. 1+9?9                    NB. 1-9 in random order
  end.
  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.'
)