Question

So I am building a Memory Game. This is a 2 part question. I have 16 boxes being used and need to have the numbers 1-8 twice. I have set up each box as a structure and have a random number generator randomly picking a number for a box and then randomly placing a number 1 - 8 into it. Problem I am coming across is this. I am getting repeat numbers for my boxes and more than 2 times for a digit that will be placed in that box, sometimes no sequential number being used all together. How can I make sure all 16 boxes are being created with out having to make code for 16 different instances. Also need to make sure each single digit number between 1-8 is being used and only used twice?

Second part of my question is I am having an issue with making a click event when a user chooses a box. Right now I can't figure out how to associate which box the user clicks on. I don't want to write code for 16 different boxes, link an array to each box and then populate each box that array's guess number. Is there a way I can simplify that code down to something short?

I have included all the code so far.

Option Strict On
Option Explicit On
Option Infer Off

Public Class Form1

    Structure MemoryBox
        Public intBox As Integer
        Public intGuess As Integer
        Public strGuess As String
    End Structure

    Private gameScore() As Integer

    Private memoryGame(15) As MemoryBox

    Dim countLoad As Integer = 0
    Dim countScore As Integer = 0
    Dim intScore As Integer

    Private Sub LoadGame()

        'this is where I am using the random numbers to make each box and populate it with a guess
        Dim randomGen As New Random
        Dim intMemBox As Integer
        Dim intMemGuess As Integer

        intScore = 0


        If countLoad <= 15 Then

            intMemBox = randomGen.Next(1, 16)
            intMemGuess = randomGen.Next(1, 8)

            memoryGame(countLoad).intBox = intMemBox
            memoryGame(countLoad).intGuess = intMemGuess
            memoryGame(countLoad).strGuess = intMemGuess.ToString

            countLoad += 1
        End If

    End Sub

    Private Sub GuessClick()

        'trying to use this area for click event for each box click

        lblMemory1.BackColor = Color.Green
        lblMemory1.Text = memoryGame()

        intScore += 1

        lblScore.Text = intScore.ToString

    End Sub

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

        Call LoadGame()

    End Sub

    Private Sub btnNewGame_Click(sender As Object, e As EventArgs) Handles btnNewGame.Click

        gameScore(countScore) = intScore
        countScore += 1

        Dim outFile As IO.StreamWriter

        outFile = IO.File.AppendText("score.txt")
        outFile.WriteLine(intScore)

        outFile.Close()

        Call LoadGame()

    End Sub

    Private Sub btnHighScore_Click(sender As Object, e As EventArgs) Handles btnHighScore.Click

        Array.Sort(gameScore)
        lblHighScore.Text = gameScore(0).ToString

    End Sub

    Private Sub btnAllScores_Click(sender As Object, e As EventArgs) Handles btnAllScores.Click

        Dim inFile As IO.StreamReader
        Dim strInfo As String

        If IO.File.Exists("score.txt") Then
            inFile = IO.File.OpenText("score.txt")
            Do Until inFile.Peek = -1
                strInfo = inFile.ReadLine
            Loop
            inFile.Close()

        Else
            MessageBox.Show("Can't find the score.txt file", "High Score", MessageBoxButtons.OK, MessageBoxIcon.Information)

        End If

        MessageBox.Show(strInfo, "All Scores", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub
End Class
Was it helpful?

Solution

One option is to populate an array with the numbers you want, then randomly shuffle it. Use a loop that runs at least one times the length of the array and use the iterator value as one index and swap it with a randomly chosen index.

Something like this:

Dim memoryGame(15) As MemoryBox
Dim randomarray() As Integer = {1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8}
Dim rnd As New Random(Now.Millisecond)
For I = 0 To 15
    Dim randindex As Integer = rnd.Next(0, 16)
    Dim temp As Integer = randomarray(I)
    randomarray(I) = randomarray(randindex)
    randomarray(randindex) = temp
Next
For I = 0 To 15
    memoryGame(I).intBox = randomarray(I)
Next

From here simply iterate through the array and assign the values to your boxes

To handle the click events:

Use the same handler for each box. One way is to add a Handles clause for each box. With 16 boxes this could get a little clunky. I would suggest in the load event handler iterate through the boxes and use the AddHandler statement to add the handler to each box. At this point sender will always point to the box that was clicked. It is simply a matter of casting sender as whatever type the box is, to access all the properties of the box.

If the boxes are buttons(Box1, Box2, etc.) it would look something like this:

For Each b As Button In Me.Controls.OfType(Of Button).Where(Function(x) x.Name.StartsWith("Box"))
    AddHandler b.Click, AddressOf Button_Click
Next

Private Sub Button_Click(sender As Object, e As EventArgs)
    Dim clickedbutton As Button = DirectCast(sender, Button)
    'access the properties here
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top