Rock-paper-scissors: Difference between revisions

m
→‎{{header|REXX}}: eliminated the need for a statement.
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:
::* keeps track of the human player's responses   (to hopefully make computer winning choices)
::* uses proper "English"   ''rock breaks scissors''
<lang rexx>/*REXX pgm plays rock-paper-scissorsrock─paper─scissors with a CBLF: (carbon-basedcarbon─based life form*/
!= '────────'; er='***error!***'; @.=0 /*some constants for pgm constants.*/
zprompt= ! 'Please enter one of: Rock Paper Scissors (or Quit)'
$.p='paper'; $.s='scissors'; $.r='rock' /*computer's choices. */
b.p=' covers '; b.s=' cuts '; b.r=' breaks ' /*how the choice wins.*/
 
do forever; say; say zprompt; say /*prompt the CBLF &, get response.*/
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.*/
if @.p==m then f='scissors' /*emulate JC's The Amazing Karnac*/
if @.s==m then f='rock' /* " " " " " */
if m\==0 then c=f /*choose based on CBLF's history.*/
c1=left(c,1); upper c1 /*C1 is used for fast comparing.*/
parse pull u; a=strip(u); /*get the CBLF's choice (answer).*/
upper a; c1 ; a1=left(a,1) /*uppercase answerchoices, get 1st char.*/
ok=0 /*indicate answer isn't OK so far*/
select /*process the CBLF's choice. */
when words(u)==0 then say er 'nothing entered'
when words(u)>1 then say er 'too many choices: ' u
when abbrev('QUIT',a) a) then do; say ! 'quitting.'; exit; end
when abbrev('ROCK', a) |,
abbrev('PAPER', a) |,
abbrev('SCISSORS',a) then ok=1 /*a valid answer by CBLF.*/
otherwise say er 'you entered a bad choice:' u
Line 3,446:
 
if \ok then iterate /*answer ¬ OK? Then get another.*/
@.a1 = @.a1+1 /*keep track of CBLF's answers. */
say ! 'computer chose: ' c
if a1==c1 then do; say ! 'draw.'; iterate; end
if a1=='R' & c1=='S' |,
a1=='S' & c1=='P' |,
Line 3,455:
end /*forever*/
/*stick a fork in it, we're done.*/</lang>
Programming note: &nbsp; 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 &nbsp; (output shown is a screen scraping):
<pre>