r/visualbasic • • Sep 06 '15

Board Game Visual Basics 2010

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
5 Upvotes

1 comment sorted by

1

u/DatMarv Sep 10 '15

When I create board games, I usually don't work with the "tag" property. Furthermore, I recommend not using a 2-dimensional array of buttons, but rather adding buttons in your program's runtime.

Private Sub CreateGrid() 
    For i = 1 To 16 
        Dim nb as new Button() 
        nb.name = "b" & i 
        nb.size = new Size(btnSize, btnSize) 
        nb.Left= btnSize + i*btnSize - Int((i - 1) / 4) * btnSize * 4 
        nb.Top= btnSize + Int((i - 1) / 4) * btnSize
        Controls.Add(nb)                       
        AddHandler nb.Click, AddressOf Grid_Click 
    Next 
End Sub

This way, you can adress each button by its name: Controls("b" & 10).Backcolor = Color.Black changes the Color of the Button in the 3rd row and 2nd col ( You just count "linewise")

Private Sub Grid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
    Dim curr as Integer = Mid(sender.name, 2) 
    Dim row as Integer = Int((curr - 1) / 4) + 1 
    Dim col as Integer = ((curr - 1) mod 4) + 1
    If row = randomRow And col = randomCol Then
         sender.backColor = Color.Black
    End If
End Sub

I don't know much about Threading in VB, so I suggest you leave out

Dim elapTimer As New Threading.Timer(AddressOf tick, Nothing, 1000, 1000) 
Dim stpw As Stopwatch = Stopwatch.StartNew

and just add a Timer Control in the Toolbox in the Form. In the Properties, you can set the Intervall(ms) of the Timer and set the Enabled property to True. If you set its Intervall to 1000, just add in its code

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick 
    Label1.Text += 1 'just seconds, but w/e 
 End Sub

Do the Button1 Event as following

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim rnd as new Random 
    randomRow = rnd.next(1,5) 
    Textbox1.Text = randomRow 
    randomCol = rnd.next(1,5) 
    Textbox2.Text = randomCol 'Why did you use a for loop? 
End Sub

Tell me if it works!