Jump to content

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.*/
:::* &nbsp; displays the game's objective and rules
:::* &nbsp; validates the input &nbsp; (must be a single non-zero digit)
:::* &nbsp; allows the user to enter &nbsp; '''quit'''
:::* &nbsp; allows the user to halt the game via '''Cntl-Break'''
<lang rexx>/*REXX programpgm (a game): reverse a jumbled set of numerals until they're in order.*/
signal on parsehalt pull ? 2 . /*getallows onethe digitCBLF fromto the gamer.HALT the program.*/
___=copies('─',9) /*a fence used for computer's messages.*/
say ___ "This game will show you nine random unique digits (1 ──► 9 inclusive), and you'll"
say "you'll___ "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 digits in"
say ___ "digits in ascending order with the fewest tries. Here're are your digits:"; say
gok=pos(?,$)123456789 /*fullthe result that validationthe ofstring inputshould digitbe.*/
$=''
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 g==0length(x)>1 then do; say ___ 'oops, invalid digitinput! ' ? x; iterate; end
$=''
if x=='' then do iterate until length($)==9 /*generatetry again, CBLF randomdidn't numericenter string.anything*/
_g=randompos(1?,9$); if pos(_,$)\==0 then iterate /*novalidate if the input digit is repeatslegal.*/
if g==0 then say ___ 'oops, invalid digit! ' ?
$=$ || _
else $=reverse(left($, g))substr($, g+1)
end /*until*/
 
do score=1 until $==123456789 /*keep truckin' until all ordered*/
say $ left('',30) 'please enter a digit:' /*issue a prompt to user.*/
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! ',7970,"═"); say 'Your score was' score
say ___ 'Your score was' score; exit /*stick a fork in it, we're all done. */</lang>
halt: say ___ 'quitting.'; exit /* " " " " " " " " */</lang>
'''output''' &nbsp; from playing one game of the &nbsp; ''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}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.