Pig the dice game/Player: Difference between revisions

→‎{{header|REXX}}: included output from the REXX program.
(→‎{{header|REXX}}: included output from the REXX program.)
Line 1,758:
 
=={{header|REXX}}==
{{output?|REXX}}
The strategy for a computer player is to roll again if the total score (including the current inning) has not won, and to roll again if the inning score is less than a quarter of the score needed to win.
 
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) with(any at# leastof oneCBLFs humanand/or playersilicons).*/
signal on syntax; signal on novalue /*handle REXX program errors. */
sw=80linesize()-1 /*get the LINESIZEwidth bifof wouldthe beterminal. nicer*/
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, 02)
@sn2w = 'score needed to win' ; win = scrutinize(win, @sn2w, 1,11e6,10060)
@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, 2 HALs*/
if hp==1 & cp==0 then cp=1 /*if one human, then use 1 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. */
Line 1,779 ⟶ 1,780:
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 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 duecedeuce trey square nickle boxcar' /*die face names.*/
/*note: snake eyes is for 2 aces.*/
!w=0; do i=1 for 6die; !.i=' ['word(dieNames,i)"] "; end /*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*/
 
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) /*display a fence for da eyeballs*/
Line 1,805 ⟶ 1,809:
do j=1 for hp+cp /*let each player roll their dice*/
say; say copies('─',sw); /*display a fence for da eyeballs*/
it=name.j
it=word('You It', 1 + (j>hp)) /*pronoun choice: You or It */
say name.jit', your total score (so far) in this pig game is: ' s.j"."
 
do until stopped stopped /*keep prompting/rolling 'til not*/
r=random(1,die); !=space(r !.r) /*for color, use /*get a random die- face name(number).*/
!=left(space(r !.r','), !w) /*for color, use a die-face name.*/
in.j=in.j+r
in.j=in.j+r /*add die-face number to 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.*/
if j>hp & stopped then say ' and' name.j "elected to stop rolling."
Line 1,827 ⟶ 1,832:
s: if arg(1)==1 then return arg(3); return word(arg(2) 's',1) /*plural?*/
/*──────────────────────────────────SCRUTINIZE subroutine───────────────*/
scrutinize: parse arg ?,what,minimummin,max /*? is the number, or maybe not. */
if ?=='' | ?==',' then return arg(45)
if \datatype(?,'N') then call err what "isn't numeric: " ?; ?=?/1
if \datatype(?,'W') then call err what "isn't an integer: " ?
if ?==0 & minimummin>0 then call err what "can't be zero."
if ?<minimummin then call err what "can't be less than" minimummin': ' ?
if ?==0 & max>0 then call err what "can't be zero."
if ?>max & max\==0 then call err what "can't be greater than" max': ' ?
return ?
/*──────────────────────────────────what2do subroutine──────────────────*/
what2do: parse arg who /*"who" is a human or a computer.*/
if (j>hp & rs.j+in.j>=win) then return 1 /*an easy choice for HAL.*/
if (j>hp & in.j>=win%4) then return 1 /*a simple stategy for HAL.*/
if j>hp then return 0 /*HAL says, keep truckin'! */
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.'
Line 1,845 ⟶ 1,852:
say; say; say; say center(' quitting. ',sw,'─'); say; say; say; exit
/*───────────────────────────────error handling subroutines and others.─*/
err: say; say; say center(' error! ',max(40,linesize()%2),"*"); say
do j=1 for arg(); say arg(j); say; end; say; exit 13
 
novalue: syntax: call err 'REXX program' condition('C') "error",
,condition('D'),'REXX source statement (line' sigl"):",
,sourceline(sigl)</lang>
<br><br>The &nbsp; '''$T''' &nbsp; REXX program makes use of &nbsp; '''LINESIZE''' &nbsp; BIF &nbsp; which returns the terminals width (linesize).
<br>Some REXXes doen't have a &nbsp; '''LINESIZE''' &nbsp; BIF, so one is included here &nbsp; ──► &nbsp; [[LINESIZE.REX]].
<br><br>
 
novalue: syntax: call err 'REXX program' condition('C') "error",,
condition('D'),'REXX source statement (line' sigl"):",,
sourceline(sigl)</lang>
To play this game with two computer players (simulate), use the following arguments:
<pre>
Line 1,860 ⟶ 1,871:
</pre>
to specify names for the (two) computer players.
<br><br>
 
'''output''':
<pre>
Pig (the dice game) is being played with:
2 computer players
no human players
and the score needed to win is: 60 (or greater).
 
███████████████████████████████████████████████████████████████████████████████
The score for HAL 1 the computer is 0.
The score for HAL 2 the computer is 0.
███████████████████████████████████████████████████████████████████████████████
 
───────────────────────────────────────────────────────────────────────────────
HAL 1 the computer, your total score (so far) in this pig game is: 0.
HAL 1 the computer just rolled a 2 [deuce], and the inning total is: 2
HAL 1 the computer just rolled a 5 [nickle], and the inning total is: 7
HAL 1 the computer just rolled a 2 [deuce], and the inning total is: 9
HAL 1 the computer just rolled a 1 [ace], and the inning is a bust.
 
───────────────────────────────────────────────────────────────────────────────
HAL 2 the computer, your total score (so far) in this pig game is: 0.
HAL 2 the computer just rolled a 6 [boxcar], and the inning total is: 6
HAL 2 the computer just rolled a 4 [square], and the inning total is: 10
HAL 2 the computer just rolled a 4 [square], and the inning total is: 14
HAL 2 the computer just rolled a 5 [nickle], and the inning total is: 19
and HAL 2 the computer elected to stop rolling.
 
███████████████████████████████████████████████████████████████████████████████
The score for HAL 1 the computer is 0.
The score for HAL 2 the computer is 19.
███████████████████████████████████████████████████████████████████████████████
 
───────────────────────────────────────────────────────────────────────────────
HAL 1 the computer, your total score (so far) in this pig game is: 0.
HAL 1 the computer just rolled a 1 [ace], and the inning is a bust.
 
───────────────────────────────────────────────────────────────────────────────
HAL 2 the computer, your total score (so far) in this pig game is: 19.
HAL 2 the computer just rolled a 1 [ace], and the inning is a bust.
 
███████████████████████████████████████████████████████████████████████████████
The score for HAL 1 the computer is 0.
The score for HAL 2 the computer is 19.
███████████████████████████████████████████████████████████████████████████████
 
───────────────────────────────────────────────────────────────────────────────
HAL 1 the computer, your total score (so far) in this pig game is: 0.
HAL 1 the computer just rolled a 6 [boxcar], and the inning total is: 6
HAL 1 the computer just rolled a 6 [boxcar], and the inning total is: 12
HAL 1 the computer just rolled a 6 [boxcar], and the inning total is: 18
and HAL 1 the computer elected to stop rolling.
 
───────────────────────────────────────────────────────────────────────────────
HAL 2 the computer, your total score (so far) in this pig game is: 19.
HAL 2 the computer just rolled a 6 [boxcar], and the inning total is: 6
HAL 2 the computer just rolled a 3 [trey], and the inning total is: 9
HAL 2 the computer just rolled a 6 [boxcar], and the inning total is: 15
and HAL 2 the computer elected to stop rolling.
 
███████████████████████████████████████████████████████████████████████████████
The score for HAL 1 the computer is 18.
The score for HAL 2 the computer is 34.
███████████████████████████████████████████████████████████████████████████████
 
───────────────────────────────────────────────────────────────────────────────
HAL 1 the computer, your total score (so far) in this pig game is: 18.
HAL 1 the computer just rolled a 5 [nickle], and the inning total is: 5
HAL 1 the computer just rolled a 1 [ace], and the inning is a bust.
 
───────────────────────────────────────────────────────────────────────────────
HAL 2 the computer, your total score (so far) in this pig game is: 34.
HAL 2 the computer just rolled a 4 [square], and the inning total is: 4
HAL 2 the computer just rolled a 4 [square], and the inning total is: 8
HAL 2 the computer just rolled a 3 [trey], and the inning total is: 11
HAL 2 the computer just rolled a 5 [nickle], and the inning total is: 16
and HAL 2 the computer elected to stop rolling.
 
███████████████████████████████████████████████████████████████████████████████
The score for HAL 1 the computer is 18.
The score for HAL 2 the computer is 50.
███████████████████████████████████████████████████████████████████████████████
 
───────────────────────────────────────────────────────────────────────────────
HAL 1 the computer, your total score (so far) in this pig game is: 18.
HAL 1 the computer just rolled a 4 [square], and the inning total is: 4
HAL 1 the computer just rolled a 1 [ace], and the inning is a bust.
 
───────────────────────────────────────────────────────────────────────────────
HAL 2 the computer, your total score (so far) in this pig game is: 50.
HAL 2 the computer just rolled a 1 [ace], and the inning is a bust.
 
███████████████████████████████████████████████████████████████████████████████
The score for HAL 1 the computer is 18.
The score for HAL 2 the computer is 50.
███████████████████████████████████████████████████████████████████████████████
 
───────────────────────────────────────────────────────────────────────────────
HAL 1 the computer, your total score (so far) in this pig game is: 18.
HAL 1 the computer just rolled a 6 [boxcar], and the inning total is: 6
HAL 1 the computer just rolled a 6 [boxcar], and the inning total is: 12
HAL 1 the computer just rolled a 4 [square], and the inning total is: 16
and HAL 1 the computer elected to stop rolling.
 
───────────────────────────────────────────────────────────────────────────────
HAL 2 the computer, your total score (so far) in this pig game is: 50.
HAL 2 the computer just rolled a 6 [boxcar], and the inning total is: 6
HAL 2 the computer just rolled a 3 [trey], and the inning total is: 9
HAL 2 the computer just rolled a 5 [nickle], and the inning total is: 14
and HAL 2 the computer elected to stop rolling.
 
 
 
 
═══════════════════════════HAL 2 the computer won! ════════════════════════════
</pre>
 
=={{header|Tcl}}==