Hack Forums
[.NET] Visual basic 2010 Express need help. - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Visual basic 2010 Express need help. (/showthread.php?tid=5187288)



Visual basic 2010 Express need help. - HackFourmIsHelpful - Feb 28, 2016

Alright so we have been doing programs in my computer class, and I can't do this right now, I am trying my best and I know HF will not give me on how to do it, so I would like help. We are to make a slot machine game. This picture looks like this: https://imgur.com/QmFSy1O
It says this in the book: Create a BookMachineGame application that acts as a simple slot machine. The user starts with 100 tokens. With each "pull" of the handle, the user loses 1 token and the computer "spins" three wheels, each consisting of the numbers 1,2, and 3. If all three numbers are 1, the user get 4 tokens; if all are 2 the user gets 8 tokens and if the user get 3 the user gets 12 tokens. The number of tokens that the user has should be displayed on the form and the result of the spin should be displayed in a message box.

Any help? I know I should have: Dim Token as Integer
Dim tokenlost as integer
that's all I got, I am so lost.


* - My Computer - Feb 28, 2016

Basically in order to achieve this you must do this.

1. Have an integer that stores the token (since you said they start with 100 tokens the default value of this integer will be 100):

Code
Dim Token as Integer = 100

2. Subtract 1 from the 'Token' integer each time the "Pull" button is clicked.

Code
Token = Tokens - 1

3. After you subtract 1, you should use the Random class to get a random number.

Code
Dim R As Random = New Random()
Dim FirstValue As Integer = R.Next(1, 4)
Dim SecondValue As Integer = R.Next(1, 4)
Dim ThirdValue As Integer = R.Next(1, 4)

What this code does is that it generates a random number for each of these variables.

3. Have a Boolean and check if all three labels or variables are matching. If they are then the Boolean will be true, else false.

Code
Dim FirstValue As Integer = 1
        Dim SecondValue As Integer = 1
        Dim ThirdValue As Integer = 1

        Dim Matching As Boolean = False

        If FirstValue = SecondValue AndAlso SecondValue = ThirdValue Then
            Matching = True
            MessageBox.Show("They are matching!")
        Else
            Matching = False
            MessageBox.Show("They are NOT matching!")
        End If

The output should be that "They are matching!".

4. Check if the Boolean is true, if it is then take the first number of your label or variable and according to the number tell the user how many tokens they got and display it to them.

Code
If Matching Then
            If FirstValue = 1 Then
                MessageBox.Show("You scored 4 tokens!")
            ElseIf FirstValue = 2 Then
                MessageBox.Show("You scored 8 tokens!")
            ElseIf FirstValue = 3 Then
                MessageBox.Show("You scored 12 tokens!")
            Else
                MessageBox.Show("Unknown number...")
            End If
        Else
            MessageBox.Show("Uh oh! They were not matching so you got 0 tokens!")
        End If

5. Add the tokens to the 'Tokens' integer? (This will be easy, just add it in the source code I provided above this step.).

If this is not clear enough, want a better explanation, or you have another problem then just ask.


* - VmX5 - Feb 28, 2016

(Feb 28, 2016 09:58 PM)EggsAndToast Wrote:  2. Subtract 1 from the 'Token' integer each time the "Pull" button is clicked.

Code
Token = Tokens - 1


Easier :

Code
Token -= 1

Just a tip ;)


* - My Computer - Feb 28, 2016

(Feb 28, 2016 10:12 PM)VirusCrafter5 Wrote:  Easier :

Code
Token -= 1

Just a tip ;)

Ah yes, forgot that I could of done this. Thanks for reminding me. 😝


* - Odus - Feb 29, 2016

(Feb 28, 2016 09:58 PM)EggsAndToast Wrote:  
Code
Dim R As Random = New Random()

Take the shorthand method instead of declaring and instantiating separately:
Code
Dim r As New Random

Casing for local variables should be camel, not pascal. I'd also redo this bit:
(Feb 28, 2016 09:58 PM)EggsAndToast Wrote:  
Code
Dim Matching As Boolean = False

        If FirstValue = SecondValue AndAlso SecondValue = ThirdValue Then
            Matching = True
            MessageBox.Show("They are matching!")
        Else
            Matching = False
            MessageBox.Show("They are NOT matching!")
        End If

To:

Code
Dim match As Boolean = first = second AndAlso second = third

If match is false, then display the message box and return. If not, continue. Cleaner that way and you're not displaying a redundant message box. + I think you've been a bit too generous for a homework assignment.


* - HackFourmIsHelpful - Feb 29, 2016

Thanks guys for all the help! I really appreciated that.


* - Ninja - Feb 29, 2016

(Feb 28, 2016 09:58 PM)EggsAndToast Wrote:  Basically in order to achieve this you must do this.

1. Have an integer that stores the token (since you said they start with 100 tokens the default value of this integer will be 100):

Code
Dim Token as Integer = 100

2. Subtract 1 from the 'Token' integer each time the "Pull" button is clicked.

Code
Token = Tokens - 1

3. After you subtract 1, you should use the Random class to get a random number.

Code
Dim R As Random = New Random()
Dim FirstValue As Integer = R.Next(1, 4)
Dim SecondValue As Integer = R.Next(1, 4)
Dim ThirdValue As Integer = R.Next(1, 4)

What this code does is that it generates a random number for each of these variables.

3. Have a Boolean and check if all three labels or variables are matching. If they are then the Boolean will be true, else false.

Code
Dim FirstValue As Integer = 1
        Dim SecondValue As Integer = 1
        Dim ThirdValue As Integer = 1

        Dim Matching As Boolean = False

        If FirstValue = SecondValue AndAlso SecondValue = ThirdValue Then
            Matching = True
            MessageBox.Show("They are matching!")
        Else
            Matching = False
            MessageBox.Show("They are NOT matching!")
        End If

The output should be that "They are matching!".

4. Check if the Boolean is true, if it is then take the first number of your label or variable and according to the number tell the user how many tokens they got and display it to them.

Code
If Matching Then
            If FirstValue = 1 Then
                MessageBox.Show("You scored 4 tokens!")
            ElseIf FirstValue = 2 Then
                MessageBox.Show("You scored 8 tokens!")
            ElseIf FirstValue = 3 Then
                MessageBox.Show("You scored 12 tokens!")
            Else
                MessageBox.Show("Unknown number...")
            End If
        Else
            MessageBox.Show("Uh oh! They were not matching so you got 0 tokens!")
        End If

5. Add the tokens to the 'Tokens' integer? (This will be easy, just add it in the source code I provided above this step.).

If this is not clear enough, want a better explanation, or you have another problem then just ask.

Just a few remarks, the last if statement could be way shorter, and also you shouldn't declare a new random object each time, rather use the same one each time and randomize every now and then.


* - My Computer - Feb 29, 2016

(Feb 29, 2016 03:30 PM)Tyranitar Wrote:  Just a few remarks, the last if statement could be way shorter, and also you shouldn't declare a new random object each time, rather use the same one each time and randomize every now and then.

Yea I know, the code I posted was not the greatest. I just quickly typed it up to give OP a general idea on what he has to do to achieve his assignment.