Hello VB forums. I am currently doing a project for my Computing class in which I need to program Minesweeper in VB6. It's mostly done: the win-state, file-handling, lose-state, tile values, revealed tiles and bombs are all working. It's simply what happens when the player clicks a blank tile and it checks all the surrounding tiles, leading to an expanded effect so you don't need to click around randomly in the hopes of finding a mine.
I've tried to implement a static array, then I tried to use a dynamic array to no avail.
I apologise if the code looks horrible, I would prefer to use a language like Java, but VisualBasic makes GUI's a lot easier and quicker to make.
I hope you can help, thanks in advance.
I've tried to implement a static array, then I tried to use a dynamic array to no avail.
Code:
Option Explicit
Dim queue() 'List of numbers to be processed
Dim queueSize As Integer
Dim bomb(80) As Boolean 'Boolean array that returns true when a bomb is present
Dim marked(80) As Boolean 'Boolean array that returns true when a tile is marked
Dim revealed(80) As Boolean 'Boolean array that returns true when a tile is revealed
Dim bombCount As Integer 'Number of bombs present on the grid
Dim gameState As Boolean 'True = Game in progress, False = Game over
Dim time As Double 'Game time
Const stats = "C:\Program FIles\AntCGallagher\Minesweeper\stats.txt" 'Statistics file rootpath
Private Sub cmdBack_Click()
frmIntro.Show
Me.Hide
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 0 To 80
picGrid(i).FontSize = 12
picGrid(i).Font = "Rockwell Extra Bold"
picGrid(i).AutoRedraw = True
picGrid(i).BackColor = RGB(100, 255, 100)
picDisplay.AutoRedraw = True
Next i
gameState = False
End Sub
Private Sub cmdStart_Click()
Dim i As Integer
Dim j As Integer
'Store Game
tmrTime.Enabled = False
frmStatistics.CreateStats
gameState = True
bombCount = 0
time = 0
queueSize = -1
For i = 0 To 80 '
bomb(i) = False
marked(i) = False
revealed(i) = False
Next i
Dim bombLoc As Integer
Do While bombCount < 10
Randomize
bombLoc = CInt(Int((80 * Rnd())))
If bomb(bombLoc) = False Then
bombCount = bombCount + 1
bomb(bombLoc) = True
revealed(bombLoc) = True
End If
Loop
For i = 0 To 8 'Select row
For j = 0 To 8 'Select column
If (Not bomb((i * 9) + j)) Then
picGrid((i * 9) + j).Cls
picGrid((i * 9) + j).BackColor = RGB(100, 100, 255) 'Set tiles to blue
Else
picGrid((i * 9) + j).Cls
picGrid((i * 9) + j).BackColor = RGB(255, 100, 100) 'Set bombs to red
End If
Next j
Next i
tmrTime.Enabled = True
End Sub
Private Sub picGrid_MouseUp(index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim value As Integer '0 = blank, 1-9 = 1-9 bombs surrounding, -1 = bomb
Dim row As Integer
Dim column As Integer
Dim tempIndex As Integer
If gameState = True Then
If Button = 1 Then
row = 0
column = 0
If bomb(index) = True Then
Call processVal(-1, index, 0, 0, True)
End If
tempIndex = index
Do While (tempIndex >= 9)
tempIndex = tempIndex - 9
row = row + 1
Loop
column = tempIndex
value = checkTile(index, row, column)
Call processVal(value, index, row, column, True)
ElseIf Button = 2 Then
value = -2
Call processVal(value, index, 0, 0, True)
End If
End If
End Sub
Sub processVal(ByVal value As Integer, index As Integer, row As Integer, column As Integer, doQueue As Boolean)
Dim times As Integer
Dim i As Integer
Select Case value
Case -1
tmrTime.Enabled = False
For times = 0 To 80
If (bomb(times) = True) Then
picGrid(times).Cls
picGrid(times).BackColor = RGB(255, 0, 0)
End If
If bomb(times) = False And marked(times) = True Then
picGrid(times).Cls
picGrid(times).Print " x"
End If
Next times
MsgBox "You have lost", vbOKOnly, "Game Over"
Call storeResult("loss")
Exit Sub
Case 0
picGrid(index).Cls
picGrid(index).BackColor = RGB(255, 255, 255)
'Check tiles around it
If revealed(index) = False Then
For times = (row - 1) To (row + 1)
For i = (column - 1) To (column + 1)
If Not (times < 0 Or times > 9 Or i < 0 Or i > 9) Then
If Not (times = row And i = column) Then
queueSize = queueSize + 1
ReDim Preserve queue(queueSize)
queue(queueSize) = ((times * 9) + i)
End If
End If
Next i
Next times
End If
revealed(index) = True
Case 1 To 9
picGrid(index).Cls
picGrid(index).BackColor = RGB(255, 255, 255)
picGrid(index).Print value
revealed(index) = True
Case -2
If marked(index) = False Then
bombCount = bombCount - 1
picGrid(index).Cls
picGrid(index).BackColor = RGB(255, 100, 100)
marked(index) = True
Else
bombCount = bombCount + 1
picGrid(index).Cls
picGrid(index).BackColor = RGB(100, 100, 255)
marked(index) = False
End If
End Select
If (doQueue = True) Then
Call executeQueue
End If
Dim markedbombs As Integer
markedbombs = 0
For times = 0 To 80 ' check winstate
If (marked(times) = True And bomb(times) = True) Then
markedbombs = markedbombs + 1
End If
Next times
Dim bombsIsolated As Boolean
bombsIsolated = True
For times = 0 To 80
If (revealed(times) = False) Then
bombsIsolated = False
End If
Next times
If ((markedbombs = 10 Or bombsIsolated = True) And gameState = True) Then
tmrTime.Enabled = False
MsgBox "You win!", vbOKOnly, "Winner!"
Call storeResult("win")
End If
End Sub
Private Sub executeQueue()
Dim times As Integer
Dim row As Integer
Dim column As Integer
For times = 0 To queueSize
If queue(times) >= 0 And queue(times) <= 9 Then
row = 0
column = 0
Dim tempIndex As Integer
tempIndex = queue(times)
Do While (tempIndex >= 9)
tempIndex = tempIndex - 9
row = row + 1
Loop
column = tempIndex
Dim tempvalue As Integer
tempvalue = checkValue(row, column)
If Not (tempvalue = -1) Then
Call processVal(tempvalue, ((row * 9) + column), row, column, False)
queue(queueSize) = -2
End If
End If
Next times
End Sub
Private Sub tmrTime_Timer()
time = Round(time + 0.01, 2)
picDisplay.Cls
picDisplay.Print "Time: "; time; "s"
picDisplay.Print
picDisplay.Print "Bombs: "; bombCount
End Sub
Private Function checkTile(ByVal index As Integer, row As Integer, column As Integer) As Integer
Dim value As Integer
value = checkValue(row, column)
If value >= 0 And value <= 9 Then
checkTile = value
End If
End Function
Private Function checkValue(ByVal row As Integer, column As Integer) As Integer
Dim value As Integer
Dim i As Integer
Dim j As Integer
value = 0
For i = (row - 1) To (row + 1)
If (i >= 0 And i <= 8) Then
For j = (column - 1) To (column + 1)
If (j >= 0 And j <= 8) Then
If bomb((i * 9) + j) = True Then
value = value + 1
End If
End If
Next j
End If
Next i
checkValue = value
End Function
Sub storeResult(ByVal status As String)
Open stats For Append As #1
Select Case status
Case "loss"
Print #1, "l"; time
Case "win"
Print #1, "w"; time
End Select
Close #1
gameState = False
End Sub
I hope you can help, thanks in advance.