Rudi
  • Home
  • About
  • Contact
  • VB.Net
    • Flippin
    • Calculator
    • Mine Scout
    • Slot Machine
    • Snake
    • Find Words
    • Accounting using Child forms
  • Excel
    • Calendar 1
    • Calendar 2
    • Sudoku
    • Mine Scout
    • Tilexcel
    • ExceLudo
    • Alphabet Game
  • C# Child Forms
  • C# Struct Array

Slot Machine
with a VB.Net Control Array

This project employs an Object or Control Array. It works just as it did with VB6 although the programming is slightly different to achieve it.
To demonstrate this, let's create an 'array' of 15 Labels. We can then loop through the Labels using the statement ("Label" & Index). In VB6 it was Label(Index).

Just like with VB6 the name of the label has to be the same.

That's actually incorrect as every object has to have a unique name even in VB6. Here I meant the first part has to be the same just like VB6. But actually the objects can have any name and we can treat them like an array and loop through them. Jump to the end and I'll explain.

VB.Net unfortunately only applies the default name to all copies but we can rename them to Income1 to Income15 if desired.

Picture

In this project we'll start with a global array of 6 colours.
 
Dim clr() As Color = {Color.Aquamarine, Color.Green, Color.Red, Color.Blue, Color.Yellow, Color.Violet}

Now we'll loop through the array of labels to randomly colour them in the Form_Load procedure.

  Private Sub frmSlotMC_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Randomize()
    For i As Integer = 1 To 15
      Me.Controls("Label" & i).BackColor = clr(Rnd() * 5)
    Next
  End Sub

Picture

Add more items to the form so the Slot Machine can be played and the label array put towards some use.

Picture

  Private Async Sub btnSpin_Click(sender As Object, e As EventArgs) Handles btnSpin.Click

    Randomize()

    Dim intReel As Integer
    Dim intReelColor(6) As Integer

    'Subtract points for spin
    lblGameScore.Text = (CInt(lblGameScore.Text) - 20).ToString
    lblResult.Text = ""
    lblSpinScore.Text = ""

    'Random number of spins but at least 12
    For spins As Integer = 1 To 12 + Int(5 * Rnd())

      'Spin the reels
      For intReel = 2 To 14 Step 3

        Me.Controls("Label" & (intReel + 1)).BackColor = Me.Controls("Label" & (intReel)).BackColor 'move colours from row 2 to3
        Me.Controls("Label" & (intReel)).BackColor = Me.Controls("Label" & (intReel - 1)).BackColor 'move colours from row 1 to 2
        Me.Controls("Label" & (intReel - 1)).BackColor = clr(Int(6 * Rnd())) 'new colours in row 1

        Await Task.Delay(30)

      Next
    Next

    'Scoring
    Dim intColorScore(5) As Integer
    Dim intColor As Integer
    Dim intScore As Integer

    'Count each of the colours along the centre
    For intReel = 2 To 14 Step 3
      For intColor = 0 To 5
        If Me.Controls("Label" & intReel).BackColor = clr(intColor) Then
          intColorScore(intColor) += 1
        End If
      Next
    Next

    'Scores
    For intColor = 0 To 5

      If intColorScore(intColor) = 2 Then  intScore += 10 : lblResult.Text = "A pair!"
      If intColorScore(intColor) = 3 Then  intScore += 25 : lblResult.Text = "A triple!"
      If intColorScore(intColor) = 4 Then  intScore += 60 : lblResult.Text = "A foursome!"
      If intColorScore(intColor) = 5 Then  intScore += 150 : lblResult.Text = "Royal flush!"
      End If

    Next

    'Bonuses
    If intScore = 20 Then intScore = 30 : lblResult.Text = "2 pairs!"
    If intScore = 35 Then intScore = 55 : lblResult.Text = "Full house!"
 
    lblSpinScore.Text = intScore.ToString
    lblGameScore.Text = (CInt(lblGameScore.Text) + intScore).ToString

  End Sub

Picture

If desired, add an Event handling procedure for clicking an individual label. One way is to add a line to the Form_Load procedure.

  Private Sub frmSlotMC_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Randomize()
    For i As Integer = 1 To 15
      Me.Controls("Label" & i.ToString).BackColor = clr(Rnd() * 5)
      'Add this line
      AddHandler Me.Controls("Label" & i.ToString).Click, AddressOf Me.myLabel_click
    Next
  End Sub

Then add the event procedure.

  Sub myLabel_click(sender As Object, e As System.Windows.Forms.MouseEventArgs)
    MsgBox("My colour is " & sender.backcolor.name)
  End Sub

Picture

Loop through an array of objects with different names
At the start I proclaimed that it's possible to loop through objects where the name of each object is different. To do this, we'll place the names of the object array into a string array and then the loop can reference the name of the objects from this string array during the loop.

In this demonstration there are 3 labels with different names.

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

    Dim arrLabels() As String = {"lblFirst", "lblSecond", "lblThird"}

    Dim x As Integer

    For x = 0 To 2

     'Fetch the name of the object to manipulate out of the string array
      Me.Controls(arrLabels(x)).Text = "This works for " & arrLabels(x)

    Next

  End Sub
Picture
Picture

The loop above can handle different types of objects but they each must have a Text property in this example.

Cheers.

Proudly powered by Weebly