Anonymous user
Pig the dice game/Player: Difference between revisions
→{{header|REXX}}: included output from the REXX program.
(→{{header|REXX}}: Output?) |
(→{{header|REXX}}: included output from the REXX program.) |
||
Line 1,758:
=={{header|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
signal on syntax; signal on novalue /*handle REXX program errors. */
sw=
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
@ncp = 'number of computer players'; cp
@sn2w = 'score needed to win' ; win
@nsid = 'number of sides in die' ; die
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
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
/*note: snake eyes is for 2 aces.*/
!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='
/*──────────────────────────────────────────────────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 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
say
do until
r=random(1,die)
!=left(space(r !.r','), !w) /*for color, use a die-face name.*/
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,
if ?=='' | ?==',' then return arg(
if \datatype(?,'N') then call err what "isn't numeric: " ?; ?=?/1
if \datatype(?,'W') then call err what "isn't an integer: " ?
if ?==0 &
if ?<
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
if
if
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 '''$T''' REXX program makes use of '''LINESIZE''' BIF which returns the terminals width (linesize).
<br>Some REXXes doen't have a '''LINESIZE''' BIF, so one is included here ──► [[LINESIZE.REX]].
<br><br>
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}}==
|