Add a new text box, which we will use to provide feedback to the user. For the come out roll, the user may win with a 7 or win with an 11. We will calculate that and display an approprite message.
Rename it ComeOutResult. There is nothing special about using the word result in our variable. We can decide on our own how to name variables so that it makes sense to us so that when we are programming, we can remember which text box we want to use for display.
In the next code, we need to refer to the description for our game to determine which feedback message we want to display to the user.
Public Class Form1 Private Sub GoButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GoButton.Click ' Declare the variables Dim Dice1 As Integer Dim Dice2 As Integer Dim ComeOut As Integer ' Produce the random dice rolls Randomize() Dice1 = CInt(Int(6 * Rnd()) + 1) Dice2 = CInt(Int(6 * Rnd()) + 1) ' Display the result DiceRoll1.Text = Dice1 DiceRoll2.Text = Dice2 ' Compute the come out roll ComeOut = Dice1 + Dice2 ' Display the come out roll ComeOutRoll.Text = ComeOut ' Determine if Win via come out roll If ComeOut = 7 Then ComeOutResult.Text = "You Win!" End If If ComeOut = 11 Then ComeOutResult.Text = "You Win!" End If End Sub End Class
The section of code that has been added is the Determine if Win via come out roll which displays a feedback message. We will use an if statement which is used to do something only in certain situation. In our game if a user rolls a combined sum of 7 for the first roll of the dice, then the user wins. Since we calculated the come out value, we can compare that variable against a 7 to determine if we should let the user that he has won. Only when the comparison is correct, which is called true, is the internal code run. We identify which code is internal, which is called nested, between the then word and the end if words.
Next we determine if the user has won based on the first roll of the dice against an 11. These two decisions are run one after another so that even if we have a 7 and the computer puts the win message, we still check to see if the come out roll is an 11. When statements are run one after another, they are called sequential. The computer will run each statement as it come across it from the start of the program to the end. It does not evaluate our work to determine if it is extra work to do the second statement in the case that we already produced a win message for a 7. It does just what we tell it to do.
If ComeOut = 7 Then
ComeOutResult.Text = "You Win!"
End If
If ComeOut = 11 Then
ComeOutResult.Text = "You Win!"
End If
Run the program. Now it will say if you won on a 7 or if you won on an 11 and then otherwise it does not say anything.
What about when we won already and try again by pressing the Go button? The message is already there from before. We did not do anything to erase it and so it will always say that the user has won once the user has won once. We did not set up the situation to run again. What we would want to do is to write a few blank spaces to remove any previous message. Then if the user wins, the win statement would replace our spaces. The statement to do that would go before our code, as ComeOutResult.Text = " ". The reason to bring this up is that every assignment to a variable or a property will replace the previous assignment.
The next code will account for a lost situation as well. Same situation as before, we did not clear out the text box from one run to another. This code is only an intermediate step. We will eventually have a complete program.
Public Class Form1 Private Sub GoButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GoButton.Click ' Declare the variables Dim Dice1 As Integer Dim Dice2 As Integer Dim ComeOut As Integer ' Produce the random dice rolls Randomize() Dice1 = CInt(Int(6 * Rnd()) + 1) Dice2 = CInt(Int(6 * Rnd()) + 1) ' Display the result DiceRoll1.Text = Dice1 DiceRoll2.Text = Dice2 ' Compute the come out roll ComeOut = Dice1 + Dice2 ' Display the come out roll ComeOutRoll.Text = ComeOut ' Determine if Win via come out roll If ComeOut = 7 Then ComeOutResult.Text = "You Win!" End If If ComeOut = 11 Then ComeOutResult.Text = "You Win!" End If ' Determine if Lose via come out roll If (ComeOut = 2 Or ComeOut = 3 Or ComeOut = 12) Then ComeOutResult.Text = "You Lost!" End If End Sub End Class
The next section of code added is Determine if Lose via come out roll which does a similar determination as we did with the win situation. In the win situation we checked for each number individually. This time, we introduce the or. It allows us to check for multiple situations. The user will lose if the initial roll is 2, 3, or 12. Many new programmers will try to write the statement as (ComeOut = 2 Or 3 Or 12), but it is not a test for multiple values, but for multiple situations. ComeOut = 2 is a situation, whereas 3 is just a value. We can string together many situations, not many values.
If (ComeOut = 2 Or ComeOut = 3 Or ComeOut = 12) Then
ComeOutResult.Text = "You Lost!"
End If
Run the program. This time the user will be told if he has won or lost the first roll, again only the first time the program is run.
Now finally we will have a full program that will handle all situations for the first roll-- either won or lost or having to roll again. It will produce a correct response for every time the Go button is pressed.
Public Class Form1 Private Sub GoButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GoButton.Click ' Declare the variables Dim Dice1 As Integer Dim Dice2 As Integer Dim ComeOut As Integer ' Produce the random dice rolls Randomize() Dice1 = CInt(Int(6 * Rnd()) + 1) Dice2 = CInt(Int(6 * Rnd()) + 1) ' Display the result DiceRoll1.Text = Dice1 DiceRoll2.Text = Dice2 ' Compute the come out roll ComeOut = Dice1 + Dice2 ' Display the come out roll ComeOutRoll.Text = ComeOut ' Set it up to initially say roll again ComeOutResult.Text = "Need to roll again" ' Determine if Win via come out roll If ComeOut = 7 Then ComeOutResult.Text = "You Win!" End If If ComeOut = 11 Then ComeOutResult.Text = "You Win!" End If ' Determine if Lose via come out roll If (ComeOut = 2 Or ComeOut = 3 Or ComeOut = 12) Then ComeOutResult.Text = "You Lost!" End If End Sub End Class
This time we added a section to the middle of our code Set it up to initially say roll again which is similar to clearing out the message. It is displayed in the text box but the code will quickly replace it with a win or lost message if appropriate.
ComeOutResult.Text = "Need to roll again"
Run the program. Now we have a full intermediate program that will work for every press of the Go button.