A VB Price Is Right Game (Spin the wheel)

Status
Not open for further replies.

lazerman

Fully Optimized
Messages
1,842
Okay so we just got our final project for this semester in Programming 1....

We have a choice out of what type of project we want to do right now, I chose this one, it seemed the simplest. Here is the description:

Two players compete in a spinning of the "Big Wheel" in an attampt to get as close to $1.00 without going over. A player spins once, then decides if he/she wants to spin a second time and add the two spins together. The amounts spun are randomly generated by the computer.

Okay so the program isn't graphic based, it is only text so that makes it easier. I am stuck on what to do lol.

This is what I have so far:
Code:
Option Explicit
Private dblValue As Double, dblValue2 As Double

Private Sub Form_Load()
    Randomize     'Randomizes numbers spun
End Sub

Private Sub cmdSpin_Click()
    dblValue = Rnd()     'Generates amount of first spin
End Sub

Private Sub cmdSpin2_Click()
    dblValue2 = Rnd()     'Generates amount of second spin
End Sub

Private Sub cmdDone_Click()
    Unload Me     'Ends Game
End Sub

Private Sub cmdNames_Click()
Dim strName As String
Dim intCount As Integer

intCount = 1     'Starts loop at 1

Do While intCount <= 2
intCount = intCount + 1
strName = InputBox("Enter player one's name and press ok, then enter player two's name and press ok.")
Loop

lblPlayers.Caption = "Player's names: " & strName     'Displays player's names
End Sub

This is all just 10 minutes of work.... It isn't much because I really don't know how to do this program.

My issues, how do I get strName to retain two names entered into the same input box during a loop lasting 2 times?

How do I generate the random numbers required to be correct? Like the range should be 0-100 step 5.

How do I switch between players?

Am I going to get this done in three weeks?

Please help me out before I go insane lol. :eek:
 
Which version of VB is that? It looks Pre-.NET, so is it VB 6?

Anyway, the problem with retaining the two player names is that you are overwriting the same variable.

You do not need to use a loop. You need perhaps, playerName1 and playerName2.

I would advise against using "Hungarian Notation" in a language like VB. Hungarian Notation refers to prefacing your variable names with their types, e.g. strName for a String, and dblValue for a Double.

Also, I am not familiar with the specifics of the Rand() function in VB, but I seem to recall it being a number between 0.00 and 1.00. You could simply multiply this number by 100 to get the range you're looking for.

If, by chance, it is like C++/etc, and it generates a number between 0 and MAX_INT, you would do Rand() % 100. (Where % is the Modulus operator).
 
Well I just chose String because I couldn't think of anything else. Yes it is VB 6.

I will post on monday what the random number code is.... It's at school.

Edit: Nvm, the random number code is intValue=Int(Rnd*100)+1 Step 5

I will go try that out real quick.

Edit again: The Step 5 didn't work, so how would I accomplish that?
 
Here is the revised code:

Code:
Option Explicit
Private intValue1 As Integer, intValue2 As Integer

Private Sub Form_Load()
    Randomize     'Randomizes numbers spun
End Sub

Private Sub cmdSpin_Click()
    intValue1 = Int(Rnd * 100) + 1     'Generates amount of first spin
    lblValue1.Caption = "Value of first spin: " & intValue1 'Displays value of first spin
End Sub

Private Sub cmdSpin2_Click()
    intValue2 = Int(Rnd * 100) + 1     'Generates amount of second spin
    lblValue2.Caption = "Value of second spin: " & intValue2     'Displays value of second spin
    lblValue3.Caption = "Total value of both spins: " & (intValue1 + intValue2)     'Displays total value of both spins
End Sub

Private Sub cmdDone_Click()
    Unload Me     'Ends Game
End Sub

Private Sub cmdPlayer1Name_Click()
    Dim strNameP1 As String
    
    strNameP1 = InputBox("Enter player one's name and then press ok.")     'Creates an inputbox for user to enter his or her name
    
    lblPlayer1.Caption = "Player one's name: " & strNameP1     'Displays player one's name
End Sub

Private Sub cmdPlayer2Name_Click()
Dim strNameP2 As String
    
    strNameP2 = InputBox("Enter player two's name and then press ok.")     'Creates an inputbox for user to enter his or her name
    
    lblPlayer2.Caption = "Player one's name: " & strNameP2     'Displays player two's name
End Sub
 
Wow. Lol I look at the code above and I smile to think I did all of this in 4 hours last night:

Code:
Option Explicit
Private dblValue1 As Double, dblValue2 As Double, strNameP1 As String, strNameP2 As String, dblValue3 As Double, dblValue4 As Double, dblTotal1 As Double, dblTotal2 As Double, dblX As Double

Private Sub Form_Load()
    Randomize     'Randomizes numbers spun
End Sub

Private Sub cmdPlayer1Name_Click()
    strNameP1 = InputBox("Enter player one's name and then press ok.")     'Creates an inputbox for player 1 to enter his or her name
    
    lblPlayer1.Caption = "Player one's name: " & strNameP1     'Displays player 1's name
    
    cmdPlayer1Name.Visible = False     'Removes the option to enter player 1's name again
    
    cmdPlayer2Name.Visible = True     'Gives player 2 the option to enter their name
    
    lblPlayer1.Visible = True     'Displays the name player 1 entered
End Sub

Private Sub cmdPlayer2Name_Click()
    strNameP2 = InputBox("Enter player two's name and then press ok.")     'Creates an inputbox for player 2 to enter his or her name
    
    lblPlayer2.Caption = "Player two's name: " & strNameP2     'Displays player 2's name
    
    cmdPlayer2Name.Visible = False     'Removes the option to enter player 2's name again
    
    lblPlayer2.Visible = True     'Displays the name player 2 entered
    
    MsgBox "Spin the wheel by pressing the spin button, " & strNameP1 & "."     'Tells player 1 what to do after entering names
    
    cmdSpin1A.Visible = True     'Gives player 1 the option to spin
    
    lblTurn.Caption = "It's " & strNameP1 & "'s turn!"     'Displays player 1's name on the who's turn is it label
End Sub

Private Sub cmdStart_Click()
    imgBob.Visible = False
    
    MsgBox "Please enter both players' names by clicking the appropriate buttons."     'Allows players to enter their names
    
    cmdStart.Visible = False     'Removes start button from screen so it can't be pressed again
    
    cmdPlayer1Name.Visible = True     'Displays Enter player 1's name button on screen
End Sub

Private Sub cmdSpin1A_Click()
    dblValue2 = 0     'Sets intValue 2 for spin 2 to 0 in case player 1 only spins once
    
    dblX = Int(Rnd * 20)     'Generates amount of first spin
    
    dblValue1 = 0.05 * dblX + 0.05
    
    MsgBox "You have a value of " & (dblValue1 * 100) & " cents for your first spin."   'Message box telling player 1 his or her first spin value
    
    lblValue1.Caption = "Value of first spin: " & (dblValue1 * 100)     'Displays value of first spin by player 1 on a label
    
    lblValue1.Visible = True     'Makes the first spin value for player 1 visible
    
    dblTotal1 = (dblValue1 + dblValue2)     'Calculates the total for both spins; for only one spin, the total is intValue1
    
    lblValue3.Caption = "Total value of both spins: " & (dblTotal1 * 100) & " cents." 'Displays the total value of both spins
    
    lblValue3.Visible = True
    
    If dblValue1 <= 0.95 Then    'Checks to see if player 1 has less than a dollar, trying to determine if the user needs to spin again
        MsgBox "Would you like to spin again? If so, press the Spin it Again button. If not, press the Next Player button"     'Tells user what to do next
        cmdSpin1A.Visible = False     'Takes away first spin option
        cmdSpin2A.Visible = True     'Gives user a choice to spin again
        cmdNext.Visible = True     'Gives user the choice to pass their second spin
    End If
    If dblValue1 = 1 Then
        MsgBox strNameP1 & " WINS!" 'Checks to see if player 1 has 1 dollar on first spin, if so he or she wins automatically
        End
    End If
End Sub

Private Sub cmdSpin2A_Click()
    dblX = Int(Rnd * 20)     'Allows dblx to increment
    
    dblValue2 = 0.05 * dblX + 0.05     'Makes the increment 0.05 to match the wheel on the show
    
    MsgBox "You have a value of " & (dblValue2 * 100) & " cents for your second spin."   'Message box telling player 1 his or her second spin value
    
    lblValue2.Caption = "Value of second spin: " & (dblValue2 * 100) 'Displays the value of the second spin for player 1
    
    lblValue2.Visible = True     'Makes the second spin value for player one visible
    
    dblTotal1 = (dblValue1 + dblValue2)     'Calculates the total for both spins, sum of dblValue1 plus dblValue2
    
    lblValue3.Caption = "Total value of both spins: " & (dblTotal1 * 100) & " cents."  'Displays the total value of both spins
    
    lblValue3.Visible = True     'Makes the total value label visible
    
    If dblTotal1 > 1 Then    'Checks to see if player 1 went over 1 dollar, in which case player 2 wins automatically
        MsgBox "You went over a dollar " & strNameP1 & ", you lose; " & strNameP2 & " wins."     'Informs player 1 that they have lost
        End     'Ends the game in the event player 2 wins due to player 1 going over 1 dollar total value
    End If
    
    If dblTotal1 = 1 Then
        MsgBox strNameP1 & " WINS!" 'Checks to see if player 1 has 1 dollar on first spin, if so he or she wins automatically
        End
    End If
    MsgBox "Please press the Next Player button."     'Tells player 1 to press the next button
    
    cmdSpin2A.Visible = False     'Removes Spin it Again option
End Sub

Private Sub cmdNext_Click()
    lblValue1.Visible = False     'Removes player 1's first spin value
    
    lblValue2.Visible = False     'Removes player 1's second spin value
    
    lblValue3.Visible = False     'Removes player 1's total spin value for both spins
    
    lblTurn.Caption = "It's " & strNameP2 & " turn."     'Changes the label that displays who's turn it is
    
    cmdNext.Visible = False     'Removes option for player 2 to press Next Player
    
    MsgBox "Spin the wheel by pressing the spin button, " & strNameP2 & "."     'Tells user what to do next
    
    cmdSpin1B.Visible = True     'Makes the option for player 2 to spin for the first time
End Sub

Private Sub cmdSpin1B_Click()
    dblValue4 = 0     'Sets intValue4 for spin 2 to 0 in case player 2 only spins once
    
    dblX = Int(Rnd * 20)     'Allows dblx to increment
    
    dblValue3 = 0.05 * dblX + 0.05     'Makes the increment 0.05 to match the wheel on the show
    
    MsgBox "You have a value of " & (dblValue3 * 100) & " cents for your first spin."   'Message box telling player 2 his or her first spin value
    
    lblValue1.Caption = "Value of first spin: " & (dblValue3 * 100)     'Displays the value of the first spin for player 2
    
    lblValue1.Visible = True     'Makes label for player 2's first spin value visible
    
    dblTotal2 = (dblValue3 + dblValue4)     'Calculates the total for both spins; for only one spin, the total is dblValue3
    
    lblValue3.Caption = "Total value of both spins: " & (dblTotal2 * 100) & " cents." 'Displays the total value of both spins
    
    lblValue3.Visible = True
    
    If dblValue1 <= 0.95 Then    'Checks to see if player 2 has less than a dollar, trying to determine if the user needs to spin again
        MsgBox "Would you like to spin again " & strNameP2 & "? If so, press the Spin it Again button. If not, press the Who Won button to see who won."     'Tells user what to do next
        cmdSpin1B.Visible = False     'Hides first spin option for player 2
        cmdSpin2B.Visible = True     'Makes the second spin option for player 2 visible
        cmdWon.Visible = True     'Gives user the choice to use the value for their first spin as their total value and see who won
    End If
    If dblTotal2 = 1 Then
        MsgBox strNameP2 & " WINS!"     'Checks to see if player 2 has 1 dollar on first spin, if so he or she wins automatically
        End
    End If
End Sub

Private Sub cmdSpin2B_Click()
    dblX = Int(Rnd * 20)     'Allows dblx to increment
    
    dblValue3 = 0.05 * dblX + 0.05     'Makes the increment 0.05 to match the wheel on the show
    
    MsgBox "You have a value of " & (dblValue4 * 100) & " cents for your second spin."     'Message box telling player 2 his or her second spin value
    
    lblValue2.Caption = "Value of second spin: " & (dblValue4 * 100)    'Displays the value of the second spin for player two
    
    lblValue2.Visible = True     'Makes the label for the second spin value visible
    
    dblTotal2 = (dblValue3 + dblValue4)     'Calculates the total for both spins, sum of intValue3 plus intValue4
    
    lblValue3.Caption = "Total value of both spins: " & (dblTotal2 * 100) & " cents."  'Displays total value of both spins for player 2
    
    lblValue3.Visible = True     'Makes the total value label for both spins visible
    
    cmdSpin2B.Visible = False     'Removes option for player 2 to spin again
    
    If dblTotal2 > 1 Then      'Checks to see if player 2 went over 1 dollar, in which case player 1 wins automatically
        MsgBox "You went over a dollar " & strNameP2 & ", you lose; " & strNameP1 & " wins."     'Informs player 2 that they have lost
        End     'Ends the game in the event player 1 wins due to player 2 going over 1 dollar total value
    End If
    
    If dblTotal2 = 1 Then
        MsgBox strNameP2 & " WINS!" 'Checks to see if player 1 has 1 dollar on first spin, if so he or she wins automatically
        End
    End If
    
    MsgBox "Please press the Who Won button to see who won."     'Tells user to press the Who Won button to see who won
    
    cmdSpin2B.Visible = False     'Removes the spin again button for player 2
End Sub

Private Sub cmdWon_Click()
    cmdSpin2A.Visible = False     'Removes the spin again button for player 1
    
    lblValue1.Visible = False     'Removes the label showing the first spin value for player 2
    
    lblValue2.Visible = False     'Removes the label showing the second spin value for player 2
    
    lblValue3.Visible = False     'Removes the label showing the total spin value for player 2
    
    If dblTotal1 <= 1 And dblTotal1 > dblTotal2 Then     'Decides if player 1 won by seeing if their total spun value is the closest to 100
        lblWinner.Visible = True     'Displays winner label
        MsgBox strNameP1 & " WINS!"     'Says player 1 wins
        lblWinner.Caption = strNameP1 & " wins with a total of " & (dblTotal1 * 100) & " cents!"   'Says who won on the winner label and their total spun value
    Else
        lblWinner.Visible = True     'Says if player 1's total spun value wasn't closest to 100 the player 2's total spun value was
        MsgBox strNameP2 & " WINS!"     'Says player 2 wins
        lblWinner.Caption = strNameP2 & " wins with a total of " & (dblTotal2 * 100) & " cents!"   'Says who won on the winner label and their total spun value
    End If
    
    cmdWon.Visible = False
End Sub

Private Sub cmdDone_Click()
    Unload Me     'Ends Game
End Sub

Game works really well. I do have to integrate a counter, and a loop somehow. Preferably in conjunction. Anyone have any ideas on how change something above into a Do-While or For-Next Loop with a counter? That will score me six more points.
 
Status
Not open for further replies.
Back
Top Bottom