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 |
<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) |
___= copies('─', 9); pad=left('', 9) /*a fence used for computer's messages.*/ |
||
say ___ "This game will show you nine random unique digits (1 ──► 9), |
say ___ "This game will show you nine random unique digits (1 ──► 9), and you'll enter" |
||
say ___ |
say ___ "one of those digits which will reverse all the digits from the left-most digit" |
||
say ___ "including) that digit. |
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. |
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 |
do until length($)==9; _=random(1, 9) /*build a random unique numeric string.*/ |
||
if pos(_, $) \== 0 then iterate /*Unique? Only use a decimal digit once*/ |
|||
$=$ || _ |
$=$ || _ /*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 |
do score=1 until $==ok; say /* [↓] display the digits & the prompt*/ |
||
say ___ $ right('please enter a digit (or Quit):', 50) |
|||
pull |
parse pull ox . 1 ux . 1 x .; upper ux /*get a decimal digit (maybe) from CBLF*/ |
||
if abbrev('QUIT', |
if abbrev('QUIT', ux, 1) then signal halt /*does the CBLF want to quit this game?*/ |
||
if length(x)>1 then do; say ___ ' |
if length(x)>1 then do; say ___ pad '***error*** invalid input: ' ox; iterate; end |
||
if x |
if x='' then iterate /*try again, CBLF didn't enter anything*/ |
||
g=pos( |
g=pos(x, $) /*validate if the input digit is legal.*/ |
||
if g==0 then say |
if g==0 then say ___ pad '***error*** invalid digit: ' ox |
||
else $=reverse(left($, |
else $=strip(reverse(left($,g))substr($,g+1)) /*reverse some (or all) digits*/ |
||
end /*score*/ |
end /*score*/ |
||
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> |
||
{{out|output|text= from playing one game of the ''number reversal game'': |
|||
<pre> |
<pre> |
||
───────── This game will show you nine random unique digits (1 ──► 9), |
───────── This game will show you nine random unique digits (1 ──► 9), and you'll enter |
||
───────── |
───────── one of those digits which will reverse all the digits from the left-most digit |
||
───────── including) that digit. |
───────── up to (and including) that decimal digit. The game's objective is to get all |
||
───────── digits in ascending order with the fewest tries. |
───────── 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> |
||