110 lines
2.6 KiB
Text
110 lines
2.6 KiB
Text
N = 6 : M = 5 : H = 25 : P = 0.2
|
|
|
|
fastgraphics
|
|
graphsize N*H,(M+1)*H
|
|
font "Arial",H/2+1,75
|
|
dim f(N,M) # 1 open, 2 mine, 4 expected mine
|
|
dim s(N,M) # count of mines in a neighborhood
|
|
|
|
trian1 = {1,1,H-1,1,H-1,H-1} : trian2 = {1,1,1,H-1,H-1,H-1}
|
|
mine = {2,2, H/2,H/2-2, H-2,2, H/2+2,H/2, H-2,H-2, H/2,H/2+2, 2,H-2, H/2-2,H/2}
|
|
flag = {H/2-1,3, H/2+1,3, H-4,H/5, H/2+1,H*2/5, H/2+1,H*0.9-2, H*0.8,H-2, H*0.2,H-2, H/2-1,H*0.9-2}
|
|
|
|
mines = int(N*M*P) : k = mines : act = 0
|
|
while k>0
|
|
i = int(rand*N) : j = int(rand*M)
|
|
if not f[i,j] then
|
|
f[i,j] = 2 : k = k - 1 # set mine
|
|
s[i,j] = s[i,j] + 1 : gosub adj # count it
|
|
|
|
end if
|
|
end while
|
|
togo = M*N-mines : over = 0 : act = 1
|
|
|
|
gosub redraw
|
|
while not over
|
|
clickclear
|
|
while not clickb
|
|
pause 0.01
|
|
end while
|
|
i = int(clickx/H) : j = int(clicky/H)
|
|
if i<N and j<M then
|
|
if clickb=1 then
|
|
if not (f[i,j]&4) then ai = i : aj = j : gosub opencell
|
|
if not s[i,j] then gosub adj
|
|
else
|
|
if not (f[i,j]&1) then
|
|
if f[i,j]&4 then mines = mines+1
|
|
if not (f[i,j]&4) then mines = mines-1
|
|
f[i,j] = (f[i,j]&~4)|(~f[i,j]&4)
|
|
end if
|
|
end if
|
|
if not (togo or mines) then over = 1
|
|
gosub redraw
|
|
end if
|
|
end while
|
|
imgsave "Minesweeper_game_BASIC-256.png", "PNG"
|
|
end
|
|
|
|
redraw:
|
|
for i = 0 to N-1
|
|
for j = 0 to M-1
|
|
if over=-1 and f[i,j]&2 then f[i,j] = f[i,j]|1
|
|
gosub drawcell
|
|
next j
|
|
next i
|
|
# Counter
|
|
color (32,32,32) : rect 0,M*H,N*H,H
|
|
color white : x = 5 : y = M*H+H*0.05
|
|
if not over then text x,y,"Mines: " + mines
|
|
if over=1 then text x,y,"You won!"
|
|
if over=-1 then text x,y,"You lost"
|
|
refresh
|
|
return
|
|
|
|
drawcell:
|
|
color darkgrey
|
|
rect i*H,j*H,H,H
|
|
if f[i,j]&1=0 then # closed
|
|
color black : stamp i*H,j*H,trian1
|
|
color white : stamp i*H,j*H,trian2
|
|
color grey : rect i*H+2,j*H+2,H-4,H-4
|
|
if f[i,j]&4 then color blue : stamp i*H,j*H,flag
|
|
else
|
|
color 192,192,192 : rect i*H+1,j*H+1,H-2,H-2
|
|
# Draw
|
|
if f[i,j]&2 then # mine
|
|
if not (f[i,j]&4) then color red
|
|
if f[i,j]&4 then color darkgreen
|
|
circle i*H+H/2,j*H+H/2,H/5 : stamp i*H,j*H,mine
|
|
else
|
|
if s[i,j] then color (32,32,32) : text i*H+H/3,j*H+1,s[i,j]
|
|
end if
|
|
end if
|
|
return
|
|
|
|
adj:
|
|
aj = j-1
|
|
if j and i then ai = i-1 : gosub adjact
|
|
if j then ai = i : gosub adjact
|
|
if j and i<N-1 then ai = i+1 : gosub adjact
|
|
aj = j
|
|
if i then ai = i-1 : gosub adjact
|
|
if i<N-1 then ai = i+1 : gosub adjact
|
|
aj = j+1
|
|
if j<M-1 and i then ai = i-1 : gosub adjact
|
|
if j<M-1 then ai = i : gosub adjact
|
|
if j<M-1 and i<N-1 then ai = i+1 : gosub adjact
|
|
return
|
|
|
|
adjact:
|
|
if not act then s[ai,aj] = s[ai,aj]+1 : return
|
|
if act then gosub opencell : return
|
|
|
|
opencell:
|
|
if not (f[ai,aj]&1) then
|
|
f[ai,aj] = f[ai,aj]|1
|
|
togo = togo-1
|
|
end if
|
|
if f[ai,aj]&2 then over = -1
|
|
return
|