Post by bplus on Jan 16, 2024 19:56:29 GMT
The goal is to cover the board in one color, select a color on right column and then click the board where you want to 'flood" a color.
Every color left, right up and down of same color will be filled by color selected at right.
This is a variation of this game:
qb64.boards.net/thread/267/floodfill-recursive-algorithm?page=2
That allows you to fill from any cell on the board.
Every color left, right up and down of same color will be filled by color selected at right.
Option _Explicit
_Title "Flood Mouse(mx,my) Game" ' b+ 2024-01-16
' thanks william33 who got me interested in flood fill problem then the game
' https://qb64.boards.net/thread/267/floodfill-recursive-algorithm
' This is a variation on the flood(1,1) cell game where you are free to chose
' which cell on board to flood with your selected color.
Randomize Timer
Screen _NewImage(680, 672, 12)
_PrintMode _KeepBackground
_ScreenMove 250, 20 ' center on my screen you may need different
Dim Shared As Long level(1 To 14, 1 To 14), Count, ColorNumber
Dim As Long x, y, cl, c, mb, my, mx
While _KeyDown(27) = 0
Count = 0: ColorNumber = 0
For y = 1 To 14
For x = 1 To 14
level(x, y) = Int(Rnd * 6) + 1
Next
Next
drawlevel
Do
While _MouseInput: Wend
mb = _MouseButton(1): mx = _MouseX: my = _MouseY
If mb Then
_Delay .3
If mx < 560 Then ' mouse click on board convert to cell x, y
my = Int((my - 1) / 48) + 1
mx = Int((mx - 1) / 40) + 1
' flood from (mx, my)
Count = Count + 1
If ColorNumber Then
flood mx, my
drawlevel
End If
c = level(1, 1) ' check win
For y = 1 To 14
For x = 1 To 14
If level(x, y) <> c Then GoTo skip
Next
Next
Sound 1000, 4: _Delay 5: Exit Do
Else
cl = Int((_MouseY - 145) / 48) + 1
If cl >= 1 And cl <= 6 Then
ColorNumber = cl
drawlevel
End If
End If
End If ' if no mouse click move along
skip:
_Limit 30
Loop
Wend
Sub drawlevel
Dim As Long x, y
Cls
For y = 1 To 14
For x = 1 To 14
Line ((x - 1) * 40, (y - 1) * 48)-Step(39, 47), level(x, y), BF
_PrintString ((x - 1) * 40 + 16, (y - 1) * 48 + 16), _Trim$(Str$(level(x, y)))
Next
Print
Next
Print
For y = 1 To 6 ' the color choices for clicking
Line (600, (y - 1) * 48 + 148)-Step(40, 47), y, BF
If y = ColorNumber Then Line (600 + 2, (y - 1) * 48 + 148 + 2)-Step(36, 43), 15, B
_PrintString (15 * 40 + 16, (y - 1) * 48 + 148 + 16), _Trim$(Str$(y))
Next
_PrintString (15 * 40 + 16, 7 * 48 + 148 + 16), _Trim$(Str$(Count))
End Sub
Sub flood (x0 As Long, y0 As Long) ' needs max, min functions mod of my Paint3 sub
Dim As Long W, H, parentF, fill, tick, ystart, ystop, xstart, xstop, x, y
W = 14: H = 14
Dim temp(W, H)
temp(x0, y0) = 1: parentF = 1
fill = level(x0, y0) ' the color at x0, y0 to fill with selected colornumber
level(x0, y0) = ColorNumber
While parentF = 1
parentF = 0: tick = tick + 1
ystart = max(y0 - tick, 1): ystop = min(y0 + tick, H)
y = ystart
While y <= ystop
xstart = max(x0 - tick, 1): xstop = min(x0 + tick, W)
x = xstart
While x <= xstop
If level(x, y) = fill And temp(x, y) = 0 Then
If temp(max(1, x - 1), y) Then
temp(x, y) = 1: parentF = 1: level(x, y) = ColorNumber
ElseIf temp(min(x + 1, W), y) Then
temp(x, y) = 1: parentF = 1: level(x, y) = ColorNumber
ElseIf temp(x, max(y - 1, 1)) Then
temp(x, y) = 1: parentF = 1: level(x, y) = ColorNumber
ElseIf temp(x, min(y + 1, H)) Then
temp(x, y) = 1: parentF = 1: level(x, y) = ColorNumber
End If
End If
x = x + 1
Wend
y = y + 1
Wend
Wend
End Sub
Function max (n1, n2)
If n1 > n2 Then max = n1 Else max = n2
End Function
Function min (n1, n2)
If n1 > n2 Then min = n2 Else min = n1
End Function
This is a variation of this game:
qb64.boards.net/thread/267/floodfill-recursive-algorithm?page=2
That allows you to fill from any cell on the board.