Number reversal game: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|J}}: more robust for bad inputs)
(→‎BASIC: improved version; now works with FreeBASIC)
Line 12: Line 12:
{{works with|QBasic}}
{{works with|QBasic}}


{{works with|FreeBASIC}}
<lang qbasic>PRINT "number reversal game"

PRINT " Given a jumbled list of the numbers 1 to 9"
PRINT " Show the list."
<lang qbasic>PRINT "Given a jumbled list of the numbers 1 to 9,"
PRINT " Ask the player how many digits from the left to reverse."
PRINT "you must select how many digits from the left to reverse."
PRINT " Reverse those digits then ask again."
PRINT "Your goal is to get the digits in order with 1 on the left and 9 on the right."
PRINT " until all the digits end up in ascending order."


RANDOMIZE TIMER
RANDOMIZE TIMER


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


'initial values and Knuth shuffle
'initial values and Knuth shuffle
Line 33: Line 32:
NEXT
NEXT


tries = 1
again = -1
DO
DO
IF tries < 10 THEN PRINT " ";
IF tries < 10 THEN PRINT " ";
Line 41: Line 40:
NEXT
NEXT


IF NOT again THEN EXIT DO
INPUT "How many numbers should be flipped"; flip
IF flip < 0 THEN flip = 0
IF flip > 9 THEN flip = 9


INPUT " -- How many numbers should be flipped"; flp
FOR L0 = 1 TO (flip \ 2)
SWAP nums(L0), nums(flip - L0 + 1)
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
NEXT


Line 53: Line 54:
FOR L0 = 1 TO 8
FOR L0 = 1 TO 8
IF nums(L0) > nums(L0 + 1) THEN
IF nums(L0) > nums(L0 + 1) THEN
tries = tries + 1
again = -1
again = -1
EXIT FOR
EXIT FOR
END IF
END IF
NEXT
NEXT
LOOP WHILE again


IF flp > 0 THEN tries = tries + 1
PRINT "You took "; tries; " attempts to put the digits in order"</lang>
LOOP

PRINT : PRINT "You took"; tries; " tries to put the digits in order."</lang>


Sample output:
Sample output:
Given a jumbled list of the numbers 1 to 9,
number reversal game
you must select how many digits from the left to reverse.
Given a jumbled list of the numbers 1 to 9
Your goal is to get the digits in order with 1 on the left and 9 on the right.
Show the list.
0 : 1 4 5 3 2 7 6 8 9 -- How many numbers should be flipped? 7
Ask the player how many digits from the left to reverse.
1 : 6 7 2 3 5 4 1 8 9 -- How many numbers should be flipped? 2
Reverse those digits then ask again.
2 : 7 6 2 3 5 4 1 8 9 -- How many numbers should be flipped? 7
until all the digits end up in ascending order.
1 : 2 6 9 1 3 7 5 8 4 How many numbers should be flipped? 3
3 : 1 4 5 3 2 6 7 8 9 -- How many numbers should be flipped? 3
2 : 9 6 2 1 3 7 5 8 4 How many numbers should be flipped? 9
4 : 5 4 1 3 2 6 7 8 9 -- How many numbers should be flipped? 5
3 : 4 8 5 7 3 1 2 6 9 How many numbers should be flipped? 2
5 : 2 3 1 4 5 6 7 8 9 -- How many numbers should be flipped? 2
4 : 8 4 5 7 3 1 2 6 9 How many numbers should be flipped? 8
6 : 3 2 1 4 5 6 7 8 9 -- How many numbers should be flipped? 3
5 : 6 2 1 3 7 5 4 8 9 How many numbers should be flipped? 5
7 : 1 2 3 4 5 6 7 8 9
You took 7 tries to put the digits in order.
6 : 7 3 1 2 6 5 4 8 9 How many numbers should be flipped? 7
7 : 4 5 6 2 1 3 7 8 9 How many numbers should be flipped? 3
8 : 6 5 4 2 1 3 7 8 9 How many numbers should be flipped? 6
9 : 3 1 2 4 5 6 7 8 9 How many numbers should be flipped? 3
10 : 2 1 3 4 5 6 7 8 9 How many numbers should be flipped? 2
You took 10 attempts to put the digits in order


=={{header|Factor}}==
=={{header|Factor}}==

Revision as of 20:12, 4 April 2010

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, 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.

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 and Knuth shuffle FOR L0 = 1 TO 9

   nums(L0) = L0

NEXT FOR L0 = 9 TO 1 STEP -1

   n = INT(RND * (L0)) + 1
   IF n <> L0 THEN SWAP nums(n), nums(L0)

NEXT

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"; 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.

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=. >: 9?9                             NB. 1-9 in random order
 score=. 0
 smoutput INTRO                            NB. Display instructions
 while. (-.@-: /:~) nums do.
  score=. >: score                         NB. increment score
  nnum=. 0&".@prompt (;":&.>score;': ';nums), ' How many numbers to flip?: '
  if. 0 = #nnum do. return. end.           NB. exit on ENTER
  nums=. nnum |.@{.`(i.@[)`]} :: ] 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>

PureBasic

<lang PureBasic>Define i Dim MyList(9)

For i=1 To 9  ;- Initiate the list

 MyList(i)=i

Next

For i=0 To 50  ;- Mix it up

 Swap MyList(Random(8)+1),MyList(Random(8)+1)

Next

If OpenConsole() ;Start playing

 Define ok=#False, score, indata
 Repeat
   score+1
   Print(RSet(str(score), 3)+": ")      ;- Show current list
   For i=1 To 9
     Print(str(MyList(i))+" ")
   Next
   
   Repeat                               ;- Get input & swap
     Print(#CRLF$+"How many numbers should be flipped? ")
     indata=Val(Input())
   Until indata>=1 And indata<=9
   For i=1 To (indata/2)
     Swap MyList(i),MyList(indata-i+1)
   Next
   
   ok=#True                             ;- Check if ok?
   For i=1 To 9
     If MyList(i)<>i
       ok=#False
     EndIf
   Next
 Until ok
 PrintN(#CRLF$+"You did it in "+str(score)+" moves")
 Print("Press ENTER to exit"): Input()
 CloseConsole()

EndIf</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!

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.