'Mine Sweeper v4.sdlbas by MGA/B+ thank's to jalih code example on BP.org found a simple recursive way
option qbasic
randomize
common arrd=10 ' for square (arrd X arrd) array pick a size you want 3x3 to 25x25, arrd= square array dimension
common board[],m[],toggle[]
common xmax=680
common ymax=680 'adjust for your screen
common th=24 'text height
common tly=2*th ' offset Top Left corner Y
common sq=int((ymax-4*th)/arrd)
common tlx=int((xmax-arrd*sq)/2) ' offset Top Left corner X
common textoff=(sq-th)/2
common restart=1
common mines
setdisplay(xmax,ymax,32,1)
autoback(0)
'===========================================================main
ink(rgb(0,200,200))
title="Mine Sweeper v4: <esc> to quit"
text((xmax-len(title)*9)/2,0,18, title)
title="MGA/B+ 2015-08-26"
text((xmax-len(title)*9)/2,th,18, title)
while key(27)=0 '==============================start a game
if restart then
initialize()
end if
mb=mousebutton
dx=bx(mousex)
dy=by(mousey)
if mb=1 then
if dx<>0 and dy<>0 then
if m[dx,dy]=1 then 'hit a mine blow up then show board and mine positions
ink(0)
bar(tlx-1,tly-1,tlx+arrd*sq+1,tly+arrd*sq+1)
ink(rgb(255,255,255))
text( xmax/2-5*th,ymax/2-3*th,2*th,"KA-BOOM!")
screenswap
wait(2000)
ink(0)
bar(tlx-1,tly-1,tlx+arrd*sq+1,tly+arrd*sq+1)
for x=1 to arrd
for y=1 to arrd
if m[x,y]=1 then
ink(rgb(216,0,0))
bar(tlx+sq*(x-1),tly+sq*(y-1), tlx+sq*(x-1)+sq-2,tly+sq*(y-1)+sq-2)
elseif toggle[x,y]=-1 then
show(x,y) 'here just show cell
else
ink(rgb(0,190+rnd(64),0))
bar(tlx+sq*(x-1),tly+sq*(y-1), tlx+sq*(x-1)+sq-2,tly+sq*(y-1)+sq-2)
end if
next
next
screenswap
wait(5000)
restart=1
else 'normal reveal left click
if board[dx,dy]=0 then: sweepzeros(dx,dy) : else: show(dx,dy) : end if
screenswap
wait(200)
TFwin()
end if
end if
elseif mb=3 then 'right mouse click toggle cell on or off to mark mine (red)
if dx<>0 and dy<>0 then
if toggle[dx,dy]=0 then
toggle[dx,dy]=1
ink(rgb(216,0,0))
bar(tlx+sq*(dx-1),tly+sq*(dy-1), tlx+sq*(dx-1)+sq-2,tly+sq*(dy-1)+sq-2)
elseif toggle[dx,dy]=1 then
toggle[dx,dy]=0
ink(rgb(0,190+rnd(64),0))
bar(tlx+sq*(dx-1),tly+sq*(dy-1), tlx+sq*(dx-1)+sq-2,tly+sq*(dy-1)+sq-2)
end if
screenswap
wait(200)
end if
end if
wend
'=======================================ending after esc pressed
ink(rgb(0,200,200))
s1="Thanks for playing!"
text((xmax-len(s1)*9)/2,2*th+sq*arrd,18, s1)
screenswap
wait(500)
end
'================================= procedures
sub initialize()
restart=0
mines=int(.15*arrd^2) 'determine your own level of difficulty 15 percent is nice amount of mines
'mines=1 'for testing recursive sweeps
minesplaced=0
dim m[0 to arrd+1,0 to arrd+1]
while minesplaced<mines
rx=rnd(arrd+1)
ry=rnd(arrd+1)
if m[rx,ry]=0 then
m[rx,ry]=1
minesplaced=minesplaced+1
end if
wend
dim board[1 to arrd,1 to arrd]
' count mines amoung the neighbors
for y=1 TO arrd
for x=1 TO arrd
if m[x,y] <>1 then
board[x,y]=(m[x-1,y-1]+m[x-1,y]+m[x-1,y+1]+m[x,y-1]+m[x,y+1]+m[x+1,y-1]+m[x+1,y]+m[x+1,y+1])
else
board[x,y]=-1
end if
next
next
'tracking what has been shown and how -1=left clicked, 0/1= not/marked red for mine
dim toggle[1 to arrd,1 to arrd]
' show covered board
ink(0)
bar(tlx-1,tly-1,tlx+arrd*sq+1,tly+arrd*sq+1)
for x=1 to arrd
for y=1 to arrd
ink(rgb(0,190+rnd(64),0))
bar(tlx+sq*(x-1),tly+sq*(y-1), tlx+sq*(x-1)+sq-2,tly+sq*(y-1)+sq-2)
next
next
screenswap
end sub
function bx(mx) 'convert mouse x to array x
rtn=int((mx-tlx)/sq +1)
if rtn<arrd+1 and rtn>0 then
bx=rtn
else
bx=0
end if
end function
function by(my) 'convert mouse y to array y
rtn=int((my-tly)/sq +1)
if rtn<arrd+1 and rtn>0 then
by=rtn
else
by=0
end if
end function
sub TFwin()
tc=0
for x=1 to arrd
for y=1 to arrd
if toggle[x,y]=-1 then
tc=tc+1
end if
next
next
if tc=arrd*arrd-mines then
ink(0)
bar(tlx-1,tly-1,tlx+arrd*sq+1,tly+arrd*sq+1)
ink(rgb(255,255,255))
text( xmax/2-5*th,ymax/2-3*th,2*th,"ALL CLEAR!")
screenswap
wait(5000)
restart=1
end if
end sub
sub show(x,y)
toggle[x,y]=-1
ink(rgb(255,255,255))
bar(tlx+sq*(x-1),tly+sq*(y-1), tlx+sq*(x-1)+sq-2,tly+sq*(y-1)+sq-2)
ink(0)
text(tlx+sq*(x-1)+textoff,tly+sq*(y-1)+textoff,18,str(board[x,y]))
end sub
sub sweepzeros(x,y) 'this was a tricky routine for recursive sweep
for row = y-1 TO y+1
if row>=1 and row<=arrd then
for col = x-1 TO x+1
if col >=1 and col<=arrd then
if toggle[col,row]>-1 then
t = board[col,row]
if t = 0 then
show(col,row)
sweepzeros(col,row)
elseif t >= 1 AND t <= 8 then
show(col,row)
end if
end if
end if
next
end if
next
end sub