June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,18 +1,10 @@
|
|||
'''
|
||||
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.
|
||||
'''.print()
|
||||
|
||||
var data = list('123456789')
|
||||
var trials = 0
|
||||
while data == sorted(data): random.shuffle data
|
||||
print '# Number reversal game'
|
||||
var data, trials = list('123456789'), 0
|
||||
while data == sorted(data):
|
||||
random.shuffle data
|
||||
while data != sorted(data):
|
||||
trials += 1
|
||||
flip = int scan '#$trials: LIST: $(join data) Flip how many?: '
|
||||
data[:flip] = data[!flip:]
|
||||
trials += 1
|
||||
flip = int input '#$trials: LIST: $(join data) Flip how many?: '
|
||||
data[:flip] = data[!flip:]
|
||||
|
||||
print '\nYou took $trials attempts to put digits in order!'
|
||||
|
|
|
|||
|
|
@ -1,29 +1,29 @@
|
|||
/*REXX pgm (a game): reverse a jumbled set of numerals until they're in order.*/
|
||||
signal on halt /*allows the CBLF to HALT the program.*/
|
||||
___=copies('─',9) /*a fence used for computer's messages.*/
|
||||
say ___ "This game will show you nine random unique digits (1 ──► 9), and you'll"
|
||||
say ___ "enter one of those digits which will reverse all the digits up to (and"
|
||||
say ___ "including) that digit. The game's objective is to get all the"
|
||||
say ___ "digits in ascending order with the fewest tries. Here are your digits:"
|
||||
ok=123456789 /*the result that the string should be.*/
|
||||
$=
|
||||
do until length($)==9 /*build a random unique numeric string.*/
|
||||
_=random(1,9); if pos(_,$)\==0 then iterate /*only use a dig once.*/
|
||||
$=$ || _ /*construct a string. */
|
||||
if $==ok then $= /*string can't be in order, start over.*/
|
||||
end /*until ··· */
|
||||
/*REXX program (a game): reverse a jumbled set of decimal digits 'til they're in order.*/
|
||||
signal on halt /*allows the CBLF to HALT the program.*/
|
||||
___= copies('─', 9); pad=left('', 9) /*a fence used for computer's messages.*/
|
||||
say ___ "This game will show you nine random unique digits (1 ──► 9), and you'll enter"
|
||||
say ___ "one of those digits which will reverse all the digits from the left-most digit"
|
||||
say ___ "up to (and including) that decimal digit. The game's objective is to get all"
|
||||
say ___ "of the digits in ascending order with the fewest tries. Here're your digits:"
|
||||
ok= 123456789 /*the result that the string should be.*/
|
||||
$= /* ◄─── decimal target to be generated.*/
|
||||
do until length($)==9; _=random(1, 9) /*build a random unique numeric string.*/
|
||||
if pos(_, $) \== 0 then iterate /*¬ unique? Only use a decimal dig once*/
|
||||
$=$ || _ /*construct a string of unique digits. */
|
||||
if $==ok then $= /*string can't be in order, start over.*/
|
||||
end /*until*/
|
||||
|
||||
do score=1 until $==ok /* [↓] display the digs and the prompt*/
|
||||
say; say ___ $ right('please enter a digit (or Quit):', 50)
|
||||
pull x .; ?=left(x,1) /*get a decimal digit (maybe) from CBLF*/
|
||||
if abbrev('QUIT',x,1) then signal halt
|
||||
if length(x)>1 then do; say ___ 'oops, invalid input! ' x; iterate; end
|
||||
if x=='' then iterate /*try again, CBLF didn't enter anything*/
|
||||
g=pos(?,$) /*validate if the input digit is legal.*/
|
||||
if g==0 then say ___ 'oops, invalid digit! ' ?
|
||||
else $=reverse(left($, g))substr($, g+1)
|
||||
do score=1 until $==ok; say /* [↓] display the digits & the prompt*/
|
||||
say ___ $ right('please enter a digit (or Quit):', 50)
|
||||
parse pull ox . 1 ux . 1 x .; upper ux /*get a decimal digit (maybe) from CBLF*/
|
||||
if abbrev('QUIT', ux, 1) then signal halt /*does the CBLF want to quit this game?*/
|
||||
if length(x)>1 then do; say ___ pad '***error*** invalid input: ' ox; iterate; end
|
||||
if x='' then iterate /*try again, CBLF didn't enter anything*/
|
||||
g=pos(x, $) /*validate if the input digit is legal.*/
|
||||
if g==0 then say ___ pad '***error*** invalid digit: ' ox
|
||||
else $=strip(reverse(left($,g))substr($,g+1)) /*reverse some (or all) digits*/
|
||||
end /*score*/
|
||||
|
||||
say; say ___ $; say; say center(' Congratulations! ',70,"═"); say
|
||||
say ___ 'Your score was' score; exit /*stick a fork in it, we're all done. */
|
||||
halt: say ___ 'quitting.'; exit /* " " " " " " " " */
|
||||
say; say ___ $; say; say center(' Congratulations! ', 70, "═"); say
|
||||
say ___ pad 'Your score was' score; exit /*stick a fork in it, we're all done. */
|
||||
halt: say ___ pad 'quitting.'; exit /* " " " " " " " " */
|
||||
|
|
|
|||
53
Task/Number-reversal-game/Ring/number-reversal-game.ring
Normal file
53
Task/Number-reversal-game/Ring/number-reversal-game.ring
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
# Project : Number reversal game
|
||||
# Date : 2017/12/02
|
||||
# Author : Gal Zsolt (~ CalmoSoft ~)
|
||||
# Email : <calmosoft@gmail.com>
|
||||
|
||||
rever = 1:9
|
||||
leftrever = []
|
||||
for n = 1 to len(rever)
|
||||
rnd = random(8) + 1
|
||||
temp = rever[n]
|
||||
rever[n] = rever[rnd]
|
||||
rever[rnd] = temp
|
||||
next
|
||||
see rever
|
||||
see nl
|
||||
while true
|
||||
num = 0
|
||||
leftrever = []
|
||||
showarray(rever)
|
||||
see " : Reverse how many = "
|
||||
give r
|
||||
r = number(r)
|
||||
for n = 1 to r
|
||||
add(leftrever, rever[n])
|
||||
next
|
||||
leftrever = reverse(leftrever)
|
||||
for pos = 1 to r
|
||||
rever[pos] = leftrever[pos]
|
||||
next
|
||||
for m = 1 to len(rever)
|
||||
if rever[m] = m
|
||||
num = num + 1
|
||||
ok
|
||||
next
|
||||
if num = len(rever)
|
||||
exit
|
||||
ok
|
||||
end
|
||||
see "You took " + num + " attempts." + nl
|
||||
|
||||
func swap(a, b)
|
||||
temp = a
|
||||
a = b
|
||||
b = temp
|
||||
return [a, b]
|
||||
|
||||
func showarray(vect)
|
||||
svect = ""
|
||||
for n = 1 to len(vect)
|
||||
svect = svect + vect[n] + " "
|
||||
next
|
||||
svect = left(svect, len(svect) - 1)
|
||||
see svect
|
||||
Loading…
Add table
Add a link
Reference in a new issue