Anonymous user
Penney's game: Difference between revisions
m
→{{header|REXX}}: added/changed comments and whitespace, used a template for the output section.
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
m (→{{header|REXX}}: added/changed comments and whitespace, used a template for the output section.) |
||
Line 2,794:
A feature also added was to allow a seed for the '''random''' BIF to allow repeatability for a game.
<lang rexx>/*REXX program plays/simulates Penney's Game, a
__= copies('─', 9)
signal on halt /*a clean way out if CLBF quits. */
parse arg # seed . /*obtain optional arguments from the CL*/
if #=='' | #=="," then #=
if datatype(seed,'W') then call random ,,seed /*use seed for RANDOM #s repeatability.*/
wins=0; do games=1 /*simulate a number of Penney's games. */
Line 2,806:
/*──────────────────────────────────────────────────────────────────────────────────────*/
halt: say; say __ "Penney's Game was halted."; say; exit 13
r: arg ,$; do arg(1); $=$ || random(
s: if arg(1)==1 then return arg(3); return word(arg(2) 's',1) /*pluralizer.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
game: @.=; tosses=@. /*the coin toss sequence so far. */
toss1= r(1)
if \toss1 then call randComp /*maybe let the computer go first*/
if toss1 then say __ "You win the first toss, so you pick your sequence first."
Line 2,820:
say __ "computer's pick:" @.comp /* " comp.'s " " " */
say /* [↓] flip the coin 'til a win.*/
do flips=1 until pos(@.CBLF, tosses)\==0 | pos(@.comp, tosses)\==0
tosses= tosses || translate( r(1), 'HT', 10)
end /*flips*/ /* [↑] this is a flipping coin,*/
/* [↓] series of tosses*/
Line 2,831:
_=wins; if _==0 then _='no'
say __ "You've won" _ "game"s(wins) 'out of ' games"."
say; say copies('╩╦', 79 % 2)'╩'; say
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
prompter: oops= __ 'Oops! '; a=
@a_z= 'ABCDEFG-IJKLMNOPQRS+UVWXYZ'
p=__ 'Pick a sequence of' # "coin tosses of H or T (Heads or Tails) or Quit:"
do until ok; say; say p; pull a /*uppercase the answer. */
if abbrev('QUIT', a, 1) then exit 1
a= space( translate(a,,@a_z',./\;:_'), 0)
b= translate(a, 10, 'HT');
ok=
select /*verify the user response. */
when \datatype(b, 'B') then say oops "Illegal response."
when \datatype(a, 'M') then say oops "Illegal characters in response."
when L==0 then say oops "No choice was given."
when L<# then say oops "Not enough coin choices."
when L># then say oops "Too many coin choices."
when a==@.comp then say oops "You can't choose the computer's
otherwise ok= 1
end /*select*/
end /*until ok*/
@.CBLF= a; @.CBLF!=
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
randComp: if @.comp\=='' then return /*the computer already has a pick*/
_= @.CBLF!
if _\=='' then g= left((\substr(_, min(2, #), 1))left(_, 1)substr(_, 3), #)
do until g\==@.CBLF!; g= r(#); end
@.comp= translate(g, 'HT', 10)
return</lang>
<pre>
───────── The computer won first toss, the pick was: HHH
|