RosettaCodeData/Task/Mastermind/M2000-Interpreter/mastermind.m2000

89 lines
1.8 KiB
Plaintext

Module MasterMind {
cls #225522, 0
GetInput=lambda (s as string, min as integer, max as integer) -> {
do
print s+"("+min+" - "+max+")";
Input ":",v%
until v%>=min and v%<=max
=v%
}
GetInputGuess=lambda (moves as integer, length as integer, max as integer, once as boolean) -> {
string k, s
integer j, oldi
boolean check[max]=false
pen 15 {print str$(moves,"00")+". ";}
for i=1 to length
do
do
if oldi=i then beep
k=ucase$(key$)
j = asc(k)-64
oldi=i
when j<1 or j>max
when once and check[j]
pen #99ffbb {print k;}
if once then check[j]=true
s+=k
next
=s
}
DisplayFinal=lambda (a) -> {
string s
for i=1 to len(a)-1
s+=chr$(64+a[i])
next
=s
}
pen 15
integer C=GetInput("number of colors", 2, 20)
integer CL=GetInput("code length", 4, 10)
integer mn=GetInput("maximum number of guesses", 7, 20)
if c>=CL then
boolean rc=GetInput("colors may be repeated in the code", 0, 1)
else
boolean rc=true
end if
pen #00ff00
Cls ,0
integer acc[CL], i
do
if rc then
for i=1 to cl: acc[i]=random(1, C): next
else
boolean color[C]
for i=1 to cl
do
acc[i]=random(1, C)
when color[acc[i]]
color[acc[i]]=true
next
end if
string target=DisplayFinal(acc), z, mark
integer moves=1
pen 15 {Print "Guess", @(20),"Mark"}
for i=1 to mn
z=GetInputGuess(i, CL, C, Not RC)
if target=z then exit for
mark=""
for j=1 to CL
if acc[j]=asc(mid$(z, j, 1))-64 then
mark+="X"
else.if instr(target, mid$(z, j, 1))>0 then
mark+="O"
else
mark+="-"
end if
next
Print @(20), mark
next
print
if target=z then
pen 15 {print "Guess Done"}
else
print "You loose, secret code was:"+target
end if
print "Play again ? (Y - N)"
Until ucase$(key$)<>"Y"
}
MasterMind