User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the VB.NET section within the Software Development category of DaniWeb, a massive community of 455,987 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,782 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our VB.NET advertiser: Programming Forums
Views: 448 | Replies: 0
Reply
Join Date: Nov 2007
Posts: 8
Reputation: kidjl is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
kidjl kidjl is offline Offline
Newbie Poster

Help Need help with this game i've created

  #1  
Dec 4th, 2007
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


[code]
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
AddThis Social Bookmark Button
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb VB.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the VB.NET Forum

All times are GMT -4. The time now is 9:30 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC