'
'mastermind v4.sdlbas 2016-02-23 [B+=MGA] now graphics
'overhaul from "bulls and cows no duplicates.sdlbas copied 2016-02-20 fixed as per Ricks suggestion
'
option qbasic
setDisplay(800,600,32,1)
common curcolr,clr$,lc,gues$[]
const r=rgb(220,0,0)
const g=rgb(0,125,0)
const b=rgb(0,0,255)
const y=rgb(255,255,0)
const o=rgb(255,128,0)
const p=rgb(255,0,200)
const w=rgb(255,255,255)
const gy=rgb(128,128,128)
const cy=rgb(0,255,255)
const deck$="RGBYOP" 'here are 6 color initials Red Green Blue Yellow Orange Purple
DIM ccs[6],ccg[6],gues$[4] 'cc= color counts, so ccs= color counts in secret$, ccg= color counts in guess$
for i=1 to 4 : gues$[i]="" : next 'this might not be needed, nope very much needed!!! comment out and guess button ready on empty guess
randomize(timer)
secret$="" : for i=1 to 4 : secret$=secret$+mid$(deck$,rnd(7),1) : next 'one line to make secret$
for i=1 to 4 'load ccs with cooresponding color counts, how many R, how many G...
for j=1 to 6
if mid$(secret$,i,1)=mid$(deck$,j,1) then : ccs[j]=ccs[j]+1 : end if
next
next '5 lines to count 4 colors in secret$
curcolr=r:clr$="R" 'screen prep and initialization
drawcontrols()
ink(w):text(360,0,30,"Mastermind v4 for SdlBasic")
text(340,40,12,"A secret 4 color combination (repeats allowed) is ready for you to crack.")
text(360,54,12,"Clues B=Bull right color, right spot, C=Cow right color, wrong spot")
guesses = 0 : lc=0
drawframe()
while guesses < 16 'the game begins
if mousebutton()=1 then
mx=mouseX:my=mouseY
if mx>700 and my >200 then 'click in control panel
' if my<250 then 'guess button clicked
if mouseZone(701,201,99,49) then 'guess button clicked
OK=1:guess$=""
for i=1 to 4
if gues$[i]="" then
OK=0
else
guess$=guess$+gues$[i]
end if
next
if OK then 'handle the guess$
guesses += 1
if guess$ = secret$ then 'do we have a winner?
ink(w)
text(375,70,30,"You won after " & guesses & " guesses!")
wait(6000) : end
end if
if guesses = 12 then
ink(w)
text(350,70,30,"You have run out of guesses.")
text(350,100,30,"The correct code was: " + secret$)
wait(5000)
end
end if
'===================counting cattle how many color counts are right
for i=1 to 6:ccg[i]=0:next 'clear old
cattle=0:bulls=0
for i=1 to 4 'load ccg with cooresponding color counts from guess$, how many R, how many G...
for j=1 to 6
if mid$(guess$,i,1)=mid$(deck$,j,1) then : ccg[j]=ccg[j]+1 : end if
next
next
for j=1 to 6 'now compare color counts in secret$ with those in guess$ for total cattle
if ccs[j] and ccg[j] then
if ccg[j]>=ccs[j] then
cattle=cattle+ccs[j]
else
cattle=cattle+ccg[j]
end if
end if
next
for i = 1 to 4 'how many are right on! so much easier to count!!!
if mid$(guess$, i, 1) = mid$(secret$, i, 1) then : bulls += 1 : end if
next
cows=cattle-bulls
ink(w)
if bulls or cows then
text(170,lc*40+9,16,string(bulls,"B")+string(cows,"C"))
else
text(170,lc*40+9,16,"X")
end if
lc=lc+1
drawframe()
for i=1 to 4 : gues$[i]="" : next
end if 'guess OK ends handling guess$
' elseif my<300 then 'clicked a color update in control panel or quit
elseif mouseZone(750,250,50,50) then 'clicked a color update in control panel or quit
curcolr=r:clr$="R":updatecolor()
' elseif my<350 then
elseif mouseZone(750,300,50,50) then
curcolr=g:clr$="G":updatecolor()
' elseif my<400 then
elseif mouseZone(750,350,50,50) then
curcolr=b:clr$="B":updatecolor()
' elseif my<450 then
elseif mouseZone(750,400,50,50) then
curcolr=y:clr$="Y":updatecolor()
' elseif my<500 then
elseif mouseZone(750,450,50,50) then
curcolr=o:clr$="O":updatecolor()
' elseif my<550 then
elseif mouseZone(750,500,50,50) then
curcolr=p:clr$="P":updatecolor()
else
end
end if 'mouse in control box
elseif mx<160 and my>lc*40 and my<lc*40+40 then 'mouse click in the guess boxes just update colors and gues$ array
if mx<40 then
gues$[1]=clr$
ink(curcolr)
' bar(1,lc*20+1,39,lc*20+19)
fillcircle(20,lc*40+20,19)
elseif mx<80 then
gues$[2]=clr$
ink(curcolr)
' bar(41,lc*20+1,79,lc*20+19)
fillcircle(60,lc*40+20,19)
elseif mx<120 then
gues$[3]=clr$
ink(curcolr)
' bar(81,lc*20+1,119,lc*20+19)
fillcircle(100,lc*40+20,19)
else
gues$[4]=clr$
ink(curcolr)
' bar(121,lc*20+1,159,lc*20+19)
fillcircle(140,lc*40+20,19)
end if 'mouse in guess frame
end if ' mouse positions on click
end if 'mousebutton
checkguess()
if key(27) = 1 then: end: end if
wend
wait(80000)
end
sub checkguess()
OK=1
for i=1 to 4
if gues$[i]="" then : OK=0 : end if
next
if OK=1 then
ink(gy)
bar(701,201,799,249)
ink(0)
text(715,215,20,"GUESS")
else
ink(0)
bar(701,201,799,249)
end if
end sub
sub drawframe()
ink(w)
' box(0,lc*20,160,lc*20+20)
' box(40,lc*20,120,lc*20+20)
' line(80,lc*20,80,lc*20+20)
circle(20,lc*40+20,20)
circle(60,lc*40+20,20)
circle(100,lc*40+20,20)
circle(140,lc*40+20,20)
end sub
sub drawcontrols()
ink(0)
bar(700,200,800,250)
ink(0)
text(715,215,20,"GUESS")
ink(r)
bar(750,250,800,300)
ink(g)
bar(750,300,800,350)
ink(b)
bar(750,350,800,400)
ink(y)
bar(750,400,800,450)
ink(o)
bar(750,450,800,500)
ink(p)
bar(750,500,800,550)
ink(gy)
bar(700,550,800,600)
ink(0)
text(725,565,20,"QUIT")
ink(w)
box(700,200,800,250)
box(700,550,800,600)
line(750,250,750,550)
line(800,250,800,550)
for i=300 to 550 step 50
line(750,i,800,i)
next
updatecolor()
end sub
sub updatecolor()
ink(curcolr)
bar(700,251,749,549)
end sub