Rock-paper-scissors: Difference between revisions

Content deleted Content added
m →‎{{header|REXX}}: added/changed whitespace and comments, changed error output(s).
Line 3,453:
===traditional, 3 choices===
This REXX program version:
::*   allows the human player to abbreviate their choice
::*   issues appropriate error messages for an incorrect (or no) choice(s)
::*   allows the human player to   QUIT   at any time
::*   keeps track of the human player's responses   (to hopefully make computer winning choices)
::*   uses proper "English"   ''rock breaks scissors''
<lang rexx>/*REXX pgmprogram plays rock─paper─scissors with a CBLF: carbon─based life form.*/
!= '────────'; err=! '***error!***'; @.=0 /*some pgmprogram constants. */
prompt=! 'Please enter one of: Rock Paper Scissors (or Quit)'
$.p='paper' ; $.s='scissors'; $.r='rock' /*list of computer's choices. */
t.p=$.r ; t.s=$.p ; t.r=$.s /*thingys that beats stuff. */
w.p=$.s ; w.s=$.r ; w.r=$.p /*stuff beats " " thingys.*/
b.p='covers'; b.s='cuts' ; b.r='breaks' /*verbs: how the choice wins.*/
 
do forever; say; say prompt; say /*prompt the CBLF,; then get a response.*/
c=word($.p $.s $.r, random(1, 3)) /*choose the computer's 1stfirst pick. */
m=max(@.r, @.p, @.s); c=w.r /*prepare to examine the choice history.*/
if @.p==m then c=w.p /*emulate JC's: The Amazing Karnac. */
if @.s==m then c=w.s /* " " " " " */
c1=left(c,1) /*C1 is used for fastfaster comparing. */
parse pull u; a=strip(u) /*get the CBLF's choice/pick (answer). */
upper a c1 ; a1=left(a,1) /*uppercase choices, get 1st charcharacter.*/
ok=0 /*indicate answer isn't OK (so far). */
select /*process/verify the CBLF's choice. */
when words(u)==0 then say err 'nothing entered'
when words(u)>1 then say err 'too many choices: ' u
when abbrev('QUIT', a) then do; say ! 'quitting.'; exit; end
when abbrev('ROCK', a) |,
abbrev('PAPER', a) |,
abbrev('SCISSORS',a) then ok=1 /*aYes? A valid answer by CBLF.*/
otherwise say err 'you entered a bad choice: ' u
end /*select*/
 
if \ok then iterate /*answer ¬ OK? Then get another choice.*/
@.a1=@.a1+1 /*keep tracka history of the CBLF's answerschoices. */
say ! 'computer chose: ' c
if a1==c1 then do; say ! 'draw.'; iterate; end /*it's a draw. */
if $.a1==t.c1 then say ! 'the computer wins. ' ! $.c1 b.c1 $.a1
else say ! 'you win! ' ! $.a1 b.a1 $.c1
end /*forever*/
/*stick a fork in it, we're all done. */</lang>
'''output''' with various responses from the user &nbsp; (output shown is a screen scraping):
<pre>
Line 3,538:
 
===extended, 5 choices===
This REXX program version supports more choices: &nbsp; rock paper scissors lizard Spock
<lang rexx>/*REXX program plays rock─paper─scissors with a CBLF: carbon─based life form.*/
<lang rexx>prompt=! 'Please enter one of: Rock Paper SCissors Lizard SPock (Vulcan) (or Quit)'
!= '────────'; err=! '***error!***'; @.=0 /*some program constants.*/
$.p='paper' ; $.s='scissors' ; $.r='rock' ; $.l='lizard' ; $.v='Spock' /*names of the thingys*/
<lang rexx>prompt=! 'Please enter one of: Rock Paper SCissors Lizard SPock (Vulcan) (or Quit)'
t.p= $.r $.v ; t.s= $.p $.l ; t.r= $.s $.l ; t.l= $.p $.v ; t.v= $.r $.s /*thingys beats stuff.*/
w$.p='paper' $.l $.s ; w$.s= $.v $.r 'scissors' ; w$.r='rock' $.v $.p ; w$.lL= $.r $.s'lizard' ; w$.v='Spock' $.l $.p /*stuffnames beatsof the thingys.*/
bt.p='covers disproves'$.r $.v ; bt.s='cuts decapitates'$.p $.L ; bt.r='breaks crushes'$.s $.L ; bt.lL='eats poisons'$.p $.v ; bt.v='vaporizes smashes'$.r $.s /*how thethingys choicebeats winsstuff.*/
$w.p='paper' $.L $.s ; $w.s='scissors' $.v $.r ; $w.r='rock' $.v $.p ; $w.lL='lizard' $.r $.s ; $w.v='Spock' $.L $.p /*namesstuff of thebeats thingys.*/
whom.1=! 'the computer wins. ' !; whom.2=! 'you win! ' !; win=words(t.p)
b.p='covers disproves'; b.s='cuts decapitates'; b.r='breaks crushes'; b.L='eats poisons'; b.v='vaporizes smashes' /*how the choice wins.*/
whom.1=! 'the computer wins. ' !; whom.2=! 'you win! ' !; win=words(t.p)
 
do forever; say; say prompt; say /*prompt CBLF; &then get a response.*/
c=word($.p $.s $.r $.lL $.v',random(1,5)) /*the computer's first choice/pick. */
m=max(@.r,@.p,@.s,$@.lL,$@.v) /*prepareused toin examineexamining CBLF's history.*/
if @.p==m then c=word(w.p,random(1,2)) /*emulate JC's The Amazing Karnac.*/
if @.s==m then c=word(w.s,random(1,2)) /* " " " " " */
if @.r==m then c=word(w.r,random(1,2)) /* " " " " " */
if @.lL==m then c=word(w.lL,random(1,2)) /* " " " " " */
if @.v==m then c=word(w.v,random(1,2)) /* " " " " " */
c1=left(c,1) /*C1 is used for fastfaster comparing. */
parse pull u; a=strip(u) /*getobtain the CBLF's choice (answer)/pick. */
upper a c1 ; a1=left(a,1) /*uppercase the choices, get 1st char. */
ok=0 /*indicate answer isn't OK (so far). */
select /* [↓] process the CBLF's choice/pick. */
when words(u)==0 then say err 'nothing entered.'
when words(u)>1 then say err 'too many choices: ' u
when abbrev('QUIT', a) then do; say ! 'quitting.'; exit; end
when abbrev('LIZARD', a) |,
abbrev('ROCK', a) |,
Line 3,567 ⟶ 3,569:
abbrev('Vulcan', a) |,
abbrev('SPOCK', a,2) | ,
abbrev('SCISSORS',a,2) then ok=1 /*it's a valid CBLF answerchoice.*/
otherwise say err 'you entered a bad choice: ' u
end /*select*/
 
if \ok then iterate then iterate /*answer ¬ OK? Then get another choice.*/
@.a1=@.a1+1 /*keep tracka history of the CBLF's answerschoices. */
say ! 'computer chose: ' c
if a1==c1 then do; say ! 'draw.'; iterate; end /*O rats! Computer and CBLF got a draw*/
else do who=1 for 2 /*either the computer or the CBLF won. */
 
if who==2 then parse value a1 c1 with do who=1 for 2 /*either computer |c1 CBLF*/a1
do j=1 for win if who==2 then parse value a1 c1 with c1 a1 /*see who won. */
if $.a1\==word(t.c1,j) then iterate /*not this 'un. do j=1 for win*/
say ifwhom.who $.a1\==c1 word(tb.c1,j) $.a1 then iterate /*notify winner.*/
leave say whom.who $.c1 word(b.c1,j) $ /*leave J loop.a1*/
end leave/*j*/
end end /*jwho*/
end /*who*/
end /*forever*/
/*stick a fork in it, we're all done. */</lang>
 
=={{header|Ruby}}==