Computer Programming

Computer Programming » An Actual Computer Program » Additional Roll for Win or Lost

Additional Roll for Win or Lost Calculation

Since there will be additional dice rolls and additional messages beyond the initial come out roll, we need to add three more text boxes.


Add labels to identify the next two dice rolls.


Rename the text boxes to hold the next dice rolls as DiceRoll1B and DiceRoll2B.


Name the last one FinalResult. It will display the final message to say if the user has won or lost the game based on the initial come out dice roll as well as any additional dice rolls if needed.


This latest intermediate solution suffers from the same problem as one of the previous intermediate programs. We did not set up a means to clear out previous games.


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
        Dim NextRoll 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

        ' Do more if we have to because we need to do more rolls
        If (ComeOut = 4 Or ComeOut = 5 Or ComeOut = 6 Or ComeOut = 8 Or ComeOut = 9 Or ComeOut = 10) Then
            ' Produce the random dice rolls
            Randomize()
            Dice1 = CInt(Int(6 * Rnd()) + 1)
            Dice2 = CInt(Int(6 * Rnd()) + 1)
            NextRoll = Dice1 + Dice2

            ' Display the result in the second dice roll area
            DiceRoll1B.Text = Dice1
            DiceRoll2B.Text = Dice2

            ' Determine if this result means correct
            If (NextRoll = 7) Then
                FinalResult.Text = "You Lost!"
            ElseIf (NextRoll = ComeOut) Then
                FinalResult.Text = "You Win!"
            Else
                FinalResult.Text = "More rolls needed"
            End If
        End If

    End Sub
End Class

The next section of code added is large though it reproduces many of the earlier code that we covered already. The section is Do more if we have to because we need to do more rolls which is based on the previous rolls. If we need to roll again, this section will determine that and then run the code related to rolling again.

If (ComeOut = 4 Or ComeOut = 5 Or ComeOut = 6 Or ComeOut = 8 Or ComeOut = 9 Or ComeOut = 10) Then
End If

The section Produce the random dice rolls is conceptually the same as what we have done before. We will reuse the same variables we used before. Even though we change the value in the variable, it will not change the value in the textbox. The value is copied over to the text box during the assignment.

Randomize()
Dice1 = CInt(Int(6 * Rnd()) + 1)
Dice2 = CInt(Int(6 * Rnd()) + 1)
NextRoll = Dice1 + Dice2

The section Display the result in the second dice roll area takes the dice rolls and shows them. The variables and the text boxes are different buckets.

DiceRoll1B.Text = Dice1
DiceRoll2B.Text = Dice2

The section Determine if this result means correct will calculate if a user has won or lost by comparing the current dice roll against the come out roll. For this situation, we will use an elseif. It allows us to identify mutually exclusive situations. If the roll is a 7 there is no need to do any more comparisons. We only check against the come out roll if the roll is not a 7. Finally, we have the situation in which the new dice roll does not match a 7 nor does it match the come out roll, which is the else.

If (NextRoll = 7) Then
    FinalResult.Text = "You Lost!"
ElseIf (NextRoll = ComeOut) Then
    FinalResult.Text = "You Win!"
Else
    FinalResult.Text = "More rolls needed"
End If

Run the program. If more dice rolls are needed after the initial come out roll, we will roll one more time and then use that roll to determine if the user has won or lost or has to roll again. We really want to keep rolling over and over when there is no win or lost message.


Hit the go button to run. We are getting closer to a full program.


⇐ Prev Chapter
Initial win or lost calculation

Next Chapter ⇒
Keep rolling until lost or win

Computer Programming for Everyday People
Michael Bigrigg

Amazon eBook

Powered by w3.css