Pls Help me create a simple guessing game in vba code!!

l0lcat

New Member
Joined
May 23, 2015
Messages
5
Design a simple guessing game, played by one player against the computer.

When the program begins it will ask the user for their name, then welcome them to the Guessing Game, saying “Hello <name>, welcome to the CARD Guessing Game”. The game will then begin, displaying the players name and current balance (which starts at $100). The player will be asked how much they want to bet, which must be a whole number no less than 1 and no more than their balance.

Once their bet has been entered the game will randomly generate two numbers, one for the player and one for the computer. These numbers are to be whole numbers between 1 and 13 (representing cards: Ace, 2 -10, Jack, Queen, King).

The player will then be told the value of their card as either a number (if the value is between 2 and 10) or the name of the card (1 is an Ace, 11 is a Jack, 12 is a Queen, and 13 is a King). The player will then choose whether they think the computers card will be higher or lower, by entering H or L. Upper and lower case must be accepted, but any other input will result in an error and being asked again. Once the player has chosen then the computers card will be announced to the player.

If the player was correct they get double their bet back (e.g. started with 100, bet 10 (balance is 90), win 20 (balance is 110). If the player was wrong they lost their bet (e.g. started with 100, bet 10 (balance is 90)), and if the two cards are equal then it’s a tie (e.g. started with 100, bet 10 (balance is 90), get 10 back (balance is 100)) and the next round starts.

Each round follows the same pattern:
· Display name and balance
· Take the bet
· Reveal players card
· Get players guess
· Reveal computers card with winning/losing message, update balance

After each round the player can choose to continue, or quit. If the players balance hits zero then the game ends, as if they had chosen to quit.

Once the game ends a farewell message will be displayed, noting the final balance and whether or not the player won (or lost) money overall.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try something like this:

Code:
Sub HigherOrLower()

  Dim sPlayerName As String
  Dim lBalance As Long
  Dim vCardValues
  Dim vBetValue
  Dim iRandomCard(1 To 2) As Integer
  Dim sHighOrLow As String
  Dim sMessage As String
  
  'Get player name
  sPlayerName = InputBox( _
    Prompt:="Enter your name:", _
    Title:="Welcome to Higher Or Lower", _
    Default:=Application.UserName)
  
  'Exit if cancelled
  If sPlayerName = vbNullString Then Exit Sub
  
  vCardValues = Array("A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K")
  
  'Set initial balance
  lBalance = 100
  
GetBet:
  
  'Get bet value
  vBetValue = Application.InputBox( _
    Prompt:=sPlayerName & ", your current balance is $" & lBalance & "." _
      & vbCrLf & vbCrLf & "How much would you like to bet?", _
    Title:="Your Bet", _
    Default:=lBalance, _
    Type:=1)
  
  'Exit if cancelled
  If vBetValue = False Then Exit Sub
  
  'Validate entry
  If vBetValue <> Int(vBetValue) _
  Or vBetValue < 1 _
  Or vBetValue > lBalance Then
    MsgBox sPlayerName & ", you must enter a whole number between 1 and " _
      & lBalance & ".", vbExclamation
    GoTo GetBet
  End If

  'Generate random cards
  Randomize
  iRandomCard(1) = Int(13 * rnd + 1)  'player's card
  iRandomCard(2) = Int(13 * rnd + 1)  'computer's card
  
GetHighOrLow:
  
  'Get higher or lower guess
  sHighOrLow = InputBox( _
    Prompt:=sPlayerName & ", your card is: " & Application.Index(vCardValues, iRandomCard(1)) _
      & vbCrLf & vbCrLf & "Do you think the computer's card is higher [H] or lower [L]?", _
    Title:="Higher Or Lower?", _
    Default:="H")
  
  'Exit if cancelled
  If sHighOrLow = vbNullString Then Exit Sub
  
  'Validate entry
  If Not UCase$(sHighOrLow) Like "[HL]" Then
    MsgBox sPlayerName & ", you must enter [H] for higher or [L] for lower.", vbExclamation
    GoTo GetHighOrLow
  End If
  
  sMessage = vbCrLf & vbCrLf & "The computer's card was: " & _
    Application.Index(vCardValues, iRandomCard(2)) & "."
  
  'Check results and adjust balance
  Select Case UCase$(sHighOrLow)
    Case "H"  'player guessed higher
      Select Case True
        Case iRandomCard(2) > iRandomCard(1)
          MsgBox "That's correct, " & sPlayerName & "." & sMessage, vbInformation, "Result"
          lBalance = lBalance + vBetValue
        Case iRandomCard(2) < iRandomCard(1)
          MsgBox "That's incorrect, " & sPlayerName & "." & sMessage, vbInformation, "Result"
          lBalance = lBalance - vBetValue
        Case Else
          MsgBox "It's a tie, " & sPlayerName & "." & sMessage, vbInformation, "Result"
      End Select
    Case "L"  'player guessed lower
      Select Case True
        Case iRandomCard(2) < iRandomCard(1)
          MsgBox "That's correct, " & sPlayerName & "." & sMessage, vbInformation, "Result"
          lBalance = lBalance + vBetValue
        Case iRandomCard(2) > iRandomCard(1)
          MsgBox "That's incorrect, " & sPlayerName & "." & sMessage, vbInformation, "Result"
          lBalance = lBalance - vBetValue
        Case Else
          MsgBox "It's a tie, " & sPlayerName & "." & sMessage, vbInformation, "Result"
      End Select
  End Select
  
  'Get new bet if player still in credit
  If lBalance > 0 Then GoTo GetBet
  
  'Goodbye message
  MsgBox sPlayerName & ", your balance is $0." & vbCrLf & vbCrLf _
    & "Thanks for playing Higher Or Lower.", vbInformation, "Goodbye"
  
End Sub
 
Upvote 0
Would you know how to break this down into these functions, then call them in Sub main()

getName
getBetNum
getGuess
calcResults
 
Upvote 0

Forum statistics

Threads
1,213,533
Messages
6,114,179
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top