Mastermind: Difference between revisions

m
→‎{{header|REXX}}: moved some code to subroutines.
(→‎{{header|REXX}}: added/changed comments and whitespace, added a check for repeated (the same) guesses (doesn't count as a try).)
m (→‎{{header|REXX}}: moved some code to subroutines.)
Line 312:
call vet rep, 'REP', 0, 1e8 /* " " value if repeats are allowed*/ /*◄■■■■■■ optional vetting.*/
call gen; yourG= 'Your guess must be exactly '
youve= "You've already tried that guess "
do prompt=0 by 0 until xx==wid; say /*play until guessed or QUIT is entered*/
say id 'Please enter a guess with ' wid ' letters [or Quit]:'
pull g; g=space(g,0); L=length(g); if abbrev('QUIT',g,1) then exit 0
if L\==wid then do; say id '***error***' yourG wid " letters."; iterate; end
call dups /*look through the history log for youve= "You've already tried that guess "dups*/
q=?; do hXX=1 for try 0; OO=0; try=try+1 /*look through the history log. initialize some REXX vars; bump TRY.*/
if g\=word(@.h, 8) then iterate /*look for duplicated guesses. */
say; say id youve " (guess number" h').'; iterate prompt
end /*h*/
q=?; XX=0; OO=0; try=try+1 /*initialize some REXX variables. */
 
do j=1 for L; if substr(g,j,1) \== substr(q,j,1) then iterate /*hit? */
Line 327 ⟶ 324:
end /*j*/ /* [↑] XX correct count; scrub guess.*/
 
do k=1 for L; _=substr(g, k, 1); /*process the count for "spots". */
if pos(_, q)==0 then iterate /*is this (spot) letter in the code? */
oo=oo+1; q=translate(q, , _) /*bump the OO spot count. */
end /*k*/ /* [↑] OO spot count; & scrub guess.*/
say
@.try=id right('guess' try, 11) ' ('mxG "is the max):" g '──►' ,
copies('X', xx)copies("O", oo)copies('-', wid-xx-oo)
call end /*hist*/
do hist=1 for try; say @.hist /*show all previous and current guesses*/
end /*hist*/
if try==mxG then do; say; say id "you've used the maximum guesses:" mxG
say; say id "The code was: " ?; say; exit 1
Line 346 ⟶ 342:
say " └─────────────────────────────────────────┘"
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
dups: do h=1 for try; if g\=word(@.h, 8) then iterate /*look forany duplicated guesses. ? */
say; say id youve " (guess number" h').'; iterate prompt; end iterate/*h*/; prompt return
/*──────────────────────────────────────────────────────────────────────────────────────*/
gen: if rep==0 then reps= 'no' /*create a literal for the prompt msg. */
Line 354 ⟶ 353:
do until length(?)==wid /*gen random codes 'til there's enough.*/
r=substr(@abc, random(1, L@abc), 1) /*generate a random letter, 1 at a time*/
if \rep & pos(r, ?)\==0 then iterate /*maybe don't allow a repeated digit. */
?=? || r; if ?=='QUIT'&let==4 then ?= /*append random letter; ··· except this*/
end /*until*/ /* [↑] builds a unique N-letter code.*/
Line 362 ⟶ 361:
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
hist: do hist=1 for try; say @.hist; end; return /*show "guess" history.*/
s: if arg(1)==1 then return ''; return "s" /*a simpler pluraizer. */
ser: say; say; say '***error***' arg(1); say; say; exit 13