fb:porticula NoPaste
Minesweeper layouter challange
Uploader: | gast |
Datum/Zeit: | 12.06.2006 23:28:03 |
'Autor: VonGodric
option explicit
option escape
randomize timer
' generate minesweeper layout
' args:
' cols - how many columns?
' lines - how many lines?
' bombs - how many bombs to generate?
' return:
' tring map.
function GenerateGrid ( byval cols as integer, lines as integer, bombs as integer ) as string
dim grid (1 to cols, 1 to lines) as byte
dim as integer x, y
dim cnt as integer
dim strReturn as string
' place bombs
for cnt = 1 to bombs
do
x = rnd * cols + 1
y = rnd * lines + 1
if (grid(x, y) = 0) then
grid(x, y) = -1
exit do
end if
loop
next
' add numbers
for y = 1 to lines
for x = 1 to cols
cnt = 0
' has a bomb?
if (grid(x,y)=-1) then continue for
' on left
if (x > 1) then if (grid(x-1, y)=-1) then cnt += 1
' on right
if (x < cols) then if (grid(x+1, y)=-1) then cnt += 1
' top ?
if (y > 1) then if (grid(x, y-1)=-1) then cnt += 1
' bottom ?
if (y < lines) then if (grid(x, y+1)=-1) then cnt += 1
' upper-left?
if (x > 1 and y > 1) then if (grid(x-1, y-1)=-1) then cnt += 1
' upper-right
if (x < cols and y > 1) then if (grid(x+1, y-1)=-1) then cnt += 1
' bottom-left
if (x > 1 and y < lines) then if (grid(x-1, y+1)) then cnt += 1
' bottom-right
if (x < cols and y < lines) then if (grid(x+1, y+1)) then cnt += 1
if (cnt > 0) then grid(x,y) = cnt
next
next
'Generate string
for y = 1 to lines
for x = 1 to cols
if (grid(x, y)=-1) then
strReturn += "x"
elseif (grid(x, y)>0) then
strReturn += str(grid(x, y))
else
strReturn += " "
end if
next
strReturn += "\n"
next
return strReturn
end function
' run it
print GenerateGrid( 9, 9, 10)
sleep