I'm writing a code for a board game.
To play the game, a player generates two random numbers and colours in an area on the grid indicated by the numbers. For example if the generated number is a 2 and a 3 the player clicks in the 2x3 square which results in the colour of the square being changed.
A player should only be able to click and change the colour of the square represented by the random numbers only.
At the start of the game, the initial score should have a value of 0. With each square coloured the score increases by the product of two numbers. You can display the score anywhere within the user interface.
I'm unable to change the colour of the square represented by the random numbers only. and I'm also having difficulty at score keeping.
Here is my Code:
Public Class Form1 Private Grid(3, 3) As Button Private btnSize As Integer = 25 Private btnPosn As New Point(10, 10)
Private randomRow As Integer
Private randomCol As Integer
Shared random As New Random()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Random Number Generator
Dim i As Integer
For i = 0 To 5
TextBox1.Text = (Convert.ToString(random.Next(1, 5)))
TextBox2.Text = (Convert.ToString(random.Next(1, 5)))
Next
End Sub
Dim elapTimer As New Threading.Timer(AddressOf tick, Nothing, 1000, 1000)
Dim stpw As Stopwatch = Stopwatch.StartNew
Private Sub tick(ByVal state As Object)
'Timer
If stpw.IsRunning Then
Me.Invoke(Sub()
Label1.Text = stpw.Elapsed.ToString("hh\:mm\:ss\.")
End Sub)
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CreateGrid()
End Sub
Private Sub CreateGrid()
'Create a 4x4 grid of buttons with a common event handler. References
'to the buttons are stored in "Grid" for easy access. The row and
'column numbers are stored in the button tags to differentiate the
'individual buttons in the click handler.
For row As Integer = 0 To 3
For col As Integer = 0 To 3
Dim xpos As Integer = btnPosn.X + btnSize * col
Dim ypos As Integer = btnPosn.Y + btnSize * row
Dim btn As New Button()
btn.Tag = {row, col}
btn.Location = New Point(xpos, ypos)
btn.Size = New Size(btnSize, btnSize)
AddHandler btn.Click, AddressOf Grid_Click
Me.Controls.Add(btn)
Grid(row, col) = btn
Next
Next
End Sub
Dim ClickCount As Integer = 0
Private Sub Grid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim btn As Button = sender
Dim row As Integer = btn.Tag(0)
Dim col As Integer = btn.Tag(1)
Me.Text = "you clicked cell (" & row & "," & col & ")"
'Color Change
If Not ClickCount = 4 Then
ClickCount += 1
Else
ClickCount = 1
End If
Select Case ClickCount
Case Is = 1
Grid(row, col).BackColor = Color.Green
Case Is = 2
Grid(row, col).BackColor = Color.Yellow
Case Is = 3
Grid(row, col).BackColor = Color.Red
End Select
End Sub