Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Hello, I am making a guessing game in excel. I have completed the construction of the basic framework ( such as text box settings, start

Hello, I am making a guessing game in excel. I have completed the construction of the basic framework (such as text box settings, start and end button settings) but have not completed writing the VBA code. Please help me follow the following requirements. Complete the VBA code. grateful.
The game itself is a very simple game. At the start of the game, the computer thinks of a secret number between 1 and 100. The player then has to guess the number correctly using as few guesses as possible.
You will clear the game display in the workbook open event.
First, you need to clear the text inside the number input cell at the start. You can do that by clearing the Value property of the cell, like this:
Worksheets("Game").Range("Number").Value =""
Then, you need to clear the information display so that the game can put some useful information there later.
This is a little tricky as the text display is an Excel text shape. To change the text content of an Excel text shape , you need to change the TextFrame of the shape, like this:
Worksheets("Game").Shapes("Hint").TextFrame.Characters.Text =...new content...
The above line of code is very long but it is only a simple property of the Excel shape for changing its text content.
At the start of the game, the computer needs to think of a secret number. This number will be between 1 and 100 inclusive. You can generate such a number using VBA code easily, using Randomize() and Rnd() that you have used.
You can create a global variable by putting the following line at the very top of the code area, outside of any macro:
Dim SecretNumber As Integer
Let's emphasize again that the above line of code has to be put at the top of the code area, instead of inside any function or subroutine. The variable that the above line of code creates is called SecretNumber. You will use this variable to store the random number and you will use this variable throughout the rest of the game.
The final task of this exercise is to check the player's guess. The player makes his/her guess by entering the number in the number input cell and then clicking on the 'Make My Guess!' button. Therefore, the VBA code that you will use to check the guess should go inside the MakeMyGuess macro of the 'Make My Guess!' button.
Inside the macro, you need to check the following situations based on the player's input:
1. If the guess is not a valid number
2. If the guess is not inside the range of 1 and 100
3. If the guess is smaller than SecretNumber
4. If the guess is bigger than SecretNumber
5. Otherwise, the guess must be SecretNumber
As you can see, the above situations can be represented in a very big If...ElseIf...Else statement, i.e.
If ...the guess is not a valid number... Then
Hint =...some message...
ElseIf ...the guess is not within 1 to 100... Then
Hint =...some message...
ElseIf ...the guess is smaller than the secret number... Then
Hint =...some message...
ElseIf ...the guess is bigger than the secret number... Then
Hint =...some message...
Else ' By now, the guess must be the secret number
Hint =...some message...
End If
...Put the Hint into the information display...
When the player correctly guesses the secret number, the game is over. You need to tell the player that he/she has finished the game using the information text display so that the player can start the game again.
You don't need to clear the content of the number input cell or the information text display because the player still needs to know that he/she has finished the game by reading the information content.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions