Number reversal game: Difference between revisions

Line 3,614:
done</lang>
 
=={{header|VBA}}==
{{trans|Phix}}<lang vb>Private Function shuffle(ByVal a As Variant) As Variant
Dim t As Variant, i As Integer
For i = UBound(a) To LBound(a) + 1 Step -1
j = Int((UBound(a) - LBound(a) + 1) * Rnd + LBound(a))
t = a(i)
a(i) = a(j)
a(j) = t
Next i
shuffle = a
End Function
Private Sub reverse(ByRef a As Variant, n As Integer)
Dim b As Variant
b = a
For i = 1 To n
a(i) = b(n + 1 - i)
Next i
End Sub
Public Sub game()
Debug.Print "Given a jumbled list of the numbers 1 to 9"
Debug.Print "you must select how many digits from the left to reverse."
Debug.Print "Your goal is to get the digits in order with 1 on the left and 9 on the right."
inums = [{1,2,3,4,5,6,7,8,9}]
Dim nums As Variant
Dim turns As Integer, flip As Integer
nums = shuffle(inums)
Do While True
Debug.Print turns; ":";
For Each x In nums: Debug.Print x;: Next x
Debug.Print
flag = False
For i = LBound(nums) To UBound(nums)
If nums(i) <> inums(i) Then
flag = True
Exit For
End If
Next i
If flag Then
flip = InputBox(" -- How many numbers should be flipped? ")
reverse nums, flip
turns = turns + 1
Else
Exit Do
End If
Loop
Debug.Print "You took"; turns; "turns to put the digits in order."
End Sub</lang>{{out}}
<pre>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 3 9 8 7 6 2 5
1 : 9 3 4 1 8 7 6 2 5
2 : 5 2 6 7 8 1 4 3 9
3 : 8 7 6 2 5 1 4 3 9
4 : 3 4 1 5 2 6 7 8 9
5 : 5 1 4 3 2 6 7 8 9
6 : 2 3 4 1 5 6 7 8 9
7 : 4 3 2 1 5 6 7 8 9
8 : 1 2 3 4 5 6 7 8 9
You took 8 turns to put the digits in order.</pre>
=={{header|XPL0}}==
<lang XPL0>int Taken, I, Digit, Num, Score, Rev, Temp;
255

edits