Anonymous user
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
q=?;
if g\=word(@.h, 8) then iterate /*look for duplicated guesses. */▼
say; say id youve " (guess number" h').'; iterate prompt▼
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)
if pos(_, q)==0 then iterate /*is this (spot) letter in the code? */
oo=oo+1; q=translate(q, , _)
end /*k*/ /* [↑] OO spot count; & scrub guess.*/
say
@.try=id right('guess' try, 11) ' ('mxG "is the max):" g '──►' ,
▲ 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. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
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
|