Number reversal game: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added/changed comments and whitespace, used a template for the OUTPUTs.)
Line 2,846: Line 2,846:
:::*   allows the user to enter   '''quit'''
:::*   allows the user to enter   '''quit'''
:::*   allows the user to halt the game via   '''Cntl-Break'''   (or equivalent)
:::*   allows the user to halt the game via   '''Cntl-Break'''   (or equivalent)
<lang rexx>/*REXX pgm (a game): reverse a jumbled set of numerals until they're in order.*/
<lang rexx>/*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.*/
signal on halt /*allows the CBLF to HALT the program.*/
___=copies('─',9) /*a fence used for computer's messages.*/
___= 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"
say ___ "This game will show you nine random unique digits (1 ──► 9), and you'll enter"
say ___ "enter one of those digits which will reverse all the digits up to (and"
say ___ "one of those digits which will reverse all the digits from the left-most digit"
say ___ "including) that digit. The game's objective is to get all the"
say ___ "up to (and including) that decimal digit. The game's objective is to get all"
say ___ "digits in ascending order with the fewest tries. Here are your digits:"
say ___ "of the digits in ascending order with the fewest tries. Here're your digits:"
ok=123456789 /*the result that the string should be.*/
ok= 123456789 /*the result that the string should be.*/
$= /* ◄─── decimal target to be generated.*/
$=
do until length($)==9 /*build a random unique numeric string.*/
do until length($)==9; _=random(1, 9) /*build a random unique numeric string.*/
_=random(1,9); if pos(_,$)\==0 then iterate /*only use a dig once.*/
if pos(_, $) \== 0 then iterate /*Unique? Only use a decimal digit once*/
$=$ || _ /*construct a string. */
$=$ || _ /*construct a string of unique digits. */
if $==ok then $= /*string can't be in order, start over.*/
if $==ok then $= /*string can't be in order, start over.*/
end /*until ··· */
end /*until*/


do score=1 until $==ok /* [↓] display the digs and the prompt*/
do score=1 until $==ok; say /* [↓] display the digits & the prompt*/
say; say ___ $ right('please enter a digit (or Quit):', 50)
say ___ $ right('please enter a digit (or Quit):', 50)
pull x .; ?=left(x,1) /*get a decimal digit (maybe) from CBLF*/
parse pull ox . 1 ux . 1 x .; upper ux /*get a decimal digit (maybe) from CBLF*/
if abbrev('QUIT',x,1) then signal halt
if abbrev('QUIT', ux, 1) then signal halt /*does the CBLF want to quit this game?*/
if length(x)>1 then do; say ___ 'oops, invalid input! ' x; iterate; end
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*/
if x='' then iterate /*try again, CBLF didn't enter anything*/
g=pos(?,$) /*validate if the input digit is legal.*/
g=pos(x, $) /*validate if the input digit is legal.*/
if g==0 then say ___ 'oops, invalid digit! ' ?
if g==0 then say ___ pad '***error*** invalid digit: ' ox
else $=reverse(left($, g))substr($, g+1)
else $=strip(reverse(left($,g))substr($,g+1)) /*reverse some (or all) digits*/
end /*score*/
end /*score*/


say; say ___ $; say; say center(' Congratulations! ',70,"═"); say
say; say ___ $; say; say center(' Congratulations! ', 70, "═"); say
say ___ 'Your score was' score; exit /*stick a fork in it, we're all done. */
say ___ pad 'Your score was' score; exit /*stick a fork in it, we're all done. */
halt: say ___ 'quitting.'; exit /* " " " " " " " " */</lang>
halt: say ___ pad 'quitting.'; exit /* " " " " " " " " */</lang>
'''output''' &nbsp; from playing one game of the &nbsp; ''number reversal game'':
{{out|output|text=&nbsp; from playing one game of the &nbsp; ''number reversal game'':
<pre>
<pre>
───────── This game will show you nine random unique digits (1 ──► 9), and you'll
───────── This game will show you nine random unique digits (1 ──► 9), and you'll enter
───────── enter one of those digits which will reverse all the digits up to (and
───────── one of those digits which will reverse all the digits from the left-most digit
───────── including) that digit. The game's objective is to get all the
───────── up to (and including) that decimal digit. The game's objective is to get all
───────── digits in ascending order with the fewest tries. Here are your digits:
───────── of the digits in ascending order with the fewest tries. Here're your digits:


───────── 613279548 please enter a digit (or Quit):
───────── 613279548 please enter a digit (or Quit):
Line 2,922: Line 2,922:
══════════════════════════ Congratulations! ══════════════════════════
══════════════════════════ Congratulations! ══════════════════════════


───────── Your score was 12
───────── Your score was 12
</pre>
</pre>