I'm building a board game. The game is played on a 4 by 4 grid in pairs, one player at a time. I would really appreciate if you could help me with this. Please help me. I'm new to Visual Basics. It has been only 1 month since I started learnig VB.

I'm using Visual Basics 2010:

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.

The game should maintain the time taken to colour all the squares. Hence, when a player starts the game the timer should also start. You can display the time anywhere within the user interface.

The game should end in two ways. They include: a. A player generating the same random numbers in three consecutive attempts b. A player colouring all the squares

The second player should be able to play the game without closing the whole application. When the game ends for the second player, the winner should be announced. The winner should be the one who scores the highest within the least amount of time. If a player has the highest score but takes the highest time, then the game is considered a tie. Hence, display a message saying it is a tie. Sp far this is what I got:

Public Class Form1

    Private randomRow As Integer
    Private randomCol As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Shared random As New Random()
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        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)
        If stpw.IsRunning Then

            Me.Invoke(Sub()
                          Label1.Text = stpw.Elapsed.ToString("d\ \ hh\:mm\:ss\.ff")
                      End Sub)
        End If

    End Sub
    Dim ClickCount As Integer = 0
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


        Dim rand1 As String
        Dim rand2 As String
        rand1 = TextBox1.Text
        rand2 = TextBox2.Text


        If Not ClickCount = 4 Then
            ClickCount += 1
        Else
            ClickCount = 1
        End If
        Select Case ClickCount
            Case Is = 1
                Button2.BackColor = Color.Green
            Case Is = 2
                Button2.BackColor = Color.Yellow
            Case Is = 3
                Button2.BackColor = Color.Red

        End Select

    End Sub

End Class

I've no idea how to get the random numbers from the Textboxes and use it to locate the buttons. Also I'm having difficulties with the Score keeping. Please help me.. :)

Recommended Answers

All 6 Replies

I'm not sure I follow the logic but I'm going to offer a suggestion to see if that simplifies things. The user is expected to click on a textbox and "Click" is not an event associated with a textbox. It's more of a button thing. My suggestion is to create the grid at runtime and make it a grid of buttons. The buttons can be accessed through an array by the row and column numbers (0-3). Buttons, like textboxes, can have text and you can also change the colour.

Public Class Form1

    Private Grid(3, 3) As Button
    Private btnSize As Integer = 25
    Private btnPosn As New Point(10, 10)

    Private Sub Form1_Load(sender As System.Object, 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

    Private Sub Grid_Click(sender As System.Object, 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 & ")"
        Grid(row, col).BackColor = Color.Red

    End Sub

End Class

Change the values of btnPosn and btnSize to change the size of each button and the position of the top left cell.

commented: Actually It should be able to press only the number specified in the Random Generator. If he clicked the right one colour changes if else no change +0

Thanks for answering my question. It is difficult for me to explain this question. But it goes like this:

The Random Generator will generate 2 numbers. One for rows and other for columns. And when clicking the right button specified in the Random Number the colour should change otherwise it should not.

That's how I really want it... :) if you could help, that would be great.

If you declare class level integers nextRow and nextCol you can generate a new randow row and column by

Private Sub btnNumber_Click(sender As System.Object, e As System.EventArgs) Handles btnNumber.Click

    Dim rnd As New Random()
    Dim clicked As Boolean = True

    Do While clicked
        nextRow = rnd.Next(0, 4)
        nextCol = rnd.Next(0, 4)
        clicked = Grid(nextRow, nextCol).BackColor = Color.Red
        Me.Text = nextRow & "," & nextCol
    Loop

End Sub

The loop generates new pairs until it finds a pair for a cell that has not yet been clicked. You can modify the grid click handler to flip the colours only if the correct cell was clicked. You could even add

btnNumber.PerformClick()

as the last line in the grid click handler to automatically generate the next random pair.

By the way, Microsoft, for some insane reason, decided that the Next method would generate a random number which is greater than or equal to the lower bound but only less than the upper bound. This is a completely brain dead decision (IMHO). Thus rnd.Next(0,4) generates a number from 0 to 3.

commented: It's working... Thanks a lot... :) +0

Thus rnd.Next(0,4) generates a number from 0 to 3.

Looks "logical" to me. Think most pseudo random generators use MOD somewhere in their formula and produce numbers between 0 and 0.99999999..

One more thing, I'm unable to change the colour of the square represented by the random numbers only. How can I solve this problem?

Grid(nextRow,nextCol).BackColor = Color.Red
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.