Rock-paper-scissors: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: changed comments in the REXX section header and program note (output section).) |
m (→{{header|REXX}}: eliminated the need for a statement.) |
||
Line 3,419: | Line 3,419: | ||
::* keeps track of the human player's responses (to hopefully make computer winning choices) |
::* keeps track of the human player's responses (to hopefully make computer winning choices) |
||
::* uses proper "English" ''rock breaks scissors'' |
::* uses proper "English" ''rock breaks scissors'' |
||
<lang rexx>/*REXX pgm plays |
<lang rexx>/*REXX pgm plays rock─paper─scissors with a CBLF: carbon─based life form*/ |
||
!='────────'; |
!= '────────'; er='***error!***'; @.=0 /*some pgm constants.*/ |
||
prompt= ! 'Please enter one of: Rock Paper Scissors (or Quit)' |
|||
$.p='paper'; $.s='scissors'; $.r='rock' /*computer's choices. |
$.p='paper'; $.s='scissors'; $.r='rock' /*computer's choices.*/ |
||
b.p=' covers '; b.s=' cuts '; b.r=' breaks ' /*how the choice wins |
b.p=' covers '; b.s=' cuts '; b.r=' breaks ' /*how the choice wins*/ |
||
do forever; |
do forever; say; say prompt; say /*prompt CBLF, get response*/ |
||
c=word('rock paper scissors',random(1,3)) |
c=word('rock paper scissors',random(1,3)) /*the computer's 1st pick. */ |
||
m=max(@.r,@.p,@.s); f='paper' /*prepare to examine the history.*/ |
m=max(@.r,@.p,@.s); f='paper' /*prepare to examine the history.*/ |
||
if @.p==m then f='scissors' /*emulate JC's The Amazing Karnac*/ |
if @.p==m then f='scissors' /*emulate JC's The Amazing Karnac*/ |
||
if @.s==m then f='rock' /* " " " " " */ |
if @.s==m then f='rock' /* " " " " " */ |
||
if m\==0 then c=f /*choose based on CBLF's history.*/ |
if m\==0 then c=f /*choose based on CBLF's history.*/ |
||
c1=left(c,1) |
c1=left(c,1) /*C1 is used for fast comparing.*/ |
||
parse pull u; a=strip(u) |
parse pull u; a=strip(u) /*get the CBLF's choice (answer).*/ |
||
upper a |
upper a c1 ; a1=left(a,1) /*uppercase choices, get 1st char*/ |
||
ok=0 /*indicate answer isn't OK so far*/ |
ok=0 /*indicate answer isn't OK so far*/ |
||
select /*process the CBLF's choice. */ |
select /*process the CBLF's choice. */ |
||
when words(u)==0 then say er 'nothing entered' |
when words(u)==0 then say er 'nothing entered' |
||
when words(u)>1 then say er 'too many choices: ' u |
when words(u)>1 then say er 'too many choices: ' u |
||
when abbrev('QUIT', |
when abbrev('QUIT', a) then do; say ! 'quitting.'; exit; end |
||
when abbrev('ROCK',a) |
when abbrev('ROCK', a) |, |
||
abbrev('PAPER',a) |, |
abbrev('PAPER', a) |, |
||
abbrev('SCISSORS',a) then ok=1 /*a valid answer by CBLF.*/ |
abbrev('SCISSORS',a) then ok=1 /*a valid answer by CBLF.*/ |
||
otherwise say er 'you entered a bad choice:' u |
otherwise say er 'you entered a bad choice:' u |
||
Line 3,446: | Line 3,446: | ||
if \ok then iterate /*answer ¬ OK? Then get another.*/ |
if \ok then iterate /*answer ¬ OK? Then get another.*/ |
||
@.a1 |
@.a1=@.a1+1 /*keep track of CBLF's answers. */ |
||
say ! 'computer chose: ' c |
say ! 'computer chose: ' c |
||
if a1==c1 |
if a1==c1 then do; say ! 'draw.'; iterate; end |
||
if a1=='R' & c1=='S' |, |
if a1=='R' & c1=='S' |, |
||
a1=='S' & c1=='P' |, |
a1=='S' & c1=='P' |, |
||
Line 3,455: | Line 3,455: | ||
end /*forever*/ |
end /*forever*/ |
||
/*stick a fork in it, we're done.*/</lang> |
/*stick a fork in it, we're done.*/</lang> |
||
Programming note: the REXX program (two) statements on line 15: |
|||
<lang> upper a; a1=left(a,1) /*uppercase answer, get 1st char.*/</lang> |
|||
could be replaced with a more compact (and faster and obtuse and less idiomatic) version: |
|||
<lang>parse upper var a a 1 a1 2</lang><br> |
|||
'''output''' with various responses from the user (output shown is a screen scraping): |
'''output''' with various responses from the user (output shown is a screen scraping): |
||
<pre> |
<pre> |