Pig the dice game/Player: Difference between revisions

m
→‎{{header|REXX}}: optimized some code, added whitespace, used indentations for subroutines/functions, used a cleaner subroutine fence..
m (→‎{{header|REXX}}: optimized some code, added whitespace, used indentations for subroutines/functions, used a cleaner subroutine fence..)
Line 3,038:
 
The (somewhat aggressive) "quarter" strategy was chosen to give the advantage to a human (it was presumed that this dice game would be played with a CBLF).
<lang rexx>/*REXX program plays "pig the dice game" (any #number of CBLFs and/or silicons or HALs).*/
sw= linesize() - 1 /*note:get snakethe eyeswidth isof forthe 2terminal aces.screen,*/
signal on syntax; signal on novalue /*handle REXX program errors. */
sw=linesize()-1 parse arg hp cp win die _ . '(' names ")" /*getobtain theoptional widtharguments offrom the terminal. CL*/
/*names with blanks should use an _ */
parse arg hp cp win die _ . '(' names ")" /*obtain optional arguments.*/
/*names with blanks should use _ */
if _\=='' then call err 'too many arguments were specified: ' _
@nhp = 'number of human players' ; hp = scrutinize( hp, @nhp , 0, 0, 0)
@ncp = 'number of computer players'; cp = scrutinize( cp, @ncp , 0, 0, 2)
@sn2w = 'score needed to win' ; win= scrutinize(win, @sn2w, 1, 1e6, 60)
@nsid = 'number of sides in die' ; die= scrutinize(die, @nsid, 2, 999, 6)
if hp==0 & cp==0 then cp= 2 /*if both counts are zero, 2two HALs. */
if hp==1 & cp==0 then cp= 1 /*if one human, then use 1one HAL. */
name.= /*nullify all names (to a blank). */
L= 0 /*maximum length of a player name. */
do i=1 for hp+cp /*get the player's names, ... maybe. */
if i>hp then @= 'HAL_'i"_the_computer" /*use this for default name. */
else @= 'player_'i /* " " " " " */
name.i = translate( word( strip( word( names, i) ) @, 1), , '_')
L= max(L, length( name.i) ) /*use L for nice name formatting. */
end /*i*/ /*underscores are changed─►blankschanged ──► blanks. */
 
hpn=hp; if hpn==0 then hpn= 'no' /*use normal English for the display. */
cpn=cp; if cpn==0 then cpn=" 'no"' /* " " " " " " */
say 'Pig (the dice game) is being played with:' /*introduction to pig.*/
if cpn\==0 then say right(cpn,9) 'computer player's(cp)
if hpn\==0 then say right(hpn,9) 'human player's(hp)
say 'and the' @sn2w "is: " win ' (or greater).'
!.=; dieNames='ace deuce trey square nickle boxcar' /*die face names.*/
/*note: snake eyes is for 2 aces.*/
!w=0; do i=1 for die; !.i=' ['word(dieNames,i)"]"
!w=max(!w, length(!.i)) /*!w ──► maximum length die name.*/
end /*i*/
s.=0 /*set all player's scores to zero*/
!w=!w+length(die)+3 /*pad the die number and die name*/
@=copies('─',9) /*an eyecatcher (for prompting). */
@jra='just rolled a '; @ati='and the inning' /*nice literals to have.*/
/*──────────────────────────────────────────────────let's play some pig.*/
do game=1; in.=0 /*set each inning's score to zero*/
say; say copies('█',sw) /*display a fence for da eyeballs*/
 
say 'Pig (the dice game) is being played with:' /*the introduction to pig.-the-dice-game*/
do k=1 for hp+cp /*display the scores (as a recap)*/
say 'The score for ' left(name.k,L) " is " right(s.k,length(win))'.'
end /*k*/
 
say copies('█',sw) if cpn\==0 then say right(cpn, 9) /*display'computer a fence for da eyeballs*/player's(cp)
if cpnhpn\==0 then say right(cpnhpn, 9) 'computerhuman player's(cphp)
!.=
say 'and the' @sn2w "is: " win ' (or greater).'
!.dieNames=; dieNames='ace deuce trey square nickle boxcar' /*diesome slangy vernacular facedie─face names.*/
!w=0 /*note: snake eyes is for two aces. */
do i=1 for die /*assign the vernacular die─face names.*/
!.i= ' ['word(dieNames,i)"]" /*pick a word from die─face name lists.*/
!w= max(!w, length(!.i) ) /*!w ──► maximum length diedie─face name. */
end /*i*/
s.= 0 /*set all player's scores to zero. */
!w= !w + length(die) + 3 /*pad the die number and die namenames. */
@= copies('─', 9) /*an eyecatcher (for the prompting text). */
@jra= 'just rolled a '; @ati='and the inning' /*a nice literalsliteral to have laying 'round.*/
@ati= 'and the inning' /*" " " " " " " */
/*═══════════════════════════════════════════════════let's play some pig.*/
do game=1; in.= 0; call score /*set each inning's score to zero0; display*/
 
do j=1 for hp+cp; say /*let each player roll their dice. */
say; say copies('─', sw); /*display a fence for da ole eyeballs. */
it= name.j
say it', your total score (so far) in this pig game is: ' s.j"."
 
do until stopped /*keep prompting/rolling 'til notstopped. */
r= random(1, die) /*get a random die face (number). */
!= left(space(r !.r','), !w) /*for color, use a die-facedie─face name. */
in.j= in.j + r /*add die-facedie─face number to the inning. */
 
if r==1 then do; say it @jra ! || @ati "is a bust."; leave; end
say it @jra ! || @ati "total is: " in.j
 
stopped=what2do(j) /*determine|ask to stop rolling.*/
ifstopped= what2do(j>hp) & stopped then say ' and' name.j "elected /*determine or ask to stop rolling." */
if j>hp & stopped then say ' and' name.j "elected to stop rolling."
end /*until stopped*/
 
if r\==1 then s.j= s.j + in.j /*if not a bust, then add to the inning.*/
if s.j>=win then leave game /*we have a winner, so the game ends. */
end /*j*/ /*that's the end of the players. */
end /*game*/
 
call score; say; say; say; say; say center(''name.j "won! ", sw, '═'); say; say; exit
exit say; say; exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────S subroutine────────────────────────*/
s: if arg(1)==1 then return arg(3); return word(arg(2) 's',1) /*plural?pluralizer.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────SCRUTINIZE subroutine───────────────*/
score: say; say copies('█', sw) /*display a fence for da ole eyeballs. */
scrutinize: parse arg ?,what,min,max /*? is the number, or maybe not. */
do k=1 for hp+cp /*display the scores (as a recap). */
if ?=='' | ?==',' then return arg(5)
say 'The score for ' left(name.k, L) " is " right(s.k, length(win) )'.'
if \datatype(?,'N') then call err what "isn't numeric: " ?; ?=?/1
end /*k*/
if \datatype(?,'W') then call err what "isn't an integer: " ?
say copies('█', sw); return /*display a fence for da ole eyeballs. */
if ?==0 & min>0 then call err what "can't be zero."
/*──────────────────────────────────────────────────────────────────────────────────────*/
if ?<min then call err what "can't be less than" min': ' ?
scrutinize: parse arg ?,what,min,max /*? is the number, ... or maybe not. */
if ?==0 & max>0 then call err what "can't be zero."
if ?=='' | ?==',' then return arg(5)
if ?>max & max\==0 then call err what "can't be greater than" max': ' ?
if \datatype(?, 'N') then call err what "isn't numeric: " ?; ?=?/1
return ?
if \datatype(?, 'W') then call err what "isn't an integer: " ?
/*──────────────────────────────────what2do subroutine──────────────────*/
what2do: parse arg who if ?==0 & min>0 /*"who" is athen humancall orerr awhat computer"can't be zero.*/"
if j>hp?<min & s.j+in.j>=win then call err what return"can't 1be less than" /*an easy choicemin': ' for HAL.*/?
if j>hp & in.j> if ?==win%40 then& max>0 return 1 /*athen call err simplewhat stategy"can't forbe HALzero.*/"
if j>hp if ?>max & max\==0 then call returnerr 0what "can't be greater /*HALthan" says, keepmax': truckin'! */ ?
return ?
say @ name.who', what do you want to do? (a QUIT will stop the game),'
/*──────────────────────────────────────────────────────────────────────────────────────*/
say @ 'press ENTER to roll again, or anything else to STOP rolling.'
what2do: parse arg who /*"who" is a human or a computer.*/
pull action; action=space(action) /*remove any superfluous blanks. */
if j>hp & s.j+in.j>=win then return 1 /*an easy choice for HAL. */
if \abbrev('QUIT',action,1) then return action\==''
if j>hp & in.j>=win%4 then return 1 /*a simple strategy for HAL. */
say; say; say; say center(' quitting. ',sw,'─'); say; say; say; exit
if j>hp then return 0 /*HAL says, keep truckin'! */
/*───────────────────────────────error handling subroutines and others.─*/
say @ name.who', what do you want to do? (a QUIT will stop the game),'
err: say; say; say center(' error! ',max(40,linesize()%2),"*"); say
say @ 'press ENTER to roll again, or anything else to STOP rolling.'
do j=1 for arg(); say arg(j); say; end; say; exit 13
pull action; action=space(action) /*remove any superfluous blanks. */
 
if \abbrev('QUIT', action, 1) then return action\==''
novalue: syntax: call err 'REXX program' condition('C') "error",
say; say; say ,conditioncenter('D quitting. '), sw, 'REXX─'); source statement (line' sigl"):",say; say; exit
/*──────────────────────────────────────────────────────────────────────────────────────*/
,sourceline(sigl)</lang>
say;err: say; say; say center(' quitting.error! ',sw max(40,'─' linesize(); % say;2), say"*"); say; exit say
do j=1 for arg(); say arg(j); say; end; say; exit 13</lang>
This REXX program makes use of &nbsp; '''LINESIZE''' &nbsp; BIF &nbsp; which returns the terminals width (linesize).
<br>Some REXXes don't have a &nbsp; '''LINESIZE''' &nbsp; BIF, so one is included here &nbsp; ──► &nbsp; [[LINESIZE.REX]].