Anonymous user
Number reversal game: Difference between revisions
m
→{{header|REXX}}: added more validation, implemented a fence to help identify user's input(s), supported QUIT and halting of program, added more whitespace between displays.
No edit summary |
m (→{{header|REXX}}: added more validation, implemented a fence to help identify user's input(s), supported QUIT and halting of program, added more whitespace between displays.) |
||
Line 2,511:
=={{header|REXX}}==
This REXX version:
<lang rexx>/*REXX program game: reverse a jumbled set of numerals until in order.*/▼
:::* displays the game's objective and rules
:::* validates the input (must be a single non-zero digit)
:::* allows the user to enter '''quit'''
:::* allows the user to halt the game via '''Cntl-Break'''
___=copies('─',9) /*a fence used for computer's messages.*/
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 ··· */▼
do score=1 until $==ok /* [↓] display the digs and the prompt*/
▲say "This game will show you nine random unique digits (1 ──► 9 inclusive), and"
say; say ___ $ right('please enter a digit (or Quit):', 50)
▲say "you'll enter one of those digits which will reverse the digits up to (and"
pull x .; ?=left(x,1) /*get a decimal digit (maybe) from CBLF*/
▲say "including) that digit. The game's objective is to get all the digits in"
if abbrev('QUIT',x,1) then signal halt
▲say "ascending order with the fewest tries. Here're your digits:"; say
▲$=''
if x=='' then
if g==0 then say ___ 'oops, invalid digit! ' ?
else $=reverse(left($, g))substr($, g+1)▼
▲ end /*until*/
▲ parse pull ? 2 . /*get one digit from the gamer. */
▲ g=pos(?,$) /*full validation of input digit.*/
▲ if g==0 then say 'oops, invalid digit!' ?
▲ else $=reverse(left($,g))substr($,g+1)
end /*score*/
say; say ___ $; say; say center(' Congratulations! ',
say ___ 'Your score was' score;
halt: say ___ 'quitting.'; exit /* " " " " " " " " */</lang>
'''output''' from playing one game of the ''number reversal game'':
<pre>
───────── 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 up to (and
───────── including) that digit. The game's objective is to get all the
───────── digits in ascending order with the fewest tries. Here are your digits:
───────── 613279548 please enter a digit (or Quit):
5
───────── 597231648 please enter a digit (or Quit):
9
───────── 957231648 please enter a digit (or Quit):
8
───────── 846132759 please enter a digit (or Quit):
5
───────── 572316489 please enter a digit (or Quit):
7
───────── 752316489 please enter a digit (or Quit):
4
───────── 461325789 please enter a digit (or Quit):
6
───────── 641325789 please enter a digit (or Quit):
5
───────── 523146789 please enter a digit (or Quit):
4
───────── 413256789 please enter a digit (or Quit):
2
───────── 231456789 please enter a digit (or Quit):
3
───────── 321456789 please enter a digit (or Quit):
1
───────── 123456789
══════════════════════════ Congratulations! ══════════════════════════
───────── Your score was 12
</pre>
=={{header|Ruby}}==
|