Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Virus sweeper for CMM2 BASIC
'==================================
'Virus sweeper
'By Henryk K 2021.10.19
'==================================
option explicit
const grid.xmax = 51
const grid.ymax = 37
dim integer grid.squares
const gg.bomb = 0 ' is there a bomb i the square
const gg.checked = 1 ' has the square been checked
const gg.flag = 2 ' is there a flag on the square (unused)
const gg.counter = 3 ' neighbour bomb counter
const gg.fill = 4 ' flood fill helper (unused)
dim integer ged 'trash variable
dim integer grid(grid.xmax,grid.ymax,4)
dim integer updateq(grid.xmax*grid.ymax,1)
dim integer uqhead=0, uqtail=0
dim integer oxx%, oyy% 'old mouse pos
dim integer dirty%
dim integer xx,yy
dim numMines%
dim numLives%
'==================================
' Main loop
'==================================
controller mouse open 2, leftmouse, rightmouse
mode 1,8
font 7,1
do
page write 0
splashScreen
numLives%=2
numMines%=10
initMAP
InitGrid
page write 0
page copy 1 to 0, B
do
dirty% = uqtail
page write 1
do while uqtail <> uqhead
fill updateq(uqtail,0), updateq(uqtail,1)
inc uqtail
if uqtail > (grid.xmax*grid.ymax) then uqtail=0
loop
page write 0
if dirty% <> uqtail then
page write 1
textpanel3d 0, 0, 800, 20,"Squares: " + str$(grid.squares) + " Lives left: " + str$(numLives%)
page write 0
page copy 1 to 0
endif
xx = mouse(x,2)-5
yy = mouse(y,2)-5
if xx <> oxx% or yy <> oyy% or dirty% <> uqtail then
blit oxx% ,oyy% ,oxx% ,oyy% ,10 ,10, 1
line xx,yy,xx+9,yy+9, 1, &h000080
line xx+9,yy,xx,yy+9, 1, &h000080
box xx+2, yy+2, 4, 4, 1, &h00af00
oxx%=xx
oyy%=yy
endif
loop until keydown(1)=27 or numLives%=0 or grid.squares=0
if grid.squares=0 then
page write 0
winnerScreen
endif
loop
controller mouse close
'==================================
' SplashScreen
'==================================
sub splashScreen
cls &h000000
text 400,200,"Welcome to", "CM", 1, 5, &hffff00
text 400,300,"Virus sweeper", "CM", 1, 5, &hffff00
text 400,400,"Programming, by Henryk Krasuski", "CM", 1, 2, &hffff80
text 400,440,"October 2021", "CM", 1, 2, &hffff80
pause 5000
text 400,500,"Press a key", "CM", 1, 2, &hffffff
text 400,520,"use mouse to test squares", "CM", 1, 1, &hff0000
do
loop until keydown(0)
end sub
'==================================
' WinnerScreen
'==================================
sub winnerScreen
cls &h000000
text 400,200,"You win", "CM", 1, 5, &hffff00
text 400,300,"You cleared the map", "CM", 1, 5, &hffff00
pause 5000
end sub
'==================================
' Fill
'==================================
sub fill x, y
if grid(x,y,gg.checked) or grid(x,y,gg.flag) then exit sub
if grid(x,y,gg.bomb) then
inc numLives%, -1
redpanel3d 15*x, 20+15*y, 14, 14,"*"
page write 0
redpanel3d 15*x, 20+15*y, 14, 14,"*"
text grid.xmax*15\2, grid.ymax*15\2,"BOOOOM","CM",1,5,&hff0000,&h000000
pause 2000
if numLives% = 0 then
page copy 1 to 0
text grid.xmax*15\2, grid.ymax*15\2,"GAME OVER","CM",1,3,&hff0000,&h000000
pause 2000
end if
page write 1
page copy 1 to 0
else
inc grid.squares, -1
end if
inc grid(x,y,gg.checked)
if grid(x,y,gg.bomb) then
redpanel3d 15*x, 20+15*y, 14, 14,"!"
exit sub
end if
if grid(x,y,gg.counter) then
greenpanel3d 15*x, 20+15*y, 14, 14,str$(grid(x,y,gg.counter))
exit sub
end if
greenpanel3d 15*x, 20+15*y, 14, 14,""
if y>0 then
updateq(uqhead,0) = x
updateq(uqhead,1) = y-1
inc uqhead
if uqhead > (grid.xmax*grid.ymax) then uqhead=0
end if
if y<grid.ymax then
updateq(uqhead,0) = x
updateq(uqhead,1) = y+1
inc uqhead
if uqhead > (grid.xmax*grid.ymax) then uqhead=0
end if
if x>0 then
updateq(uqhead,0) = x-1
updateq(uqhead,1) = y
inc uqhead
if uqhead > (grid.xmax*grid.ymax) then uqhead=0
end if
if x<grid.xmax then
updateq(uqhead,0) = x+1
updateq(uqhead,1) = y
inc uqhead
if uqhead > (grid.xmax*grid.ymax) then uqhead=0
end if
end sub
'==================================
' Mouse
'==================================
sub leftmouse
local ggx, ggy
ggx = mouse(x,2)\15
ggy = (mouse(y,2)-20)\15
updateq(uqhead,0) = ggx
updateq(uqhead,1) = ggy
if ggx>grid.xmax or ggy>grid.ymax then exit sub
inc uqhead
if uqhead > (grid.xmax*grid.ymax) then uqhead=0
end sub
sub rightmouse
end sub
'==================================
' initMAP
'==================================
sub initMAP
MAP(16)=&hb0b0b0
MAP(17)=&hd0d0d0
MAP(18)=&hffffff
MAP(26)=&hffb0b0
MAP(27)=&hffd0d0
MAP(28)=&hff0000
MAP(38)=&hb0b0b0
MAP(37)=&hd0d0d0
MAP(36)=&hffffff
MAP SET
end sub
'==================================
' initGrid
'==================================
sub initGrid
local xoff%, yoff%, x%, y%, i%, j%
local c(8,1)
grid.squares = (grid.xmax+1)*(grid.ymax+1) - numMines%
' put viruses in squares
i% = 0
do
x% = grid.xmax * rnd
y% = grid.ymax * rnd
if not grid(x%,y%,gg.bomb) then
inc grid(x%,y%,gg.bomb)
inc i%
endif
loop until(i% = numMines%)
' update counters
for y% = 0 to grid.ymax
for x% = 0 to grid.xmax
c(0,0) = x%-1 : c(0,1) = y%-1
c(1,0) = x% : c(1,1) = y%-1
c(2,0) = x%+1 : c(2,1) = y%-1
c(3,0) = x%-1 : c(3,1) = y%
c(4,0) = x% : c(4,1) = y%
c(5,0) = x%+1 : c(5,1) = y%
c(6,0) = x%-1 : c(6,1) = y%+1
c(7,0) = x% : c(7,1) = y%+1
c(8,0) = x%+1 : c(8,1) = y%+1
i% = 0
for j%=0 to 8
if c(j%,0) >= 0 and c(j%,1) >= 0 and c(j%,0) <= grid.xmax and c(j%,1) <= grid.ymax then
if grid(c(j%,0),c(j%,1),gg.bomb) then inc i%
endif
next j%
grid(x%,y%,gg.counter)=i%
print @(0,0),"Generating map: " + str$(100*y%\grid.ymax)+" %"
next x%, y%
page write 1
cls MAP(17)
textpanel3d 0, 0, 800, 20,"Squares: " + str$(grid.squares) + " Virus total: " + str$(numMines%)
for y% = 0 to grid.ymax
for x% = 0 to grid.xmax
j% = x%*15
i% = y%*15+20
' if grid(x%,y%,gg.bomb) then
' redpanel3d j%, i%, 14, 14,"X"
' else if grid(x%,y%,gg.counter) then
' textpanel3d j%, i%, 14, 14,str$(grid(x%,y%,gg.counter))
' else
textpanel3d j%, i%, 14, 14,""
' endif
next x%, y%
end sub
'==================================
' textpanel3d
'==================================
sub textpanel3d x%, y%, w%, h%, t$
box x%, y%, w%, h%, 2,MAP(18), MAP(17)
line x%, y%+h%-1, x%+w%-1, y%+h%-1, 1, MAP(16)
line x%+1, y%+h%-2, x%+w%-2, y%+h%-2, 1, MAP(16)
line x%+w%-1, y%, x%+w%-1, y%+h%-1, 1, MAP(16)
line x%+w%-2, y%+1, x%+w%-2, y%+h%-2, 1, MAP(16)
text x%+5, y%+h%\2, t$, LM,,,0, MAP(17)
end sub
'==================================
' redpanel3d
'==================================
sub redpanel3d x%, y%, w%, h%, t$
box x%, y%, w%, h%, 2,MAP(28), MAP(27)
line x%, y%+h%-1, x%+w%-1, y%+h%-1, 1, MAP(26)
line x%+1, y%+h%-2, x%+w%-2, y%+h%-2, 1, MAP(26)
line x%+w%-1, y%, x%+w%-1, y%+h%-1, 1, MAP(26)
line x%+w%-2, y%+1, x%+w%-2, y%+h%-2, 1, MAP(26)
text x%+5, y%+h%\2, t$, LM,,,0, MAP(27)
end sub
'==================================
' greenpanel3d
'==================================
sub greenpanel3d x%, y%, w%, h%, t$
box x%, y%, w%, h%, 2,MAP(38), MAP(37)
line x%, y%+h%-1, x%+w%-1, y%+h%-1, 1, MAP(36)
line x%+1, y%+h%-2, x%+w%-2, y%+h%-2, 1, MAP(36)
line x%+w%-1, y%, x%+w%-1, y%+h%-1, 1, MAP(36)
line x%+w%-2, y%+1, x%+w%-2, y%+h%-2, 1, MAP(36)
text x%+5, y%+h%\2, t$, LM,,,0, MAP(37)
end sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment