Number reversal game: Difference between revisions

Content added Content deleted
No edit summary
Line 2,122: Line 2,122:
numList:show()
numList:show()
print("\n\nW00t! You scored:", score)</lang>
print("\n\nW00t! You scored:", score)</lang>

=={{header|M2000 Interpreter}}==
Using some ready made Print's from BASIC example

Using stack of values which we can move easy values. When we call subs and pass arguments that arguments as values stored to same stack, but because we pop them (read to variables) we leave stack with 9 values for the game.

To check if we have all values in order, we just check the difference of two items one after the other, if is one, and if not one then we leave checking returning false.

Sub CheckStack get a value by reference. Because stack of values always get values, a reference is a copy of a weak reference, so in CheckStack a new variable defined as local ok, and get the reference from weak reference in stack. When a sub exit any new,including local variables removed (deleted). Subs are always local to Module, and have same scope as module scope.
<lang M2000 Interpreter>
Module Number_Reversal_Game {
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."
\\ work on a new stack - old stack parked, and attached at the exit of this block
Stack New {
Data 1,2,3,4,5,6,7,8,9
\\ Create jumbled list
For i=1 to 30: Reverse(Random(2,9)):Next i
Tries=0
fin=false
Repeat {
\\ Show Stack
Stack
Try ok {
INPUT " -- How many numbers should be flipped:", flp%
}
if not Ok then continue
if flp%<2 or flp%>9 then continue
Reverse(flp%)
Tries++
CheckStack(&fin)
} Until Fin
\\ show stack again
Stack
PRINT "You took "; tries; " tries to put the digits in order."
}
Sub Reverse(n)
Shift 1, -n ' shift one item nth times in reverse
End Sub
Sub CheckStack(&ok)
ok=true
if stack.size<2 then exit sub
Local i
For i=2 to stack.size {
ok=stackitem(i)-stackitem(i-1)=1
if ok else exit
}
End Sub
}
Number_Reversal_Game
</lang>


=={{header|Mathematica}}==
=={{header|Mathematica}}==