i created this game recently....there are five rows and five columns. For each row is 0 to 4, and for each columns is 0 to 4. When the game starts ,buttons in column 1 to 3 and row 1 to 3 are all in random colors(Red or White). The goal for this game is to let the player click buttons until all of the buttons turns to either Red or White. However, I put the checkForWin() under the ClickHandler() , but the buttons can't be disable even though they are all in the same color. Also, it tells me that "Object reference not set to an instance of an object." Can someone please check it, and let me know what i am doing wrong with it? Thank you very much, Sincerely
Public Class Form1
Const ROWSIZE As Integer = 5
Const COLSIZE As Integer = 5
Dim gameButtons(,) As Button = New Button(ROWSIZE, COLSIZE) {}
Dim mintMoves As Integer = 0
Dim rndGenerator As New Random(System.DateTime.Now.Millisecond)
Dim myRnd As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim row As Integer
Dim col As Integer
Dim btnWidth As Integer = 50
Dim btnHeight As Integer = 50
'instantiate all the array buttons
For row = 0 To ROWSIZE - 1
For col = 0 To COLSIZE - 1
gameButtons(col, row) = New Button()
'use the Addhandler keyboard to book an event handler
'to an event for each array button
AddHandler gameButtons(col, row).Click, AddressOf ClickHandler
If row >= 1 And row <= 3 And col >= 1 And col <= 3 Then
Me.Controls.Add(gameButtons(col, row))
gameButtons(col, row).Size() = New Size(New Point(btnWidth, btnHeight))
gameButtons(col, row).Location() = New Point((col - 0.8) * btnWidth, (row - 0.8) * btnHeight)
gameButtons(col, row).Name = "btn" + Convert.ToString(col) + "_" + Convert.ToString(row)
End If
Next
Next
Call gameSetup()
End Sub
Private Sub btnreset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
Call gameSetup()
Call enableButtons()
End Sub
Private Sub enableButtons()
Dim row As Integer
Dim col As Integer
For row = 1 To 3
gameButtons(row, col).Enabled = True
Next
For col = 1 To 3
gameButtons(row, col).Enabled = True
Next
End Sub
Private Sub disableButtons()
Dim row As Integer
Dim col As Integer
For row = 1 To 4
gameButtons(row, col).Enabled = False
Next
For col = 1 To 4
gameButtons(row, col).Enabled = False
Next
End Sub
Public Sub ClickHandler(ByVal sender As Object, ByVal e As System.EventArgs)
Dim row As Integer
Dim col As Integer
Dim str As String
str = sender.Name
row = str.Substring(3, 1)
col = str.Substring(5, 1)
swapColor(gameButtons(row, col)) 'swap the color of the button
swapColor(gameButtons(row + 1, col)) 'swap the color above
swapColor(gameButtons(row - 1, col)) 'swap the color below
swapColor(gameButtons(row, col + 1)) 'swap the color to the right
swapColor(gameButtons(row, col - 1)) 'swap the color to the left
mintMoves = mintMoves + 1
lblMoves.Text = Convert.ToString(mintMoves)
Call checkForWin()
End Sub
Private Sub swapColor(ByRef button1 As Button)
If button1.BackColor = Color.Red Then
button1.BackColor = Color.White
Else
button1.BackColor = Color.Red
End If
End Sub
Private Sub checkForWin()
Dim row As Integer
Dim col As Integer
row = row >= 1 And row <= 3
col = col >= 1 And col <= 3
If gameButtons(row, col).BackColor() = Color.Red Then
Call disableButtons()
Else
gameButtons(row, col).BackColor() = Color.White
Call disableButtons()
End If
End Sub
Private Sub gameSetup()
Dim col As Integer
Dim row As Integer
col = rndGenerator.Next(1, 4)
row = rndGenerator.Next(1, 4)
myRnd = rndGenerator.Next(1, 4)
myRnd = row
row = col
col = myRnd
swapColor(gameButtons(row, col))
swapColor(gameButtons(row - 1, col))
swapColor(gameButtons(row + 1, col - 1))
swapColor(gameButtons(row + 1, col))
swapColor(gameButtons(row, col + 1))
myRnd = row
row = col
col = myRnd
swapColor(gameButtons(row - 1, col - 1))
swapColor(gameButtons(row + 1, col + 1))
swapColor(gameButtons(row - 1, col - 1))
swapColor(gameButtons(row + 1, col + 1))
swapColor(gameButtons(row + 1, col - 1))
mintMoves = 0
Me.lblMoves.Text = Convert.ToString(mintMoves)
End Sub
End Class