# Minesweeper problem

## Recommended Posts

Got most of it working, but the recursion level is exceeded if you click on a square not touching any mines because it tries checking all adjacent squares to see if they should be deleted too.

Is there another way to do this?

```create(\$width, \$height, \$mines, \$time)

While 1
Sleep(1000)
WEnd

Func create(\$width, \$height, \$mines, \$time)
\$maingui = GUICreate("Mine Sweeper", 15 * \$width - 8, 15 * \$height + 32, -1, -1)
\$minesleftinput = GUICtrlCreateInput(\$minesleft, 1, 1, 66, 40, BitOR(\$ES_READONLY, \$ES_CENTER))
GUICtrlSetFont(-1, 20, 400, 0, "MS Sans Serif")
\$timeinput = GUICtrlCreateInput(\$time, \$width * 15 - 74, 1, 66, 40, BitOR(\$ES_READONLY, \$ES_CENTER))
GUICtrlSetFont(-1, 20, 400, 0, "MS Sans Serif")
createbuttons(\$width, \$height)

;disperse mines
For \$i = 1 To \$mines
\$buttonnumber[Random(1, \$height, 1)][Random(1, \$width, 1)] = "mine"
Next

;generate numbers
For \$i = 1 To \$width
For \$j = 1 To \$height

If \$buttonnumber[\$i][\$j] = "mine" Then ContinueLoop

;if not lefttopmost then check lefttop
If \$i > 1 Then
If \$j > 1 Then
If \$buttonnumber[\$i - 1][\$j - 1] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf
EndIf

;if not leftmost then check left
If \$i > 1 Then
If \$buttonnumber[\$i - 1][\$j] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf

;if not leftbottommost then check leftbottom
If \$i > 1 Then
If \$j < \$height Then
If \$buttonnumber[\$i - 1][\$j + 1] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf
EndIf

;if not bottommost then check botton
If \$j < \$height Then
If \$buttonnumber[\$i][\$j + 1] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf

;if not bottomrightmost then check bottomright
If \$i < \$width Then
If \$j < \$height Then
If \$buttonnumber[\$i + 1][\$j + 1] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf
EndIf

;if not rightmost then check right
If \$i < \$width Then
If \$buttonnumber[\$i + 1][\$j] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf

;if not righttopmost then check righttop
If \$i < \$width Then
If \$j > 1 Then
If \$buttonnumber[\$i + 1][\$j - 1] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf
EndIf

;if not topmost then check top
If \$j > 1 Then
If \$buttonnumber[\$i][\$j - 1] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf
Next
Next
GUISetOnEvent(\$GUI_EVENT_CLOSE, "close", \$maingui)
GUISetState()
EndFunc

Func createbuttons(\$width, \$height)
For \$i = 1 To \$width
For \$j = 1 To \$height
\$button[\$i][\$j] = GUICtrlCreateButton("", \$i * 14 - 13, \$j * 14 + 27, 15, 15, \$BS_BITMAP)
GUICtrlSetOnEvent(-1, "buttonclick")
Next
Next
EndFunc

Func buttonclick()
For \$i = 1 To \$width
For \$j = 1 To \$height
If @GUI_CtrlId = \$button[\$i][\$j] Then
If \$buttonnumber[\$i][\$j] = "mine" Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\mine.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 1 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\1.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 2 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\2.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 3 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\3.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 4 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\4.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 5 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\5.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 6 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\6.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 7 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\7.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 8 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\8.bmp")
ElseIf \$buttonnumber[\$i][\$j] = "" Then
GUICtrlDelete(\$button[\$i][\$j])
;check left & lefttop
If \$i > 1 Then
If \$buttonnumber[\$i - 1][\$j] = "" Then
buttoncheck(\$i-1, \$j)
If \$j > 1 Then
If \$buttonnumber[\$i - 1][\$j - 1] = "" Then
buttoncheck(\$i - 1, \$j - 1)
EndIf
EndIf
EndIf
EndIf
;check top & topright
If \$j > 1 Then
If \$buttonnumber[\$i][\$j - 1] = "" Then
buttoncheck(\$i, \$j - 1)
If \$i < \$width Then
If \$buttonnumber[\$i + 1][\$j + 1] Then
buttoncheck(\$i + 1, \$j + 1)
EndIf
EndIf
EndIf
EndIf
;check right & rightbottom
If \$i < \$width Then
If \$buttonnumber[\$i + 1][\$j] = "" Then
buttoncheck(\$i + 1, \$j)
If \$j < \$height Then
If \$buttonnumber[\$i + 1][\$j + 1] = "" Then
buttoncheck(\$i + 1, \$j + 1)
EndIf
EndIf
EndIf
EndIf
;check bottom & bottomleft
If \$j < \$height Then
If \$buttonnumber[\$i][\$j + 1] = "" Then
buttoncheck(\$i, \$j + 1)
If \$i > 1 Then
If \$buttonnumber[\$i - 1][\$j + 1] = "" Then
buttoncheck(\$i - 1, \$j + 1)
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
Next
Next
EndFunc

Func buttoncheck(\$i, \$j)
If \$buttonnumber[\$i][\$j] = "mine" Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\mine.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 1 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\1.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 2 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\2.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 3 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\3.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 4 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\4.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 5 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\5.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 6 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\6.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 7 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\7.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 8 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\8.bmp")
ElseIf \$buttonnumber[\$i][\$j] = "" Then
GUICtrlDelete(\$button[\$i][\$j])
;check left & lefttop
If \$i > 1 Then
If \$buttonnumber[\$i - 1][\$j] = "" Then
buttoncheck(\$i-1, \$j)
If \$j > 1 Then
If \$buttonnumber[\$i - 1][\$j - 1] = "" Then
buttoncheck(\$i - 1, \$j - 1)
EndIf
EndIf
EndIf
EndIf
;check top & topright
If \$j > 1 Then
If \$buttonnumber[\$i][\$j - 1] = "" Then
buttoncheck(\$i, \$j - 1)
If \$i < \$width Then
If \$buttonnumber[\$i + 1][\$j + 1] Then
buttoncheck(\$i + 1, \$j + 1)
EndIf
EndIf
EndIf
EndIf
;check right & rightbottom
If \$i < \$width Then
If \$buttonnumber[\$i + 1][\$j] = "" Then
buttoncheck(\$i + 1, \$j)
If \$j < \$height Then
If \$buttonnumber[\$i + 1][\$j + 1] = "" Then
buttoncheck(\$i + 1, \$j + 1)
EndIf
EndIf
EndIf
EndIf
;check bottom & bottomleft
If \$j < \$height Then
If \$buttonnumber[\$i][\$j + 1] = "" Then
buttoncheck(\$i, \$j + 1)
If \$i > 1 Then
If \$buttonnumber[\$i - 1][\$j + 1] = "" Then
buttoncheck(\$i - 1, \$j + 1)
EndIf
EndIf
EndIf
EndIf
EndIf
EndFunc

Func close()
Exit
EndFunc```

I also need a way to mark the mines found.

Edited by Hawkwing

The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again.

##### Share on other sites

Maybe post a working example? The one you posted doesn't have all the necessary includes, doesn't have variables declared globally, and for that matter even then doesn't work so...

Post something that at least runs without erroring?

##### Share on other sites

woops, thought I had copied the whole script. >_<

```#include<GuiConstants.au3>
#include<GuiEdit.au3>
#include<Misc.au3>
#include<ButtonConstants.au3>

#NoTrayIcon

Opt("GuiOnEventMode", 1)

Local \$timeinput, \$minesleftinput

\$width = 10
\$height = 10
\$mines = 10
\$minesleft = 10
\$time = 0
Dim \$button[100][100]
Dim \$buttonnumber[100][100]

create(\$width, \$height, \$mines, \$time)

While 1
Sleep(1000)
WEnd

Func create(\$width, \$height, \$mines, \$time)
\$maingui = GUICreate("Mine Sweeper", 15 * \$width - 8, 15 * \$height + 32, -1, -1)
\$minesleftinput = GUICtrlCreateInput(\$minesleft, 1, 1, 66, 40, BitOR(\$ES_READONLY, \$ES_CENTER))
GUICtrlSetFont(-1, 20, 400, 0, "MS Sans Serif")
\$timeinput = GUICtrlCreateInput(\$time, \$width * 15 - 74, 1, 66, 40, BitOR(\$ES_READONLY, \$ES_CENTER))
GUICtrlSetFont(-1, 20, 400, 0, "MS Sans Serif")
createbuttons(\$width, \$height)

;disperse mines
For \$i = 1 To \$mines
\$buttonnumber[Random(1, \$height, 1)][Random(1, \$width, 1)] = "mine"
Next

;generate numbers
For \$i = 1 To \$width
For \$j = 1 To \$height

If \$buttonnumber[\$i][\$j] = "mine" Then ContinueLoop

;if not lefttopmost then check lefttop
If \$i > 1 Then
If \$j > 1 Then
If \$buttonnumber[\$i - 1][\$j - 1] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf
EndIf

;if not leftmost then check left
If \$i > 1 Then
If \$buttonnumber[\$i - 1][\$j] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf

;if not leftbottommost then check leftbottom
If \$i > 1 Then
If \$j < \$height Then
If \$buttonnumber[\$i - 1][\$j + 1] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf
EndIf

;if not bottommost then check botton
If \$j < \$height Then
If \$buttonnumber[\$i][\$j + 1] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf

;if not bottomrightmost then check bottomright
If \$i < \$width Then
If \$j < \$height Then
If \$buttonnumber[\$i + 1][\$j + 1] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf
EndIf

;if not rightmost then check right
If \$i < \$width Then
If \$buttonnumber[\$i + 1][\$j] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf

;if not righttopmost then check righttop
If \$i < \$width Then
If \$j > 1 Then
If \$buttonnumber[\$i + 1][\$j - 1] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf
EndIf

;if not topmost then check top
If \$j > 1 Then
If \$buttonnumber[\$i][\$j - 1] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf
Next
Next
GUISetOnEvent(\$GUI_EVENT_CLOSE, "close", \$maingui)
GUISetState()
EndFunc

Func createbuttons(\$width, \$height)
For \$i = 1 To \$width
For \$j = 1 To \$height
\$button[\$i][\$j] = GUICtrlCreateButton("", \$i * 14 - 13, \$j * 14 + 27, 15, 15, \$BS_BITMAP)
GUICtrlSetOnEvent(-1, "buttonclick")
Next
Next
EndFunc

Func buttonclick()
For \$i = 1 To \$width
For \$j = 1 To \$height
If @GUI_CtrlId = \$button[\$i][\$j] Then
If \$buttonnumber[\$i][\$j] = "mine" Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\mine.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 1 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\1.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 2 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\2.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 3 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\3.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 4 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\4.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 5 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\5.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 6 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\6.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 7 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\7.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 8 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\8.bmp")
ElseIf \$buttonnumber[\$i][\$j] = "" Then
GUICtrlDelete(\$button[\$i][\$j])
;check left & lefttop
If \$i > 1 Then
If \$buttonnumber[\$i - 1][\$j] = "" Then
buttoncheck(\$i-1, \$j)
If \$j > 1 Then
If \$buttonnumber[\$i - 1][\$j - 1] = "" Then
buttoncheck(\$i - 1, \$j - 1)
EndIf
EndIf
EndIf
EndIf
;check top & topright
If \$j > 1 Then
If \$buttonnumber[\$i][\$j - 1] = "" Then
buttoncheck(\$i, \$j - 1)
If \$i < \$width Then
If \$buttonnumber[\$i + 1][\$j + 1] Then
buttoncheck(\$i + 1, \$j + 1)
EndIf
EndIf
EndIf
EndIf
;check right & rightbottom
If \$i < \$width Then
If \$buttonnumber[\$i + 1][\$j] = "" Then
buttoncheck(\$i + 1, \$j)
If \$j < \$height Then
If \$buttonnumber[\$i + 1][\$j + 1] = "" Then
buttoncheck(\$i + 1, \$j + 1)
EndIf
EndIf
EndIf
EndIf
;check bottom & bottomleft
If \$j < \$height Then
If \$buttonnumber[\$i][\$j + 1] = "" Then
buttoncheck(\$i, \$j + 1)
If \$i > 1 Then
If \$buttonnumber[\$i - 1][\$j + 1] = "" Then
buttoncheck(\$i - 1, \$j + 1)
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
Next
Next
EndFunc

Func buttoncheck(\$i, \$j)
If \$buttonnumber[\$i][\$j] = "mine" Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\mine.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 1 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\1.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 2 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\2.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 3 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\3.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 4 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\4.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 5 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\5.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 6 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\6.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 7 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\7.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 8 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\8.bmp")
ElseIf \$buttonnumber[\$i][\$j] = "" Then
GUICtrlDelete(\$button[\$i][\$j])
;check left & lefttop
If \$i > 1 Then
If \$buttonnumber[\$i - 1][\$j] = "" Then
buttoncheck(\$i-1, \$j)
If \$j > 1 Then
If \$buttonnumber[\$i - 1][\$j - 1] = "" Then
buttoncheck(\$i - 1, \$j - 1)
EndIf
EndIf
EndIf
EndIf
;check top & topright
If \$j > 1 Then
If \$buttonnumber[\$i][\$j - 1] = "" Then
buttoncheck(\$i, \$j - 1)
If \$i < \$width Then
If \$buttonnumber[\$i + 1][\$j + 1] Then
buttoncheck(\$i + 1, \$j + 1)
EndIf
EndIf
EndIf
EndIf
;check right & rightbottom
If \$i < \$width Then
If \$buttonnumber[\$i + 1][\$j] = "" Then
buttoncheck(\$i + 1, \$j)
If \$j < \$height Then
If \$buttonnumber[\$i + 1][\$j + 1] = "" Then
buttoncheck(\$i + 1, \$j + 1)
EndIf
EndIf
EndIf
EndIf
;check bottom & bottomleft
If \$j < \$height Then
If \$buttonnumber[\$i][\$j + 1] = "" Then
buttoncheck(\$i, \$j + 1)
If \$i > 1 Then
If \$buttonnumber[\$i - 1][\$j + 1] = "" Then
buttoncheck(\$i - 1, \$j + 1)
EndIf
EndIf
EndIf
EndIf
EndIf
EndFunc

Func close()
Exit
EndFunc```

sorry

The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again.

##### Share on other sites

I've edited this post so many times I decided to just rewrite it.

the recursion error was caused by buttoncheck() calling itself too many times without ever ending;

* I renamed buttoncheck() to buttoncheck2() and adjusted it so that instead of calling itself, it will return the new values of \$i and \$j to be checked, or set @error. (part of the reason for the recursion error)

* I wrote a new function called buttoncheck(), that will call buttoncheck2() with updated values until @error is set. (at this time the two functions did the exact same thing as your old function, but without the risk of a recursion error. It did however get stuck in the same infinite loop)

* When buttoncheck2() removes a button without a mine, \$buttonnumber[\$i][\$j] will be set to "0", to avoid buttoncheck2() from bouncing between the same 2 cells. (takes care of the infinite loop; the other part of the recursion error)

* At line 37 I added a check to avoid placing the same mine twice.

* I changed the syntax of buttoncheck2() a bit, but that was just to help myself.

```#include<GuiConstants.au3>
#include<GuiEdit.au3>
#include<Misc.au3>
#include<ButtonConstants.au3>

#NoTrayIcon

Opt("GuiOnEventMode", 1)

Local \$timeinput, \$minesleftinput

\$width = 10
\$height = 10
\$mines = 10
\$minesleft = 10
\$time = 0
Dim \$button[100][100]
Dim \$buttonnumber[100][100]

create(\$width, \$height, \$mines, \$time)

While 1
Sleep(1000)
WEnd

Func create(\$width, \$height, \$mines, \$time)
Local \$rRow, \$rColumn
\$maingui = GUICreate("Mine Sweeper", 15 * \$width - 8, 15 * \$height + 32, -1, -1)
\$minesleftinput = GUICtrlCreateInput(\$minesleft, 1, 1, 66, 40, BitOR(\$ES_READONLY, \$ES_CENTER))
GUICtrlSetFont(-1, 20, 400, 0, "MS Sans Serif")
\$timeinput = GUICtrlCreateInput(\$time, \$width * 15 - 74, 1, 66, 40, BitOR(\$ES_READONLY, \$ES_CENTER))
GUICtrlSetFont(-1, 20, 400, 0, "MS Sans Serif")
createbuttons(\$width, \$height)

;disperse mines
For \$i = 1 To \$mines
Do
\$rRow = Random(1, \$height, 1)
\$rColumn = Random(1, \$width, 1)
Until \$buttonnumber[\$rRow][\$rColumn] <> "mine"
\$buttonnumber[\$rRow][\$rColumn] = "mine"
Next

;generate numbers
For \$i = 1 To \$width
For \$j = 1 To \$height

If \$buttonnumber[\$i][\$j] = "mine" Then ContinueLoop

;if not lefttopmost then check lefttop
If \$i > 1 Then
If \$j > 1 Then
If \$buttonnumber[\$i - 1][\$j - 1] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf
EndIf

;if not leftmost then check left
If \$i > 1 Then
If \$buttonnumber[\$i - 1][\$j] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf

;if not leftbottommost then check leftbottom
If \$i > 1 Then
If \$j < \$height Then
If \$buttonnumber[\$i - 1][\$j + 1] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf
EndIf

;if not bottommost then check botton
If \$j < \$height Then
If \$buttonnumber[\$i][\$j + 1] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf

;if not bottomrightmost then check bottomright
If \$i < \$width Then
If \$j < \$height Then
If \$buttonnumber[\$i + 1][\$j + 1] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf
EndIf

;if not rightmost then check right
If \$i < \$width Then
If \$buttonnumber[\$i + 1][\$j] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf

;if not righttopmost then check righttop
If \$i < \$width Then
If \$j > 1 Then
If \$buttonnumber[\$i + 1][\$j - 1] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf
EndIf

;if not topmost then check top
If \$j > 1 Then
If \$buttonnumber[\$i][\$j - 1] = "mine" Then
\$buttonnumber[\$i][\$j] += 1
EndIf
EndIf
Next
Next
GUISetOnEvent(\$GUI_EVENT_CLOSE, "close", \$maingui)
GUISetState()
EndFunc

Func createbuttons(\$width, \$height)
For \$i = 1 To \$width
For \$j = 1 To \$height
\$button[\$i][\$j] = GUICtrlCreateButton("", \$i * 14 - 13, \$j * 14 + 27, 15, 15, \$BS_BITMAP)
GUICtrlSetOnEvent(-1, "buttonclick")
Next
Next
EndFunc

Func buttonclick()
For \$i = 1 To \$width
For \$j = 1 To \$height
If @GUI_CtrlId = \$button[\$i][\$j] Then
If \$buttonnumber[\$i][\$j] = "mine" Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\mine.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 1 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\1.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 2 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\2.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 3 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\3.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 4 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\4.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 5 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\5.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 6 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\6.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 7 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\7.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 8 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\8.bmp")
ElseIf \$buttonnumber[\$i][\$j] = "" Then
GUICtrlDelete(\$button[\$i][\$j])
;check left & lefttop
If \$i > 1 Then
If \$buttonnumber[\$i - 1][\$j] = "" Then
buttoncheck(\$i-1, \$j)
If \$j > 1 Then
If \$buttonnumber[\$i - 1][\$j - 1] = "" Then
buttoncheck(\$i - 1, \$j - 1)
EndIf
EndIf
EndIf
EndIf
;check top & topright
If \$j > 1 Then
If \$buttonnumber[\$i][\$j - 1] = "" Then
buttoncheck(\$i, \$j - 1)
If \$i < \$width Then
If \$buttonnumber[\$i + 1][\$j + 1] Then
buttoncheck(\$i + 1, \$j + 1)
EndIf
EndIf
EndIf
EndIf
;check right & rightbottom
If \$i < \$width Then
If \$buttonnumber[\$i + 1][\$j] = "" Then
buttoncheck(\$i + 1, \$j)
If \$j < \$height Then
If \$buttonnumber[\$i + 1][\$j + 1] = "" Then
buttoncheck(\$i + 1, \$j + 1)
EndIf
EndIf
EndIf
EndIf
;check bottom & bottomleft
If \$j < \$height Then
If \$buttonnumber[\$i][\$j + 1] = "" Then
buttoncheck(\$i, \$j + 1)
If \$i > 1 Then
If \$buttonnumber[\$i - 1][\$j + 1] = "" Then
buttoncheck(\$i - 1, \$j + 1)
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
Next
Next
EndFunc

Func buttoncheck(\$i, \$j)
Local \$ReturnArray[2]
While 1
\$ReturnArray = buttoncheck2(\$i, \$j)
If @error Then  ExitLoop
\$i = \$ReturnArray[0]
\$j = \$ReturnArray[1]
WEnd
EndFunc

Func buttoncheck2(\$i, \$j)
Local \$ReturnArray[2]
If \$buttonnumber[\$i][\$j] = "mine" Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\mine.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 1 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\1.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 2 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\2.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 3 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\3.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 4 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\4.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 5 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\5.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 6 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\6.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 7 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\7.bmp")
ElseIf \$buttonnumber[\$i][\$j] = 8 Then
GUICtrlSetImage(\$button[\$i][\$j], @ScriptDir & "\bitmaps\8.bmp")
ElseIf \$buttonnumber[\$i][\$j] = "" Then
GUICtrlDelete(\$button[\$i][\$j])
\$buttonnumber[\$i][\$j] = "0"
;check left & lefttop
If (\$i > 1) And (\$buttonnumber[\$i - 1][\$j] = "") Then
;buttoncheck(\$i-1, \$j)
If (\$j > 1) And (\$buttonnumber[\$i - 1][\$j - 1] = "") Then
\$ReturnArray[0] = \$i - 1
\$ReturnArray[1] = \$j - 1
;buttoncheck(\$i - 1, \$j - 1)
Else
\$ReturnArray[0] = \$i - 1
\$ReturnArray[1] = \$j
EndIf
;check top & topright
ElseIf (\$j > 1) And (\$buttonnumber[\$i][\$j - 1] = "") Then
;buttoncheck(\$i, \$j - 1)
If (\$i < \$width) And (\$buttonnumber[\$i + 1][\$j + 1] = "") Then
\$ReturnArray[0] = \$i + 1
\$ReturnArray[1] = \$j + 1
;buttoncheck(\$i + 1, \$j + 1)
Else
\$ReturnArray[0] = \$i
\$ReturnArray[1] = \$j - 1
EndIf
;check right & rightbottom
ElseIf (\$i < \$width) And (\$buttonnumber[\$i + 1][\$j] = "") Then
;buttoncheck(\$i + 1, \$j)
If (\$j < \$height) And (\$buttonnumber[\$i + 1][\$j + 1] = "") Then
\$ReturnArray[0] = \$i + 1
\$ReturnArray[1] = \$j + 1
;buttoncheck(\$i + 1, \$j + 1)
Else
\$ReturnArray[0] = \$i + 1
\$ReturnArray[1] = \$j
EndIf
;check bottom & bottomleft
ElseIf (\$j < \$height) And (\$buttonnumber[\$i][\$j + 1] = "") Then
;buttoncheck(\$i, \$j + 1)
If (\$i > 1) And (\$buttonnumber[\$i - 1][\$j + 1] = "") Then
\$ReturnArray[0] = \$i
\$ReturnArray[1] = \$j + 1
;buttoncheck(\$i - 1, \$j + 1)
Else
\$ReturnArray[0] = \$i - 1
\$ReturnArray[1] = \$j + 1
EndIf
Else
Return SetError(1)
EndIf
Return \$ReturnArray
EndIf
EndFunc

Func close()
Exit
EndFunc```

There is still a chance for the script to crash when buttoncheck2() returns nothing and doesn't set @error, which was also present in the original code as far as I can see, but I have to go now, so I'll leave that to you.

Hope this helps you.

Tom

Edited by Tvern

##### Share on other sites

Well, I'm finally bored enough to work on this again. Checking adjacent squares, and then squares adjacent to those squares until it gets to a mine will likely always exceed the recursion level. I was thinking about maybe storing all of the "open spaces" in an array, but I'm not really sure where to begin.

The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again.

## Create an account

Register a new account

×

• Wiki

• Back

• Git