Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Game Programming > The Price Is Right: Spin the Big Wheel game.


Reply
 
Thread Tools Display Modes
  #1  
Old 12-15-2005, 06:06 PM
lazerman lazerman is offline
Newcomer
 
Join Date: Dec 2005
Posts: 12
Default The Price Is Right: Spin the Big Wheel game.


Okay so we just got our final project for this semester....

We have a choice out of what type of project we do in Programming 1 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.
Reply With Quote
  #2  
Old 12-15-2005, 07:58 PM
Jastin Jastin is offline
Newcomer
 
Join Date: May 2003
Posts: 16
Default

For your names you will want to use two seperate variables or an array. You would probably be better off without a loop.

Random numbers are generated with this formula
Code:
RandomCharacter = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
As far as the players go, you could put the game part in a loop or a sub routine or function and that would represent one turn. Example like this.
Code:
Function Turn() as Integer
' Generate First number
' Ask if they want to spin again
' Generate second number
  Turn = intSpinOne + intSpinTwo' Return the value
End Function
That should get you started.
Reply With Quote
  #3  
Old 12-15-2005, 08:58 PM
lazerman lazerman is offline
Newcomer
 
Join Date: Dec 2005
Posts: 12
Default

Okay well I was taught random numbers differently, here is how I do it"
intx = Int(Rnd*11)+50 which would generate a range of numbers between 50 and 60. I just need to know how to get it to step in increments. I tried:

Code:
dblValue = int(Rnd*100)+1 Step 5
but it didn't work.

I revised my code some:

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
We haven't covered functions yet, but I understand the loop concept. Ahh this is confusing!
Reply With Quote
  #4  
Old 12-16-2005, 03:30 AM
makster246 makster246 is offline
Newcomer
 
Join Date: Sep 2004
Posts: 11
Default

Hiya, i have just thought of a cool idea for it...
you may like it, you may not..

first of all, throw all the numbers into an array. (duh)


Code:
Option Explicit

Dim MyArray(5) As Double
Dim RandSelect As Long 'Number Selector
Dim i As Long 'Counter
Dim Pass As Boolean

Private Sub Command1_Click()
Pass = False
RandSelect = Rnd() * 4 + 1
Timer1.Enabled = True
MsgBox RandSelect
End Sub

Private Sub Form_Load()
Pass = False
Randomize

MyArray(1) = 0.2
MyArray(2) = 0.3
MyArray(3) = 0.6
MyArray(4) = 0.8
MyArray(5) = 1

End Sub

Private Sub Timer1_Timer()

    Text1.Text = MyArray(i)
    


If Pass = True And i = RandSelect Then
    Timer1.Enabled = False
    Text1.Text = MyArray(RandSelect)
    Pass = False
End If

If i = RandSelect Then
    Pass = True
    
End If

If i = UBound(MyArray) Then
     i = 1
End If

i = i + 1

End Sub
That code will display the numbers on the screen as if its running through them. obviously the more numbers in the array, and the more passes it does, the fancier it will look.

there are three controls on the form "command1", "text1" and "timer1" (the timer is set to disable and the time on it is what ever you fancy.

if you need more info just ask

Rob
Reply With Quote
  #5  
Old 12-16-2005, 04:44 AM
Iceplug's Avatar
Iceplug Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
Default

Actually, if you sit back and look at the wheel increments, they are increasing by 5 cents... and there are 20 of these numbers.

So, if you generate one of 20 random numbers, you can use another operation to convert it to good prices.

X = Int(Rnd * 20)
When X = 0, Price should be 0.05 (intercept)
When goes up by 1, Price should go up by 0.05 (slope).
So:
Price = 0.05 * X + 0.05
or
Price = 0.05 * (X + 1)
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #6  
Old 12-16-2005, 10:40 PM
lazerman lazerman is offline
Newcomer
 
Join Date: Dec 2005
Posts: 12
Default

I am pretty much done. Thanks for the suggestions. Looks like I finished almost 3 weeks early lol !

So there isn't a way to get my random numbers to increment using that formula?

Here is the completed game, minus the 5 cent increments:

Edit: Had to put into another post, it was too long lol.
Reply With Quote
  #7  
Old 12-16-2005, 10:41 PM
lazerman lazerman is offline
Newcomer
 
Join Date: Dec 2005
Posts: 12
Default

Sorry for all the comments, our teacher wants us to use those. I think it clutters stuff up personally.

Code:
Option Explicit
Private intValue1 As Integer, intValue2 As Integer, strNameP1 As String, strNameP2 As String, intValue3 As Integer, intValue4 As Integer, intTotal1 As Integer, intTotal2 As Integer

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()
    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()
    intValue2 = 0     'Sets intValue 2 for spin 2 to 0 in case player 1 only spins once
    
    intValue1 = Int(Rnd * 100) + 1     'Generates amount of first spin
    
    MsgBox "You have a value of " & intValue1 & " cents for your first spin."     'Message box telling player 1 his or her first spin value
    
    lblValue1.Caption = "Value of first spin: " & intValue1     'Displays value of first spin by player 1 on a label
    
    lblValue1.Visible = True     'Makes the first spin value for player 1 visible
    
    intTotal1 = (intValue1 + intValue2)     'Calculates the total for both spins; for only one spin, the total is intValue1
    
    If intValue1 <= 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
    Else
        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()
    intValue2 = Int(Rnd * 100) + 1     'Generates amount of second spin
    
    MsgBox "You have a value of " & intValue2 & " cents for your second spin."     'Message box telling player 1 his or her second spin value
    
    lblValue2.Caption = "Value of second spin: " & intValue2 'Displays the value of the second spin for player 1
    
    lblValue2.Visible = True     'Makes the second spin value for player one visible
    
    intTotal1 = (intValue1 + intValue2)     'Calculates the total for both spins, sum of intValue1 plus intValue2
    
    lblValue3.Caption = "Total value of both spins: " & intTotal1     'Displays the total value of both spins
    
    lblValue3.Visible = True     'Makes the total value label visible
    
    If intTotal1 > 100 Then    'Checks to see if player 1 went over 1 dollar, in which case player 2 wins automatically
        MsgBox "You went over a dollar, 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
    
    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()
    intValue4 = 0     'Sets intValue4 for spin 2 to 0 in case player 2 only spins once
    
    intValue3 = Int(Rnd * 100) + 1     'Generates amount of the first spin for player 2
    
    MsgBox "You have a value of " & intValue3 & " cents for your first spin."     'Message box telling player 2 his or her first spin value
    
    lblValue1.Caption = "Value of first spin: " & intValue3     'Displays the value of the first spin for player 2
    
    lblValue1.Visible = True     'Makes label for player 2's first spin value visible
    
    intTotal2 = (intValue3 + intValue4)     'Calculates the total for both spins; for only one spin, the total is intValue3
    
    If intValue1 <= 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? 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
    Else
        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()
    intValue4 = Int(Rnd * 100) + 1     'Generates amount of second spin
    
    MsgBox "You have a value of " & intValue4 & " cents for your second spin."     'Message box telling player 2 his or her second spin value
    
    lblValue2.Caption = "Value of second spin: " & intValue4     'Displays the value of the second spin for player two
    
    lblValue2.Visible = True     'Makes the label for the second spin value visible
    
    intTotal2 = (intValue3 + intValue4)     'Calculates the total for both spins, sum of intValue3 plus intValue4
    
    lblValue3.Caption = "Total value of both spins: " & intTotal2     '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 intTotal2 > 100 Then     ''Checks to see if player 2 went over 1 dollar, in which case player 1 wins automatically
        MsgBox "You went over a dollar, 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
    
    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 intTotal1 <= 100 And intTotal1 > intTotal2 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 " & intTotal1 & " 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 " & intTotal2 & " 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
Reply With Quote
  #8  
Old 12-17-2005, 04:01 AM
Iceplug's Avatar
Iceplug Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
Default

No, comments are good. Within VB6, you can always specify a less distracting color for comments, or find a style for using comments. They are immensely helpful if you have a long project or a project that you are going to put off for a while.
Quote:
So there isn't a way to get my random numbers to increment using that formula?
What do you mean?
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #9  
Old 12-17-2005, 10:13 AM
lazerman lazerman is offline
Newcomer
 
Join Date: Dec 2005
Posts: 12
Default

I mean, is there a way to get this formula:

intValue1 = Int(Rnd * 100) + 1

to increment in steps of 5? So when they spin the wheel it is 5, 10, 15, 20 etc.... Like your equation, how would I plug that into my game?

EDIT:

Nvm I got it to work.... Now I just need to find out what my teacher wants it to do if both players spin 100 cents. I had to change everything to double and dbl for the names (teacher wants that, example: dblValue1).

Here is the code:

Last edited by lazerman; 12-17-2005 at 11:02 AM.
Reply With Quote
  #10  
Old 12-17-2005, 11:04 AM
lazerman lazerman is offline
Newcomer
 
Join Date: Dec 2005
Posts: 12
Default

Edit: How do I post long code? Do I have to break it into sections?
Reply With Quote
  #11  
Old 12-19-2005, 07:53 PM
lazerman lazerman is offline
Newcomer
 
Join Date: Dec 2005
Posts: 12
Default

Okay so I have been cracking away at this. I have my counter that I needed, I used it in the counting of sudden death matches (if it happens more than once in a row).

I am having an issue with the numbers generated. Here is spin one of the first player:

Code:
Private Sub cmdSpin1A_Click()
    dblValue2 = 0     'Sets intValue 2 for spin 2 to 0 in case player 1 only spins once
    
    dblX = Int(Rnd * 19) + 1   'Creates a random number from the range of 1-20 and stores it in dblX
    
    dblValue1 = 0.05 * dblX + 0.05   'Multiplies dblX--the random increment number--times .05 to get the 5 cent increment and then tells it to start at 5 cents and stores it in dblValue1
    
    MsgBox "You have spun 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
End Sub
Originally, I wanted the values generated to be 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, and 100.

Now, I learn that isn't right. I need the values to start at 5 and run through 100 like so: 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, and 100.

I keep trying, and as you can see, my formula is a tad different than what was recommended by Iceplug (thanks). Here is the orginal formula:

dblX = Int(Rnd * 20)
dblValue1= 0.05 * dblX + 0.05


For some reason, that was generating a 0. I don't I am confused with it deeply lol. Someone please aid me!
Reply With Quote
  #12  
Old 12-20-2005, 07:41 AM
esion esion is offline
Freshman
 
Join Date: Oct 2003
Posts: 34
Default

Maybe I am completely off the mark but I will toss out what I am seeing. From your coded formula i see that when u get the random generated 0, then dblX is 1 and the lowest dblValue1 u can get is .1 or 10 cents. From the code in your post when u have the random 0, then u can get .05 or 5 cents. And since rnd can never return 1 I think the code in your post should work for what you need.

As for returning 0, I am not sure how that is possible with your current code. As for the code in the post is possible to get 0 due to rnd returning 0, but dblValue1 would never equal 0 due to the appended +0.05.

Just tossing out what I see during work, sorry I couldnt help more...ill think about it while here.
Reply With Quote
  #13  
Old 12-20-2005, 04:18 PM
Iceplug's Avatar
Iceplug Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
Default

The code above should not be having a dblValue with 0 in it...
now, if you happened to accidentally convert the value of dblValue to an integer, then that could cause a 0 to appear.

Also, if you don't have Option Explicit on, then add it to the top of your form window... this sounds like one of those cases... especially if it was generating a zero everytime.

And if it is truly bothering you to have decimal values for the dollar value instead of the number of cents, multiply through by 100... that would be:
dblValue1= 5 * dblX + 5
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #14  
Old 12-20-2005, 09:15 PM
lazerman lazerman is offline
Newcomer
 
Join Date: Dec 2005
Posts: 12
Default

Okay I feel dumb lol.... I just realized that it was only spinning a 0 on player 2's second turn.... So I checked the code, and sure enough, it was recalculating the spin 1 value (dblValue3), while the label and message box were displaying the proper value of 0 (I had set the right variable--dblValue4--to 0 in the previous spin so I could get a total). Ok so it works now.... I will be back soon thogh, I have programming class again tomorrow to the teach will give me more ideas lol.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->