1

I am currently working on a project in VB and I am having 2 issues, both in the btnPull_Click function.

First is having the 3 wheels mimic the spin of the slot machine. I have the .sleep in there for the delay you would see but I can't seem to get it to flash other images up before finally stopping. I don't think this is a requirement for the project, but I would like to know how to do this.

The MAIN issue I am having is with my if/then statement. I have my arrays(project requirement), but the problem I am running into is that I can't convert an image to a string. I was trying to set NumOfSpins(1,2,3) equal to the Slots(INDEX) and determine from there the value of the payout.

I tried taking my For statement and creating a private function, but I could not get that to work either. I guess I am just really stuck and would appreciate any advice I can get.

Const Slot_Size As Integer = 7
Dim Slots() As Image = {My.Resources.Slot_Cherry, My.Resources.Slot_7, My.Resources.Slot_Bar, My.Resources.Slot_Bell, My.Resources.Slot_Grapes, My.Resources.Slot_Lemon, My.Resources.Slot_Orange}
Dim slotImages() As String = {"cherry", "seven", "bar", "bell", "grapes", "lemon", "orange"}
Dim slotOrder() As Integer = {1, 2, 3}

Private Function RandomNumber(ByVal low As Integer, ByVal high As Integer)
    Randomize()
    Return Convert.ToInt32(((high - low) * Rnd() + low))
End Function

Private Sub Clear()
    For counter = 0 To Slot_Size - 1 Step 1
        Slots(counter) = My.Resources.Slot_Bar
    Next
End Sub

Private Sub btnPull_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPull.Click
    'Spins the 3 slot wheels
    Dim NumOfSpins1 As Integer = RandomNumber(Slot_Size * 5, Slot_Size * 10)
    Dim NumOfSpins2 As Integer = RandomNumber(Slot_Size * 5, Slot_Size * 10)
    Dim NumOfSpins3 As Integer = RandomNumber(Slot_Size * 5, Slot_Size * 10)
    lblBalance.Text = "$" & balance

    For spin = 0 To NumOfSpins1 Step 1
        Slot1.Image = Slots(spin Mod Slot_Size)
    Next
    For spin = 0 To NumOfSpins2 Step 1
        Slot2.Image = Slots(spin Mod Slot_Size)
    Next
    For spin = 0 To NumOfSpins3 Step 1
        Slot3.Image = Slots(spin Mod Slot_Size)
    Next
    Return
    System.Threading.Thread.Sleep(100)
    Slot1.Refresh()
    Slot2.Refresh()
    Slot3.Refresh()

    'Determine if the user wins or loses money
    If NumOfSpins1 = slotImages(0) Then
        balance += 2
    End If

End Sub

End Class

  • 1
    Your for loop are kind of pointless... The PictureBox have a Tag property which is used to store information. Put the image index in it Slot1.Tag = spin Mod Slot_Size then you just need to read the Tag property to get it back. – the_lotus Apr 30 '14 at 20:17
  • 1
    You are using random numbers wrongly. Calling `Randomize()` over and over again will guarantee that the numbers aren’t really random. You’re supposed to call it only once. Your random number calculation is also creating bias. Use the `Random` class instead. – Konrad Rudolph Apr 30 '14 at 20:45
  • Just to be clear with the tag property. I can take my images(Slot_Bell.png, etc) put them in there, then eliminate my For statement and the array I am using for the images? I feel like if I do that I am going to end up being more lost than I currently am. It took me all weekend to make this progress. – user3590742 Apr 30 '14 at 21:06
  • for the image to display, get a random number from 0 to 7; then use a CASE statement in a function to evaluate it and return the appropriate image from My.Resources. Save the value/index also in the Tag prop (as above, save the image **index**). If all this itself is a procedure for one wheel, then a winner is simply `If Wheel1.Tag = Wheel2.Tag AndAlso Wheel1.Tag = Wheel3.Tag`. For the spinning - you could try an image which is all blurred. – Ňɏssa Pøngjǣrdenlarp Apr 30 '14 at 21:11
  • This is done a bit wrong here. If you want to make it good use `backgroundworker`s or even a timer to spin your images. Because right now all three "spin" without any visual refresh. And the thread.sleep doing nothing really. Plus, what you do with random, can be acheved by appropriate .net way: `Dim r As New Random() Dim spins As Integer = r.Next(10, 20)` – T.S. Apr 30 '14 at 21:12
  • I am going to try and figure this out. As far as the RandomNumber() function goes, this was given to us in the rubric for the assignment. I think my professor will be expecting to see it. – user3590742 Apr 30 '14 at 23:37
  • I really hate to ask this, but can show me an example of what you are trying to get me to do? Am I supposed to use a global variable for the tag? We just covered arrays last week and I am still trying to figure out all of the ways to use them. – user3590742 May 01 '14 at 00:08
  • the result of the (non)RandomNumber function is a number from 0 to 7 (or ought to be since that is all the images you have. the right way is `Random.Next(0, 8)`). after the spin, `Slot1.Image = slotImages(n)` and to know what image you put there, `Slot1.Tag = n` or store the image name cherry etc, so you can test for a winner. also, if you make a animated GIF from 3 or 4 of the slot images, the picBoxes will show animation for "spinning" – Ňɏssa Pøngjǣrdenlarp May 01 '14 at 01:41

0 Answers0